Author: tilman
Date: Mon Apr 13 11:44:03 2026
New Revision: 1933017
Log:
PDFBOX-5660: avoid graphics leak, as suggested by Valery Bokov; closes #440
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
==============================================================================
---
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Mon Apr 13 09:10:26 2026 (r1933016)
+++
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Mon Apr 13 11:44:03 2026 (r1933017)
@@ -206,9 +206,14 @@ public final class PDFPrintable implemen
{
return NO_SUCH_PAGE;
}
+
+ Graphics2D printerGraphics = null;
+ Graphics2D graphics2D = null;
+
try
{
- Graphics2D graphics2D = (Graphics2D)graphics;
+ printerGraphics = (Graphics2D)graphics;
+ graphics2D = printerGraphics;
// capture the DPI that will be used for rasterizing the image
// if rasterizing is specified
@@ -271,7 +276,6 @@ public final class PDFPrintable implemen
}
// rasterize to bitmap (optional)
- Graphics2D printerGraphics = null;
BufferedImage image = null;
if (rasterDpi > 0)
{
@@ -284,7 +288,6 @@ public final class PDFPrintable implemen
(int)(imageableHeight * dpiScale /
scale),
BufferedImage.TYPE_INT_ARGB);
- printerGraphics = graphics2D;
graphics2D = image.createGraphics();
// rescale
@@ -311,12 +314,11 @@ public final class PDFPrintable implemen
}
// draw rasterized bitmap (optional)
- if (printerGraphics != null)
+ if (graphics2D != printerGraphics)
{
printerGraphics.setBackground(Color.WHITE);
printerGraphics.clearRect(0, 0, image.getWidth(),
image.getHeight());
printerGraphics.drawImage(image, 0, 0, null);
- graphics2D.dispose();
}
return PAGE_EXISTS;
@@ -325,6 +327,13 @@ public final class PDFPrintable implemen
{
throw new PrinterIOException(e);
}
+ finally
+ {
+ if (graphics2D != null && graphics2D != printerGraphics)
+ {
+ graphics2D.dispose();
+ }
+ }
}
/**