Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
Expand Down Expand Up @@ -208,11 +210,21 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)

Graphics2D printerGraphics = null;
Graphics2D graphics2D = null;
AffineTransform callerTransform = null;
Shape callerClip = null;
Color callerColor = null;
Color callerBackground = null;
Stroke callerStroke = null;

try
{
printerGraphics = (Graphics2D) graphics;
graphics2D = printerGraphics;
callerTransform = printerGraphics.getTransform();
callerClip = printerGraphics.getClip();
callerColor = printerGraphics.getColor();
callerBackground = printerGraphics.getBackground();
callerStroke = printerGraphics.getStroke();

// capture the DPI that will be used for rasterizing the image
// if rasterizing is specified
Expand Down Expand Up @@ -324,6 +336,16 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
}
finally
{
// restore caller's state (guarded because the cast on line above may have thrown
// before any state was captured)
if (printerGraphics != null && callerTransform != null)
{
printerGraphics.setTransform(callerTransform);
printerGraphics.setClip(callerClip);
printerGraphics.setColor(callerColor);
printerGraphics.setBackground(callerBackground);
printerGraphics.setStroke(callerStroke);
}
if (graphics2D != null && graphics2D != printerGraphics)
{
graphics2D.dispose();
Expand Down