Class PagePainter

  • All Implemented Interfaces:
    Printable

    public class PagePainter
    extends Object
    implements Printable
    The PagePainter class renders a page of a PDF to a Graphics2D 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 so
     PDFParser parser = new PDFParser(pdf);
     PagePainter painter = parser.getPagePainter(0);
     BufferedImage image = painter.getImage(100);
     
    As it implements the Printable 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 Detail

      • VALUE_COLORCONVERTRGB_DEFAULT

        public static final Object VALUE_COLORCONVERTRGB_DEFAULT
        A Rendering Hint value for KEY_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 Object VALUE_COLORCONVERTRGB_ON
        A Rendering Hint value for KEY_COLORCONVERTRGB which 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 for KEY_COLORCONVERTRGB which causes the rendering process to never convert colors to RGB.
        Since:
        2.6.3
      • KEY_SHAPETEXT

        public static final RenderingHints.Key KEY_SHAPETEXT
        A RenderingHints.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 the VALUE_SHAPETEXT_DEFAULT value 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 for KEY_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 Object VALUE_SHAPETEXT_ON
        A Rendering Hint value for KEY_SHAPETEXT which 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 for KEY_SHAPETEXT which causes the rendering process to never draw text as shapes.
        Since:
        2.6.3
      • KEY_SOFTCLIP

        public static final RenderingHints.Key KEY_SOFTCLIP
        A RenderingHints.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 the VALUE_SOFTCLIP_DEFAULT value does.
        Since:
        2.19.1
      • VALUE_SOFTCLIP_ON

        public static final Object VALUE_SOFTCLIP_ON
        A Rendering Hint value for KEY_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 Object VALUE_SOFTCLIP_OFF
        A Rendering Hint value for KEY_SOFTCLIP which 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 for KEY_SOFTCLIP which 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
        A RenderingHints.Key which allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their own Graphics2D object, the value of this key will be the PDFFont that is currently in use
        Since:
        2.17.1
      • KEY_SHAPETEXT_STRING

        public static final RenderingHints.Key KEY_SHAPETEXT_STRING
        A RenderingHints.Key which allow text that is rendered as a shape to be assocated with a glyph. Primarily of use to those implementing their own Graphics2D object, the value of this key will be the text that the next graphics operations represent, or null 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​(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 the PDFPage.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 this PagePainter. 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 of null will cause the page to be drawn onto a transparent background.
        Parameters:
        background - The background paint to draw the page on to. May be null.
        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 the PageExtractor 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
      • getImage

        public BufferedImage getImage​(float dpi)
        Draw the area inside the ViewBox if specified, or the whole page if not to a BufferedImage 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 is width*dpi/72 pixels wide and height*dpi/72 pixels high, in the ColorModel returned by ColorModel.getRGBdefault()
      • getSubImage

        public BufferedImage getSubImage​(double x1,
                                         double y1,
                                         double x2,
                                         double y2,
                                         float dpi)
        Draw a section of the page to a BufferedImage in the standard sRGB ColorModel
        Parameters:
        x1 - the left-most X co-ordinate of the area of the page to draw, in points
        y1 - the bottom-most Y co-ordinate of the area of the page to draw, in points
        x2 - the right-most X co-ordinate of the area of the page to draw, in points
        y2 - the top-most Y co-ordinate of the area of the page to draw, in points
        dpi - 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 by ColorModel.getRGBdefault()
      • getSubImage

        public BufferedImage getSubImage​(double x1,
                                         double y1,
                                         double x2,
                                         double y2,
                                         float dpi,
                                         ColorModel cm)
        Draw a section of the page to a BufferedImage using the specified ColorModel
        Parameters:
        x1 - the left-most X co-ordinate of the area of the page to draw, in points
        y1 - the bottom-most Y co-ordinate of the area of the page to draw, in points
        x2 - the right-most X co-ordinate of the area of the page to draw, in points
        y2 - the top-most Y co-ordinate of the area of the page to draw, in points
        dpi - how many dots per inch to draw the image. A value of 72 gives in 1 point per pixel
        cm - the ColorModel 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 specified ColorModel, or null if 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 a Graphics2D object. If the setRenderingHints(java.awt.RenderingHints) method has been called, those hints will override the equivalent hints on the supplied graphics object.
        Parameters:
        x1 - the left-most X co-ordinate of the area of the page to draw, in points
        y1 - the top-most Y co-ordinate of the area of the page to draw, in points
        x2 - the right-most X co-ordinate of the area of the page to draw, in points
        y2 - the bottom-most Y co-ordinate of the area of the page to draw, in points
        dpi - 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
      • paintAnnotation

        public boolean paintAnnotation​(PDFAnnotation annotation,
                                       String state,
                                       Graphics2D graphics,
                                       float[] rect)
        Paint a single annotation.
        Parameters:
        annotation - the Annotation
        state - the state to paint - "N" for normal, "D" for down or "R" for rollover
        graphics - the Graphics object to paint to
        rect - 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 the Composite that should be used to place the annotation painted by paintAnnotation() 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 the getImage method to exit early. If the painting thread is simply killed while isPainting() 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, the interrupt method must be called to stop it before the painting thread is killed.
        Returns:
        true if this object is still rendering the PDF