Package org.faceless.pdf2
Class PagePainter
- java.lang.Object
-
- org.faceless.pdf2.PagePainter
-
- All Implemented Interfaces:
Printable
public class PagePainter extends Object implements Printable
The PagePainter class renders a page of a PDF to aGraphics2Dobject - 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 thePrintableinterface 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 RenderingHints.KeyKEY_COLORCONVERTRGBARenderingHints.Keywhich tells the rendering process to convert all colors to RGB.static RenderingHints.KeyKEY_SHAPETEXTARenderingHints.Keywhich determines whether text is rendered as a Shape.static RenderingHints.KeyKEY_SHAPETEXT_FONTARenderingHints.Keywhich allow text that is rendered as a shape to be assocated with a glyph.static RenderingHints.KeyKEY_SHAPETEXT_STRINGARenderingHints.Keywhich allow text that is rendered as a shape to be assocated with a glyph.static RenderingHints.KeyKEY_SOFTCLIPARenderingHints.Keywhich determines whether shapes are "soft-clipped" when rendering.static ObjectVALUE_COLORCONVERTRGB_DEFAULTA Rendering Hint value forKEY_COLORCONVERTRGBwhich causes the rendering process to convert colors to RGB only when it's deemed necessary.static ObjectVALUE_COLORCONVERTRGB_OFFA Rendering Hint value forKEY_COLORCONVERTRGBwhich causes the rendering process to never convert colors to RGB.static ObjectVALUE_COLORCONVERTRGB_ONA Rendering Hint value forKEY_COLORCONVERTRGBwhich causes the rendering process to always convert colors to RGB.static ObjectVALUE_SHAPETEXT_DEFAULTA Rendering Hint value forKEY_SHAPETEXTwhich causes the rendering process to never draw text as shapes.static ObjectVALUE_SHAPETEXT_OFFA Rendering Hint value forKEY_SHAPETEXTwhich causes the rendering process to draw text as shapes where we think the benefits outweigh the costs.static ObjectVALUE_SHAPETEXT_ONA Rendering Hint value forKEY_SHAPETEXTwhich causes the rendering process to always draw text as shapes.static ObjectVALUE_SOFTCLIP_DEFAULTA Rendering Hint value forKEY_SOFTCLIPwhich causes the rendering process to do what it feels is beststatic ObjectVALUE_SOFTCLIP_OFFA Rendering Hint value forKEY_SOFTCLIPwhich causes the rendering process to clip normally.static ObjectVALUE_SOFTCLIP_ONA Rendering Hint value forKEY_SOFTCLIPwhich causes the rendering process to use a moderately complex hack for clipping, to work around limitations in the AWT rendering model.-
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleandrawSubImage(Graphics2D graphics, double x1, double y1, double x2, double y2, float dpi)Draw an area of a page onto aGraphics2Dobject.CompositegetAnnotationComposite()Return theCompositethat should be used to place the annotation painted bypaintAnnotation()onto the main page.BufferedImagegetImage(float dpi)Draw the area inside theViewBoxif specified, or the whole page if not to aBufferedImagein the standard sRGB ColorModel.BufferedImagegetImage(float dpi, ColorModel cm)Draw the area inside theViewBoxif specified, or the whole page if not to aBufferedImagein the specified ColorModel.PDFPagegetPage()Return the PDFPage being painted by this objectBufferedImagegetSubImage(double x1, double y1, double x2, double y2, float dpi)Draw a section of the page to aBufferedImagein the standard sRGB ColorModelBufferedImagegetSubImage(double x1, double y1, double x2, double y2, float dpi, ColorModel cm)Draw a section of the page to aBufferedImageusing the specified ColorModelvoidinterrupt()Interrupts and stops the current painting operation.booleanisPainting()Returns true if the current painting operation is still in progress.booleanpaintAnnotation(PDFAnnotation annotation, String state, Graphics2D graphics, float[] rect)Paint a single annotation.intprint(Graphics g, PageFormat format, int pagenum)voidsetBackground(Paint background)Set the background of any images drawn with thisPagePainter.voidsetBox(String boxname)Set the default "box" to paint.voidsetPageExtractor(PageExtractor extractor)Set thePageExtractorto extract text to.voidsetPaintAnnotations(boolean paintannotations)Set whether any annotations on this page are to be painted or not.voidsetRenderingHints(RenderingHints hints)Set additional rendering hints on the Graphics object.
-
-
-
Field Detail
-
KEY_COLORCONVERTRGB
public static final RenderingHints.Key KEY_COLORCONVERTRGB
ARenderingHints.Keywhich tells the rendering process to convert all colors to RGB. This is required when printing due to bugs in thesun.printpackage up to Java 1.6.- Since:
- 2.6.3
- See Also:
setRenderingHints(java.awt.RenderingHints)
-
VALUE_COLORCONVERTRGB_DEFAULT
public static final Object VALUE_COLORCONVERTRGB_DEFAULT
A Rendering Hint value forKEY_COLORCONVERTRGBwhich 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 Object VALUE_COLORCONVERTRGB_ON
A Rendering Hint value forKEY_COLORCONVERTRGBwhich causes the rendering process to always convert colors to RGB.- Since:
- 2.6.3
-
VALUE_COLORCONVERTRGB_OFF
public static final Object VALUE_COLORCONVERTRGB_OFF
A Rendering Hint value forKEY_COLORCONVERTRGBwhich causes the rendering process to never convert colors to RGB.- Since:
- 2.6.3
-
KEY_SHAPETEXT
public static final RenderingHints.Key KEY_SHAPETEXT
ARenderingHints.Keywhich 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_DEFAULTvalue does.- Since:
- 2.6.3
- See Also:
setRenderingHints(java.awt.RenderingHints)
-
VALUE_SHAPETEXT_OFF
public static final Object VALUE_SHAPETEXT_OFF
A Rendering Hint value forKEY_SHAPETEXTwhich 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 Object VALUE_SHAPETEXT_ON
A Rendering Hint value forKEY_SHAPETEXTwhich causes the rendering process to always draw text as shapes.- Since:
- 2.6.3
-
VALUE_SHAPETEXT_DEFAULT
public static final Object VALUE_SHAPETEXT_DEFAULT
A Rendering Hint value forKEY_SHAPETEXTwhich causes the rendering process to never draw text as shapes.- Since:
- 2.6.3
-
KEY_SOFTCLIP
public static final RenderingHints.Key KEY_SOFTCLIP
ARenderingHints.Keywhich 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_DEFAULTvalue does.- Since:
- 2.19.1
-
VALUE_SOFTCLIP_ON
public static final Object VALUE_SOFTCLIP_ON
A Rendering Hint value forKEY_SOFTCLIPwhich 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 Object VALUE_SOFTCLIP_OFF
A Rendering Hint value forKEY_SOFTCLIPwhich causes the rendering process to clip normally.- Since:
- 2.19.1
-
VALUE_SOFTCLIP_DEFAULT
public static final Object VALUE_SOFTCLIP_DEFAULT
A Rendering Hint value forKEY_SOFTCLIPwhich causes the rendering process to do what it feels is best- Since:
- 2.19.1
-
KEY_SHAPETEXT_FONT
public static final RenderingHints.Key KEY_SHAPETEXT_FONT
ARenderingHints.Keywhich allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their ownGraphics2Dobject, the value of this key will be thePDFFontthat is currently in use- Since:
- 2.17.1
-
KEY_SHAPETEXT_STRING
public static final RenderingHints.Key KEY_SHAPETEXT_STRING
ARenderingHints.Keywhich allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their ownGraphics2Dobject, the value of this key will be the text that the next graphics operations represent, ornullif 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(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(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 ofnullwill 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 thePageExtractorto 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(RenderingHints hints)
Set additional rendering hints on the Graphics object. When rendering a PDF to a
BufferedImageusing 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 suppliedhintsvariable are merged with the default hints of theGraphics2Dthe 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();
-
getImage
public BufferedImage getImage(float dpi)
Draw the area inside theViewBoxif specified, or the whole page if not to aBufferedImagein 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
BufferedImagethat iswidth*dpi/72pixels wide andheight*dpi/72pixels high, in the ColorModel returned byColorModel.getRGBdefault()
-
getImage
public BufferedImage getImage(float dpi, ColorModel cm)
Draw the area inside theViewBoxif specified, or the whole page if not to aBufferedImagein the specified ColorModel.- Parameters:
dpi- how many dots per inch to draw the image. A value of 72 gives 1 point per pixelcm- theColorModelto use to create the image with- Returns:
- a
BufferedImagethat iswidth*dpi/72pixels wide andheight*dpi/72pixels high using the specifiedColorModel - See Also:
PDFParser.BLACKANDWHITE,PDFParser.GRAYSCALE,PDFParser.RGB,PDFParser.RGBA,PDFParser.CMYK
-
getSubImage
public BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi)
Draw a section of the page to aBufferedImagein 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
BufferedImagethat is(x2-x1)*dpi/72pixels wide and(y2-y1)*dpi/72pixels high, in the ColorModel returned byColorModel.getRGBdefault()
-
getSubImage
public BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi, ColorModel cm)
Draw a section of the page to aBufferedImageusing 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- theColorModelto use to create the image with- Returns:
- a
BufferedImagethat is(x2-x1)*dpi/72pixels wide and(y2-y1)*dpi/72pixels high in the specifiedColorModel, ornullif this thread was interrupted before the image could be completed.
-
drawSubImage
public boolean drawSubImage(Graphics2D graphics, double x1, double y1, double x2, double y2, float dpi)
Draw an area of a page onto aGraphics2Dobject. If thesetRenderingHints(java.awt.RenderingHints)method has been called, those hints will override the equivalent hints on the suppliedgraphicsobject.- 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(Graphics g, PageFormat format, int pagenum)
-
paintAnnotation
public boolean paintAnnotation(PDFAnnotation annotation, String state, 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 Composite getAnnotationComposite()
Return theCompositethat 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 thegetImagemethod 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, theinterruptmethod must be called to stop it before the painting thread is killed.- Returns:
- true if this object is still rendering the PDF
-
-