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
19 changes: 14 additions & 5 deletions pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
{
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
Expand Down Expand Up @@ -266,7 +271,6 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
}

// rasterize to bitmap (optional)
Graphics2D printerGraphics = null;
BufferedImage image = null;
if (rasterDpi > 0)
{
Expand All @@ -276,7 +280,6 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
(int)(imageableHeight * dpiScale / scale),
BufferedImage.TYPE_INT_ARGB);

printerGraphics = graphics2D;
graphics2D = image.createGraphics();

// rescale
Expand All @@ -303,12 +306,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
}

// 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;
Expand All @@ -317,6 +319,13 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
{
throw new PrinterIOException(e);
}
finally
{
if (graphics2D != null && graphics2D != printerGraphics)
{
graphics2D.dispose();
}
}
}

/**
Expand Down