Skip to content

Commit 79ec33f

Browse files
committed
PDFBOX-6191: create new graphics device to avoid mutating caller state, by Valery Bokov and Axel Howind; closes #443
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1933960 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0544f5c commit 79ec33f

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
206206
return NO_SUCH_PAGE;
207207
}
208208

209-
Graphics2D printerGraphics = null;
210-
Graphics2D graphics2D = null;
209+
// work on a private copy so the caller's Graphics2D state (transform, clip, color,
210+
// background, stroke) is never mutated. Disposing the copy in the finally block
211+
// releases its resources without affecting the original.
212+
Graphics2D printerGraphics = (Graphics2D) graphics.create();
213+
Graphics2D graphics2D = printerGraphics;
211214

212215
try
213216
{
@@ -324,10 +327,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
324327
}
325328
finally
326329
{
327-
if (graphics2D != null && graphics2D != printerGraphics)
330+
if (graphics2D != printerGraphics)
328331
{
329332
graphics2D.dispose();
330333
}
334+
printerGraphics.dispose();
331335
}
332336
}
333337

0 commit comments

Comments
 (0)