Skip to content

Commit

Permalink
Fixed pdf page size format & margins for each direction (#215)
Browse files Browse the repository at this point in the history
* [pdf] fixed pdf page size format
* [pdf] apply margins correctly

Signed-off-by: Frank Gasdorf <[email protected]>
  • Loading branch information
fgdrf committed Mar 17, 2017
1 parent 92fef26 commit 98380d9
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.XMLMemento;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.locationtech.udig.catalog.CatalogPlugin;
import org.locationtech.udig.catalog.ICatalog;
import org.locationtech.udig.catalog.ID;
Expand All @@ -44,9 +64,9 @@
import org.locationtech.udig.project.internal.Map;
import org.locationtech.udig.project.render.RenderException;
import org.locationtech.udig.project.ui.ApplicationGIS;
import org.locationtech.udig.project.ui.ApplicationGIS.DrawMapParameter;
import org.locationtech.udig.project.ui.BoundsStrategy;
import org.locationtech.udig.project.ui.SelectionStyle;
import org.locationtech.udig.project.ui.ApplicationGIS.DrawMapParameter;
import org.locationtech.udig.project.ui.internal.Messages;
import org.locationtech.udig.project.ui.internal.ProjectUIPlugin;
import org.locationtech.udig.project.ui.wizard.export.image.Image2Pdf;
Expand All @@ -56,27 +76,6 @@
import org.locationtech.udig.project.ui.wizard.export.image.Paper;
import org.locationtech.udig.project.ui.wizard.export.image.XAffineTransform;
import org.locationtech.udig.style.sld.editor.DialogSettingsStyleContent;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.XMLMemento;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.factory.GeoTools;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
Expand Down Expand Up @@ -196,11 +195,10 @@ public static void exportSivecoMap(IMap map, int dpi, Paper paper, File dest, Se

Image2Pdf.write(image,
dest.getAbsolutePath(),
paper,
10, /*top and bottom margin*/
10, /*left and right margin*/
isLandscape,
dpi);
paper,
/* top, left, bottom and right margin*/
new Insets(10, 10, 10, 10),
isLandscape);

monitor.done();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,100 +10,132 @@
*/
package org.locationtech.udig.project.ui.wizard.export.image;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.eclipse.draw2d.geometry.Insets;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

/**
* @author Andrea Antonello - www.hydrologis.com
* @author Frank Gasdorf
*/
public class Image2Pdf {

/**
* writes a buffered image to pdf at a given resolution
*
*
* @param image
* the image to write
* @param pdfPath
* the path to the pdf document to create
* @param paper
* the paper type
* @param widthBorder
* border in pixels to use on the x-axis
* @param heightBorder
* border in pixels to use on the y-axis
* @param lanscape
* true if the document should be in landscape mode
* @param dpi the output dpi
*/
public static void write(BufferedImage image, String pdfPath, Paper paper,
int widthBorder, int heightBorder, boolean landscape, int dpi) {
Dimension printPageSize = null;
printPageSize = new Dimension(paper.getPixelWidth(landscape, dpi), paper.getPixelHeight(landscape, dpi));

// step 1: creation of a document-object
Document document = new Document(new Rectangle(printPageSize.width,
printPageSize.height));

try {

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfPath));

// step 3: we open the document
document.open();

// step 4: we create a template and a Graphics2D object that
// corresponds with it
int w = printPageSize.width;
int h = printPageSize.height;
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
Graphics2D g2 = tp.createGraphics(w, h);
tp.setWidth(w);
tp.setHeight(h);

g2.drawImage(image, null, widthBorder, heightBorder);

g2.dispose();
cb.addTemplate(tp, 0, 0);

} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.close();
}

public static void main(String[] args) {

String path = "/home/moovida/Desktop/screens/austrocontrol_2.png"; //$NON-NLS-1$
BufferedImage b = null;
try {
b = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
write(b, path + ".pdf", Paper.A1, 10, 10, false, 100); //$NON-NLS-1$
System.out.println("finished"); //$NON-NLS-1$
}
/**
* writes a buffered image to pdf at a given resolution
*
* @param image the image to write
* @param pdfPath the path to the pdf document to create
* @param paper the paper type
* @param marginBorder margins for left, right, top and bottom
* @param lanscape true if the document should be in landscape mode
*/
public static void write(BufferedImage image, String pdfPath, Paper paper,
Insets marginBorder, boolean landscape) {
Rectangle documentPageSize = calculateSize(landscape, paper, null);
Rectangle imageSizeInPixel = calculateSize(landscape, paper, marginBorder);

float imgHeightInPixel = imageSizeInPixel.getHeight();
float imgWidthInPixel = imageSizeInPixel.getWidth();

final Document doc = new Document();
try {

PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
doc.open();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
Image iTextImage = Image.getInstance(baos.toByteArray());

doc.setPageSize(documentPageSize);
doc.newPage(); // not needed for page 1, needed for >1

// high in itext is measured from lower left
int absoluteX = (marginBorder != null ? marginBorder.left : 0);
int absoluteY = (marginBorder != null ? marginBorder.bottom : 0);

iTextImage.setAbsolutePosition(absoluteX, absoluteY);
iTextImage.scaleToFit(imgWidthInPixel, imgHeightInPixel);
doc.add(iTextImage);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
doc.close();
}

/**
* @param landscape if the image is in landscape format
* @param paper paper definition
* @param marginBorder margins for left, right, top and bottom
* @return final image size to render
*/
private static Rectangle calculateSize(boolean landscape, Paper paper, Insets marginBorder) {
Rectangle rectangle = null;
switch (paper) {
case LETTER:
rectangle = PageSize.LETTER;
break;
case LEGAL:
rectangle = PageSize.LEGAL;
break;
case A4:
rectangle = PageSize.A4;
break;
case A3:
rectangle = PageSize.A3;
break;
case A2:
rectangle = PageSize.A2;
break;
case A1:
rectangle = PageSize.A1;
break;
case A0:
rectangle = PageSize.A0;
break;
default:
System.err.println("Cannot handle Paper to PageSize");
}

if (landscape) {
rectangle = new Rectangle(rectangle.getHeight(), rectangle.getWidth());
}

// apply margins for the border
int marginWidth = (marginBorder != null ? marginBorder.left + marginBorder.right : 0);
int marginHeigth = (marginBorder != null ? marginBorder.top + marginBorder.bottom : 0);
return new Rectangle(rectangle.getWidth() - marginWidth,
rectangle.getHeight() - marginHeigth);
}

public static void main(String[] args) {

String path = "/home/moovida/Desktop/screens/austrocontrol_2.png"; //$NON-NLS-1$
BufferedImage b = null;
try {
b = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
write(b, path + ".pdf", Paper.A1, new Insets(10, 10, 10, 10), false); //$NON-NLS-1$
System.out.println("finished"); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import java.awt.image.BufferedImage;
import java.io.File;

import org.locationtech.udig.project.IMap;
import org.locationtech.udig.project.ui.internal.Messages;

import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
Expand All @@ -24,15 +22,20 @@
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.locationtech.udig.project.IMap;
import org.locationtech.udig.project.ui.internal.Messages;

/**
* A strategy for exporting to PDF
*
* @author jesse
* @author Frank Gasdorf
* @since 1.1.0
*/
public class PDFImageExportFormat extends ImageExportFormat {

private static final int PDF_DEFAULT_USER_UNIT = 72;

private Combo dpiCombo;
private Spinner topMarginSpinner;
private Spinner lowerMarginSpinner;
Expand All @@ -57,8 +60,12 @@ public boolean useStandardDimensionControls() {
@Override
public void write( IMap map, BufferedImage image, File destination ) {
Image2Pdf.write(image, destination.getAbsolutePath(), paper(),
this.leftMarginSpinner.getSelection(),
this.topMarginSpinner.getSelection(), landscape(), getDPI());
new Insets(
Paper.toPixels(this.topMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT),
Paper.toPixels(this.leftMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT),
Paper.toPixels(this.lowerMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT),
Paper.toPixels(this.rightMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT)
), landscape());
}

@Override
Expand Down Expand Up @@ -171,18 +178,20 @@ public int getDPI() {

@Override
public int getHeight( double mapwidth, double mapheight ) {
// ignore viewport size of the current map and use paper format instead
int paperHeight = paper().getPixelHeight(landscape(), getDPI());
int topMargin = topMarginSpinner.getSelection();
int lowerMargin = lowerMarginSpinner.getSelection();
int topMargin = Paper.toPixels(topMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT);
int lowerMargin = Paper.toPixels(lowerMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT);

return paperHeight - topMargin - lowerMargin;
}

@Override
public int getWidth( double mapwidth, double mapheight ) {
// ignore viewport size of the current map and use paper format instead
int paperWidth = paper().getPixelWidth(landscape(), getDPI());
int rightMargin = rightMarginSpinner.getSelection();
int leftMargin = leftMarginSpinner.getSelection();
int rightMargin = Paper.toPixels(rightMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT);
int leftMargin = Paper.toPixels(leftMarginSpinner.getSelection(), PDF_DEFAULT_USER_UNIT);

return paperWidth - rightMargin - leftMargin;
}
Expand Down
Loading

0 comments on commit 98380d9

Please sign in to comment.