Skip to content

Commit e4efb05

Browse files
committed
PDFBOX-5911: move dpi calculation to top and add javadoc, as suggested by Kevin Day; add command line info; make constructors more indirect
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922294 13f79535-47bb-0310-9956-ffa450edef68
1 parent f765bf5 commit e4efb05

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
public final class PDFPrintable implements Printable
4747
{
4848
private static final Logger LOG = LogManager.getLogger(PDFPrintable.class);
49+
public static final float RASTERIZE_OFF = 0f;
50+
public static final float RASTERIZE_DPI_AUTO = -1f;
4951

5052
private final PDPageTree pageTree;
5153
private final PDFRenderer renderer;
@@ -75,7 +77,7 @@ public PDFPrintable(PDDocument document)
7577
*/
7678
public PDFPrintable(PDDocument document, Scaling scaling)
7779
{
78-
this(document, scaling, false, 0);
80+
this(document, scaling, false);
7981
}
8082

8183
/**
@@ -87,7 +89,7 @@ public PDFPrintable(PDDocument document, Scaling scaling)
8789
*/
8890
public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder)
8991
{
90-
this(document, scaling, showPageBorder, 0);
92+
this(document, scaling, showPageBorder, RASTERIZE_OFF);
9193
}
9294

9395
/**
@@ -97,7 +99,8 @@ public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder
9799
* @param document the document to print
98100
* @param scaling page scaling policy
99101
* @param showPageBorder true if page borders are to be printed
100-
* @param dpi if non-zero then the image will be rasterized at the given DPI
102+
* @param dpi if positive non-zero then the image will be rasterized at the given DPI. If
103+
* set to the special value RASTERIZE_DPI_AUTO, the dpi of the printer will be used.
101104
*/
102105
public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder, float dpi)
103106
{
@@ -111,7 +114,8 @@ public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder
111114
* @param document the document to print
112115
* @param scaling page scaling policy
113116
* @param showPageBorder true if page borders are to be printed
114-
* @param dpi if non-zero then the image will be rasterized at the given DPI
117+
* @param dpi if positive non-zero then the image will be rasterized at the given DPI. If
118+
* set to the special value RASTERIZE_DPI_AUTO, the dpi of the printer will be used.
115119
* @param center true if the content is to be centered on the page (otherwise top-left).
116120
*/
117121
public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder, float dpi,
@@ -127,7 +131,8 @@ public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder
127131
* @param document the document to print
128132
* @param scaling page scaling policy
129133
* @param showPageBorder true if page borders are to be printed
130-
* @param dpi if non-zero then the image will be rasterized at the given DPI
134+
* @param dpi if positive non-zero then the image will be rasterized at the given DPI. If
135+
* set to the special value RASTERIZE_DPI_AUTO, the dpi of the printer will be used.
131136
* @param center true if the content is to be centered on the page (otherwise top-left).
132137
* @param renderer the document renderer. Useful if {@link PDFRenderer} has been subclassed.
133138
*/
@@ -203,6 +208,14 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
203208
{
204209
Graphics2D graphics2D = (Graphics2D)graphics;
205210

211+
// capture the DPI that will be used for rasterizing the image
212+
// if rasterizing is specified
213+
float rasterDpi = dpi;
214+
if (rasterDpi == RASTERIZE_DPI_AUTO)
215+
{
216+
rasterDpi = (float) graphics2D.getTransform().getScaleX() * 72.0f;
217+
}
218+
206219
PDPage page = pageTree.get(pageIndex);
207220
PDRectangle cropBox = getRotatedCropBox(page);
208221

@@ -253,18 +266,10 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
253266
// rasterize to bitmap (optional)
254267
Graphics2D printerGraphics = null;
255268
BufferedImage image = null;
256-
if (dpi > 0 || dpi == -1)
269+
if (dpi > 0 || dpi == RASTERIZE_DPI_AUTO)
257270
{
258-
float dpiScale;
259-
if (dpi == -1)
260-
{
261-
dpiScale = (float) graphics2D.getTransform().getScaleX();
262-
LOG.debug("dpi set to {}", Math.round(graphics2D.getTransform().getScaleX() * 72));
263-
}
264-
else
265-
{
266-
dpiScale = dpi / 72;
267-
}
271+
LOG.debug("dpi set to {}", rasterDpi);
272+
float dpiScale = rasterDpi / 72;
268273
image = new BufferedImage((int)(imageableWidth * dpiScale / scale),
269274
(int)(imageableHeight * dpiScale / scale),
270275
BufferedImage.TYPE_INT_ARGB);

tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Sides toSides()
101101
@Option(names = "-border", description = "print with border.")
102102
private boolean border;
103103

104-
@Option(names = "-dpi", description = "render into intermediate image with specific dpi and then print.")
104+
@Option(names = "-dpi", description = "render into intermediate image with specific dpi and then print. Use \"-1\" for the dpi of the printer.")
105105
private int dpi;
106106

107107
@Option(names = "-noCenter", description = "align top-left (default: center on page).")

0 commit comments

Comments
 (0)