Package org.faceless.pdf2
Class PagePainter
- java.lang.Object
-
- org.faceless.pdf2.PagePainter
-
- All Implemented Interfaces:
java.awt.print.Printable
public class PagePainter extends java.lang.Object implements java.awt.print.Printable
The PagePainter class renders a page of a PDF to aGraphics2D
object - typically to a bitmap image or a printer. At it's simplest it can be used to convert a page to a BufferedImage like soPDFParser parser = new PDFParser(pdf); PagePainter painter = parser.getPagePainter(0); BufferedImage image = painter.getImage(100);
As it implements thePrintable
interface it can also be used for printing - for example, to print the first page of a document you could do something like this:PDFParser parser = new PDFParser(pdf); PagePainter painter = parser.getPagePainter(0); PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet(); printAttributes.add(OrientationRequested.PORTRAIT); MediaPrintableArea area = new MediaPrintableArea(0, 0, 210, 296, MediaPrintableArea.MM); printAttributes.add(area); PrinterJob job = PrinterJob.getPrinterJob(); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(painter); job.print(painter, printAttributes);
- Since:
- 2.5
-
-
Field Summary
Fields Modifier and Type Field Description static java.awt.RenderingHints.Key
KEY_COLORCONVERTRGB
ARenderingHints.Key
which tells the rendering process to convert all colors to RGB.static java.awt.RenderingHints.Key
KEY_SHAPETEXT
ARenderingHints.Key
which determines whether text is rendered as a Shape.static java.awt.RenderingHints.Key
KEY_SHAPETEXT_FONT
ARenderingHints.Key
which allow text that is rendered as a shape to be assocated with a glyph.static java.awt.RenderingHints.Key
KEY_SHAPETEXT_STRING
ARenderingHints.Key
which allow text that is rendered as a shape to be assocated with a glyph.static java.awt.RenderingHints.Key
KEY_SOFTCLIP
ARenderingHints.Key
which determines whether shapes are "soft-clipped" when rendering.static java.lang.Object
VALUE_COLORCONVERTRGB_DEFAULT
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to convert colors to RGB only when it's deemed necessary.static java.lang.Object
VALUE_COLORCONVERTRGB_OFF
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to never convert colors to RGB.static java.lang.Object
VALUE_COLORCONVERTRGB_ON
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to always convert colors to RGB.static java.lang.Object
VALUE_SHAPETEXT_DEFAULT
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to never draw text as shapes.static java.lang.Object
VALUE_SHAPETEXT_OFF
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to draw text as shapes where we think the benefits outweigh the costs.static java.lang.Object
VALUE_SHAPETEXT_ON
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to always draw text as shapes.static java.lang.Object
VALUE_SOFTCLIP_DEFAULT
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to do what it feels is beststatic java.lang.Object
VALUE_SOFTCLIP_OFF
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to clip normally.static java.lang.Object
VALUE_SOFTCLIP_ON
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to use a moderately complex hack for clipping, to work around limitations in the AWT rendering model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
drawSubImage(java.awt.Graphics2D graphics, double x1, double y1, double x2, double y2, float dpi)
Draw an area of a page onto aGraphics2D
object.java.awt.Composite
getAnnotationComposite()
Return theComposite
that should be used to place the annotation painted bypaintAnnotation()
onto the main page.java.awt.image.BufferedImage
getImage(float dpi)
Draw the area inside theViewBox
if specified, or the whole page if not to aBufferedImage
in the standard sRGB ColorModel.java.awt.image.BufferedImage
getImage(float dpi, java.awt.image.ColorModel cm)
Draw the area inside theViewBox
if specified, or the whole page if not to aBufferedImage
in the specified ColorModel.PDFPage
getPage()
Return the PDFPage being painted by this objectjava.awt.image.BufferedImage
getSubImage(double x1, double y1, double x2, double y2, float dpi)
Draw a section of the page to aBufferedImage
in the standard sRGB ColorModeljava.awt.image.BufferedImage
getSubImage(double x1, double y1, double x2, double y2, float dpi, java.awt.image.ColorModel cm)
Draw a section of the page to aBufferedImage
using the specified ColorModelvoid
interrupt()
Interrupts and stops the current painting operation.boolean
isPainting()
Returns true if the current painting operation is still in progress.boolean
paintAnnotation(PDFAnnotation annotation, java.lang.String state, java.awt.Graphics2D graphics, float[] rect)
Paint a single annotation.int
print(java.awt.Graphics g, java.awt.print.PageFormat format, int pagenum)
void
setBackground(java.awt.Paint background)
Set the background of any images drawn with thisPagePainter
.void
setBox(java.lang.String boxname)
Set the default "box" to paint.void
setPageExtractor(PageExtractor extractor)
Set thePageExtractor
to extract text to.void
setPaintAnnotations(boolean paintannotations)
Set whether any annotations on this page are to be painted or not.void
setRenderingHints(java.awt.RenderingHints hints)
Set additional rendering hints on the Graphics object.
-
-
-
Field Detail
-
KEY_COLORCONVERTRGB
public static final java.awt.RenderingHints.Key KEY_COLORCONVERTRGB
ARenderingHints.Key
which tells the rendering process to convert all colors to RGB. This is required when printing due to bugs in thesun.print
package up to Java 1.6.- Since:
- 2.6.3
- See Also:
setRenderingHints(java.awt.RenderingHints)
-
VALUE_COLORCONVERTRGB_DEFAULT
public static final java.lang.Object VALUE_COLORCONVERTRGB_DEFAULT
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to convert colors to RGB only when it's deemed necessary.- Since:
- 2.6.3
-
VALUE_COLORCONVERTRGB_ON
public static final java.lang.Object VALUE_COLORCONVERTRGB_ON
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to always convert colors to RGB.- Since:
- 2.6.3
-
VALUE_COLORCONVERTRGB_OFF
public static final java.lang.Object VALUE_COLORCONVERTRGB_OFF
A Rendering Hint value forKEY_COLORCONVERTRGB
which causes the rendering process to never convert colors to RGB.- Since:
- 2.6.3
-
KEY_SHAPETEXT
public static final java.awt.RenderingHints.Key KEY_SHAPETEXT
ARenderingHints.Key
which determines whether text is rendered as a Shape. Rendering text as shapes gives higher quality glyphs at low DPI, but tends to take about twice as long to draw. When drawing to Graphics2D object that is PostScript based, rendering text as shapes gives no benefit at all but results in much bigger PostScript output files. Generally you want this on for low-DPI bitmap images, and off for printing, which is what theVALUE_SHAPETEXT_DEFAULT
value does.- Since:
- 2.6.3
- See Also:
setRenderingHints(java.awt.RenderingHints)
-
VALUE_SHAPETEXT_OFF
public static final java.lang.Object VALUE_SHAPETEXT_OFF
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to draw text as shapes where we think the benefits outweigh the costs.- Since:
- 2.6.3
-
VALUE_SHAPETEXT_ON
public static final java.lang.Object VALUE_SHAPETEXT_ON
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to always draw text as shapes.- Since:
- 2.6.3
-
VALUE_SHAPETEXT_DEFAULT
public static final java.lang.Object VALUE_SHAPETEXT_DEFAULT
A Rendering Hint value forKEY_SHAPETEXT
which causes the rendering process to never draw text as shapes.- Since:
- 2.6.3
-
KEY_SOFTCLIP
public static final java.awt.RenderingHints.Key KEY_SOFTCLIP
ARenderingHints.Key
which determines whether shapes are "soft-clipped" when rendering. This is a hack which gives better results when rendering to the AWT, but it requires an intermediate buffer so is a little more expensive in terms of time. It is also not appropriate to use when rendering to any other soft of graphics other than the AWT (for example, when printing). Generally you want this on for rendering to a bitmap through the AWT, off otherwise, which is what theVALUE_SOFTCLIP_DEFAULT
value does.- Since:
- 2.19.1
-
VALUE_SOFTCLIP_ON
public static final java.lang.Object VALUE_SOFTCLIP_ON
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to use a moderately complex hack for clipping, to work around limitations in the AWT rendering model. This is good for rendering to AWT, bad for anything else.- Since:
- 2.19.1
-
VALUE_SOFTCLIP_OFF
public static final java.lang.Object VALUE_SOFTCLIP_OFF
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to clip normally.- Since:
- 2.19.1
-
VALUE_SOFTCLIP_DEFAULT
public static final java.lang.Object VALUE_SOFTCLIP_DEFAULT
A Rendering Hint value forKEY_SOFTCLIP
which causes the rendering process to do what it feels is best- Since:
- 2.19.1
-
KEY_SHAPETEXT_FONT
public static final java.awt.RenderingHints.Key KEY_SHAPETEXT_FONT
ARenderingHints.Key
which allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their ownGraphics2D
object, the value of this key will be thePDFFont
that is currently in use- Since:
- 2.17.1
-
KEY_SHAPETEXT_STRING
public static final java.awt.RenderingHints.Key KEY_SHAPETEXT_STRING
ARenderingHints.Key
which allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their ownGraphics2D
object, the value of this key will be the text that the next graphics operations represent, ornull
if future graphics operations are not part of a glyph. Note that the PDF and AWT rendering models have differences, so this is not necessarily exact - for example, setting the clipping path to the outline of a glyph is classed as text- Since:
- 2.17.1
-
-
Method Detail
-
setBox
public void setBox(java.lang.String boxname)
Set the default "box" to paint. By default this is the CropBox if specified, otherwise the MediaBox will be used. Setting this to "TrimBox", "ArtBox", "MediaBox" or any of the other boxes from thePDFPage.getBox(java.lang.String)
method will change this behaviour. If the requested box is not set for the page, it will fall back to the default behaviour.- Parameters:
boxname
- the name of the page box to render by default - "CropBox", "MediaBox", "ArtBox" or "TrimBox"- Since:
- 2.11.22
-
setBackground
public void setBackground(java.awt.Paint background)
Set the background of any images drawn with thisPagePainter
. By default pages are drawn onto an opaque white background, but this method may be used to cause PDFs to be drawn onto a different background. A value ofnull
will cause the page to be drawn onto a transparent background.- Parameters:
background
- The background paint to draw the page on to. May benull
.- Since:
- 2.5.2
-
setPaintAnnotations
public void setPaintAnnotations(boolean paintannotations)
Set whether any annotations on this page are to be painted or not. The default is true.- Since:
- 2.8
-
getPage
public PDFPage getPage()
Return the PDFPage being painted by this object- Since:
- 2.11.7
-
setPageExtractor
public void setPageExtractor(PageExtractor extractor)
Set thePageExtractor
to extract text to. Setting an extractor in this way causes the page to be painted and the text to be extracted in the same paint run, rather than having to perform two different runs. It's intended for use when a viewing application is going to highlight or select text- Since:
- 2.8
-
setRenderingHints
public void setRenderingHints(java.awt.RenderingHints hints)
Set additional rendering hints on the Graphics object. When rendering a PDF to a
BufferedImage
using one of the variousgetImage(float)
methods, this method can be called first to control the subtleties of the rendering process. Any hints that are set in the suppliedhints
variable are merged with the default hints of theGraphics2D
the PDF is drawn to.For example, if you're quite sure you never want text drawn as shapes, you could do something like the following:
RenderingHints hints = new RenderingHints(PagePainter.KEY_SHAPETEXT, PagePainter.VALUES_SHAPETEXT_OFF); pagepainter.setRenderingHints(hints); BufferedImage image = pagepainter.getImage();
- Since:
- 2.6.3
- See Also:
Graphics2D.setRenderingHints(java.util.Map<?, ?>)
,PDFParser.writeAsTIFF(OutputStream,int,ColorModel,RenderingHints)
,KEY_SHAPETEXT
,KEY_COLORCONVERTRGB
-
getImage
public java.awt.image.BufferedImage getImage(float dpi)
Draw the area inside theViewBox
if specified, or the whole page if not to aBufferedImage
in the standard sRGB ColorModel.- Parameters:
dpi
- how many dots per inch to draw the image. A value of 72 gives in 1 point per pixel- Returns:
- a
BufferedImage
that iswidth*dpi/72
pixels wide andheight*dpi/72
pixels high, in the ColorModel returned byColorModel.getRGBdefault()
-
getImage
public java.awt.image.BufferedImage getImage(float dpi, java.awt.image.ColorModel cm)
Draw the area inside theViewBox
if specified, or the whole page if not to aBufferedImage
in the specified ColorModel.- Parameters:
dpi
- how many dots per inch to draw the image. A value of 72 gives 1 point per pixelcm
- theColorModel
to use to create the image with- Returns:
- a
BufferedImage
that iswidth*dpi/72
pixels wide andheight*dpi/72
pixels high using the specifiedColorModel
- See Also:
PDFParser.BLACKANDWHITE
,PDFParser.GRAYSCALE
,PDFParser.RGB
,PDFParser.RGBA
,PDFParser.CMYK
-
getSubImage
public java.awt.image.BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi)
Draw a section of the page to aBufferedImage
in the standard sRGB ColorModel- Parameters:
x1
- the left-most X co-ordinate of the area of the page to draw, in pointsy1
- the bottom-most Y co-ordinate of the area of the page to draw, in pointsx2
- the right-most X co-ordinate of the area of the page to draw, in pointsy2
- the top-most Y co-ordinate of the area of the page to draw, in pointsdpi
- how many dots per inch to draw the image. A value of 72 gives in 1 point per pixel- Returns:
- a
BufferedImage
that is(x2-x1)*dpi/72
pixels wide and(y2-y1)*dpi/72
pixels high, in the ColorModel returned byColorModel.getRGBdefault()
-
getSubImage
public java.awt.image.BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi, java.awt.image.ColorModel cm)
Draw a section of the page to aBufferedImage
using the specified ColorModel- Parameters:
x1
- the left-most X co-ordinate of the area of the page to draw, in pointsy1
- the bottom-most Y co-ordinate of the area of the page to draw, in pointsx2
- the right-most X co-ordinate of the area of the page to draw, in pointsy2
- the top-most Y co-ordinate of the area of the page to draw, in pointsdpi
- how many dots per inch to draw the image. A value of 72 gives in 1 point per pixelcm
- theColorModel
to use to create the image with- Returns:
- a
BufferedImage
that is(x2-x1)*dpi/72
pixels wide and(y2-y1)*dpi/72
pixels high in the specifiedColorModel
, ornull
if this thread was interrupted before the image could be completed.
-
drawSubImage
public boolean drawSubImage(java.awt.Graphics2D graphics, double x1, double y1, double x2, double y2, float dpi)
Draw an area of a page onto aGraphics2D
object. If thesetRenderingHints(java.awt.RenderingHints)
method has been called, those hints will override the equivalent hints on the suppliedgraphics
object.- Parameters:
x1
- the left-most X co-ordinate of the area of the page to draw, in pointsy1
- the top-most Y co-ordinate of the area of the page to draw, in pointsx2
- the right-most X co-ordinate of the area of the page to draw, in pointsy2
- the bottom-most Y co-ordinate of the area of the page to draw, in pointsdpi
- since 2.16 this parameter is ignored - the DPI is derived from the transform on the supplied Graphics2D- Returns:
- true if the image rendering process completed, false otherwise
-
print
public int print(java.awt.Graphics g, java.awt.print.PageFormat format, int pagenum)
- Specified by:
print
in interfacejava.awt.print.Printable
-
paintAnnotation
public boolean paintAnnotation(PDFAnnotation annotation, java.lang.String state, java.awt.Graphics2D graphics, float[] rect)
Paint a single annotation.- Parameters:
annotation
- the Annotationstate
- the state to paint - "N" for normal, "D" for down or "R" for rollovergraphics
- the Graphics object to paint torect
- the position on the Graphics object - either annotation.getRectangle() or something else.- Returns:
- true if the annotation painting completed, false if this thread was interrupted or no stream exists for that state
- Since:
- 2.8
-
getAnnotationComposite
public java.awt.Composite getAnnotationComposite()
Return theComposite
that should be used to place the annotation painted bypaintAnnotation()
onto the main page. Do not rely on this method, it's subject to change.- Since:
- 2.11.5
-
interrupt
public void interrupt()
Interrupts and stops the current painting operation. This method would typically be called in interactive applications that render the PDF in one thread, while allowing user interaction in another. If that rendering process needs to be stopped for any reason (the user changes the zoom level, for example), this method must be called - it will cause thegetImage
method to exit early. If the painting thread is simply killed whileisPainting()
is still running, resources that are shared across the PDF may be left in an undefined state, which would cause any future page draws to fail.
-
isPainting
public boolean isPainting()
Returns true if the current painting operation is still in progress. If it is, theinterrupt
method must be called to stop it before the painting thread is killed.- Returns:
- true if this object is still rendering the PDF
-
-