Author: tilman
Date: Fri May 8 18:38:48 2026
New Revision: 1933959
Log:
PDFBOX-6191: create new graphics device to avoid mutating caller state, by
Valery Bokov and Axel Howind; closes #443
Modified:
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Modified:
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
==============================================================================
---
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Fri May 8 18:38:45 2026 (r1933958)
+++
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Fri May 8 18:38:48 2026 (r1933959)
@@ -206,8 +206,11 @@ public final class PDFPrintable implemen
return NO_SUCH_PAGE;
}
- Graphics2D printerGraphics = null;
- Graphics2D graphics2D = null;
+ // work on a private copy so the caller's Graphics2D state (transform,
clip, color,
+ // background, stroke) is never mutated. Disposing the copy in the
finally block
+ // releases its resources without affecting the original.
+ Graphics2D printerGraphics = (Graphics2D) graphics.create();
+ Graphics2D graphics2D = printerGraphics;
try
{
@@ -332,10 +335,11 @@ public final class PDFPrintable implemen
}
finally
{
- if (graphics2D != null && graphics2D != printerGraphics)
+ if (graphics2D != printerGraphics)
{
graphics2D.dispose();
}
+ printerGraphics.dispose();
}
}