4646public 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 );
0 commit comments