org.apache.pdfbox.rendering

## Class PageDrawer

- java.lang.Object
- - org.apache.pdfbox.contentstream.PDFStreamEngine
  - - org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine
    - - org.apache.pdfbox.rendering.PageDrawer

    public class PageDrawer
    extends PDFGraphicsStreamEngine

Paints a page in a PDF document to a Graphics context. May be subclassed to provide custom rendering.

If you want to do custom graphics processing rather than Graphics2D rendering, then you should subclass `PDFGraphicsStreamEngine` instead. Subclassing PageDrawer is only suitable for cases where the goal is to render onto a `Graphics2D` surface. In that case you'll also have to subclass `PDFRenderer` and override `PDFRenderer.createPageDrawer(PageDrawerParameters)`. See the OpaquePDFRenderer.java example in the source code download on how to do this.

Author:
  Ben Litchfield

### Constructor Summary

Constructors
Constructor and Description
`PageDrawer(PageDrawerParameters parameters)` Constructor.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3)` Append a rectangle to the current path.
`void` `beginMarkedContentSequence(COSName tag, COSDictionary properties)` Called when a marked content group begins
`void` `beginText()` Called when the BT operator is encountered.
`void` `clip(int windingRule)` Modify the current clipping path by intersecting it with the current path.
`void` `closePath()` Closes the current path.
`void` `curveTo(float x1, float y1, float x2, float y2, float x3, float y3)` Draws a curve from the current point to (x3,y3) using (x1,y1) and (x2,y2) as control points.
`void` `drawImage(PDImage pdImage)` Draw the image.
`void` `drawPage(Graphics2D g, PDRectangle pageSize)` Draws the page to the requested context.
`void` `endMarkedContentSequence()` Called when a marked content group ends
`void` `endPath()` Ends the current path without filling or stroking it.
`void` `endText()` Called when the ET operator is encountered.
`void` `fillAndStrokePath(int windingRule)` Fills and then strokes the path.
`void` `fillPath(int windingRule)` Fill the path.
`AnnotationFilter` `getAnnotationFilter()` Return the AnnotationFilter.
`Point2D` `getCurrentPoint()` Returns the current point of the current path.
`protected Graphics2D` `getGraphics()` Returns the underlying Graphics2D.
`protected GeneralPath` `getLinePath()` Returns the current line path.
`protected Paint` `getNonStrokingPaint()` Returns the non-stroking AWT Paint.
`protected Paint` `getPaint(PDColor color)` Returns an AWT paint for the given PDColor.
`PDFRenderer` `getRenderer()` Returns the parent renderer.
`protected int` `getSubsampling(PDImage pdImage, AffineTransform at)` Calculates the subsampling frequency for a given PDImage based on the current transformation and its calculated transform.
`void` `lineTo(float x, float y)` Draws a line from the current point to (x,y).
`void` `moveTo(float x, float y)` Starts a new path at (x,y).
`void` `setAnnotationFilter(AnnotationFilter annotationFilter)` Set the AnnotationFilter.
`protected void` `setClip()` Sets the clipping path using caching for performance.
`void` `shadingFill(COSName shadingName)` Fill with Shading.
`void` `showAnnotation(PDAnnotation annotation)` Shows the given annotation.
`protected void` `showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)` Called when a glyph is to be processed.
`void` `showForm(PDFormXObject form)` Shows a form from the content stream.
`void` `showTransparencyGroup(PDTransparencyGroup form)` Shows a transparency group from the content stream.
`protected void` `showTransparencyGroupOnGraphics(PDTransparencyGroup form, Graphics2D graphics)` For advanced users, to extract the transparency group into a separate graphics device.
`protected void` `showType3Glyph(Matrix textRenderingMatrix, PDType3Font font, int code, Vector displacement)` Called when a glyph is to be processed.
`void` `strokePath()` Stroke the path.
`protected void` `transferClip(Graphics2D graphics)` Transfer clip to the destination device.

### Methods inherited from class org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine

`getPage`

### Methods inherited from class org.apache.pdfbox.contentstream.PDFStreamEngine

`addOperator, applyTextAdjustment, decreaseLevel, getAppearance, getCurrentPage, getGraphicsStackSize, getGraphicsState, getInitialMatrix, getLevel, getResources, getTextLineMatrix, getTextMatrix, increaseLevel, isShouldProcessColorOperators, markedContentPoint, operatorException, processAnnotation, processChildStream, processOperator, processOperator, processPage, processSoftMask, processTilingPattern, processTilingPattern, processTransparencyGroup, processType3Stream, restoreGraphicsStack, restoreGraphicsState, saveGraphicsStack, saveGraphicsState, setLineDashPattern, setTextLineMatrix, setTextMatrix, showGlyph, showText, showTextString, showTextStrings, transformedPoint, transformWidth, unsupportedOperator`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### PageDrawer

    public PageDrawer(PageDrawerParameters parameters)
               throws IOException

Constructor.

Parameters:
  `parameters` - Parameters for page drawing.
Throws:
  `IOException` - If there is an error loading properties from the file.

### Method Detail

#### getAnnotationFilter

    public AnnotationFilter getAnnotationFilter()

Return the AnnotationFilter.

Returns:
  the AnnotationFilter

#### setAnnotationFilter

    public void setAnnotationFilter(AnnotationFilter annotationFilter)

Set the AnnotationFilter.

Allows to only render annotation accepted by the filter.

Parameters:
  `annotationFilter` - the AnnotationFilter

#### getRenderer

    public final PDFRenderer getRenderer()

Returns the parent renderer.

Returns:
  the parent renderer

#### getGraphics

    protected final Graphics2D getGraphics()

Returns the underlying Graphics2D. May be null if drawPage has not yet been called.

Returns:
  the underlying Graphics2D

#### getLinePath

    protected final GeneralPath getLinePath()

Returns the current line path. This is reset to empty after each fill/stroke.

Returns:
  the current line path

#### drawPage

    public void drawPage(Graphics2D g,
                         PDRectangle pageSize)
                  throws IOException

Draws the page to the requested context.

Parameters:
  `g` - The graphics context to draw onto.
  `pageSize` - The size of the page to draw.
Throws:
  `IOException` - If there is an IO error while drawing the page.

#### getPaint

    protected Paint getPaint(PDColor color)
                      throws IOException

Returns an AWT paint for the given PDColor.

Parameters:
  `color` - The color to get a paint for. This can be an actual color or a pattern.
Returns:
  an AWT paint for the given PDColor
Throws:
  `IOException` - if the AWT paint could not be created

#### setClip

    protected final void setClip()

Sets the clipping path using caching for performance. We track lastClip manually because `Graphics.getClip()` returns a new object instead of the same one passed to `setClip()`. You may need to call this if you override `showGlyph()`. See PDFBOX-5093 for more.

#### transferClip

    protected void transferClip(Graphics2D graphics)

Transfer clip to the destination device. Override this if you want to avoid to do slow intersecting operations but want the destination device to do this (e.g. SVG). You can get the individual clippings via `PDGraphicsState.getCurrentClippingPaths()`. See PDFBOX-5258 for sample code.

Parameters:
  `graphics` - graphics device

#### beginText

    public void beginText()
                   throws IOException

Description copied from class: `PDFStreamEngine`
Called when the BT operator is encountered. This method is for overriding in subclasses, the default implementation does nothing.

Overrides:
  `beginText` in class `PDFStreamEngine`
Throws:
  `IOException` - if there was an error processing the text

#### endText

    public void endText()
                 throws IOException

Description copied from class: `PDFStreamEngine`
Called when the ET operator is encountered. This method is for overriding in subclasses, the default implementation does nothing.

Overrides:
  `endText` in class `PDFStreamEngine`
Throws:
  `IOException` - if there was an error processing the text

#### showFontGlyph

    protected void showFontGlyph(Matrix textRenderingMatrix,
                                 PDFont font,
                                 int code,
                                 Vector displacement)
                          throws IOException

Description copied from class: `PDFStreamEngine`
Called when a glyph is to be processed. This method is intended for overriding in subclasses, the default implementation does nothing.

Overrides:
  `showFontGlyph` in class `PDFStreamEngine`
Parameters:
  `textRenderingMatrix` - the current text rendering matrix, Trm
  `font` - the current font
  `code` - internal PDF character code for the glyph
  `displacement` - the displacement (i.e. advance) of the glyph in text space
Throws:
  `IOException` - if the glyph cannot be processed

#### showType3Glyph

    protected void showType3Glyph(Matrix textRenderingMatrix,
                                  PDType3Font font,
                                  int code,
                                  Vector displacement)
                           throws IOException

Description copied from class: `PDFStreamEngine`
Called when a glyph is to be processed. This method is intended for overriding in subclasses, the default implementation does nothing.

Overrides:
  `showType3Glyph` in class `PDFStreamEngine`
Parameters:
  `textRenderingMatrix` - the current text rendering matrix, Trm
  `font` - the current font
  `code` - internal PDF character code for the glyph
  `displacement` - the displacement (i.e. advance) of the glyph in text space
Throws:
  `IOException` - if the glyph cannot be processed

#### appendRectangle

    public void appendRectangle(Point2D p0,
                                Point2D p1,
                                Point2D p2,
                                Point2D p3)

Description copied from class: `PDFGraphicsStreamEngine`
Append a rectangle to the current path.

Specified by:
  `appendRectangle` in class `PDFGraphicsStreamEngine`
Parameters:
  `p0` - starting coordinate of the rectangle
  `p1` - second coordinate of the rectangle
  `p2` - third coordinate of the rectangle
  `p3` - last coordinate of the rectangle

#### getNonStrokingPaint

    protected final Paint getNonStrokingPaint()
                                       throws IOException

Returns the non-stroking AWT Paint. You may need to call this if you override `showGlyph()`. See PDFBOX-5093 for more.

Returns:
  The non-stroking AWT Paint.
Throws:
  `IOException` - if the non-stroking AWT Paint could not be created

#### strokePath

    public void strokePath()
                    throws IOException

Description copied from class: `PDFGraphicsStreamEngine`
Stroke the path.

Specified by:
  `strokePath` in class `PDFGraphicsStreamEngine`
Throws:
  `IOException` - If there is an IO error while stroking the path.

#### fillPath

    public void fillPath(int windingRule)
                  throws IOException

Description copied from class: `PDFGraphicsStreamEngine`
Fill the path.

Specified by:
  `fillPath` in class `PDFGraphicsStreamEngine`
Parameters:
  `windingRule` - The winding rule this path will use.
Throws:
  `IOException` - if the path could not be filled

#### fillAndStrokePath

    public void fillAndStrokePath(int windingRule)
                           throws IOException

Fills and then strokes the path.

Specified by:
  `fillAndStrokePath` in class `PDFGraphicsStreamEngine`
Parameters:
  `windingRule` - The winding rule this path will use.
Throws:
  `IOException` - If there is an IO error while filling the path.

#### clip

    public void clip(int windingRule)

Description copied from class: `PDFGraphicsStreamEngine`
Modify the current clipping path by intersecting it with the current path. The clipping path will not be updated until the succeeding painting operator is called.

Specified by:
  `clip` in class `PDFGraphicsStreamEngine`
Parameters:
  `windingRule` - The winding rule which will be used for clipping.

#### moveTo

    public void moveTo(float x,
                       float y)

Description copied from class: `PDFGraphicsStreamEngine`
Starts a new path at (x,y).

Specified by:
  `moveTo` in class `PDFGraphicsStreamEngine`
Parameters:
  `x` - the x-coordinate to move to
  `y` - the y-coordinate to move to

#### lineTo

    public void lineTo(float x,
                       float y)

Description copied from class: `PDFGraphicsStreamEngine`
Draws a line from the current point to (x,y).

Specified by:
  `lineTo` in class `PDFGraphicsStreamEngine`
Parameters:
  `x` - the X-coordinate of the ending-point of the line to be drawn
  `y` - the Y-coordinate of the ending-point of the line to be drawn

#### curveTo

    public void curveTo(float x1,
                        float y1,
                        float x2,
                        float y2,
                        float x3,
                        float y3)

Description copied from class: `PDFGraphicsStreamEngine`
Draws a curve from the current point to (x3,y3) using (x1,y1) and (x2,y2) as control points.

Specified by:
  `curveTo` in class `PDFGraphicsStreamEngine`
Parameters:
  `x1` - the X coordinate of the first Bézier control point
  `y1` - the Y coordinate of the first Bézier control point
  `x2` - the X coordinate of the second Bézier control point
  `y2` - the Y coordinate of the second Bézier control point
  `x3` - the X coordinate of the final end point
  `y3` - the Y coordinate of the final end point

#### getCurrentPoint

    public Point2D getCurrentPoint()

Description copied from class: `PDFGraphicsStreamEngine`
Returns the current point of the current path.

Specified by:
  `getCurrentPoint` in class `PDFGraphicsStreamEngine`
Returns:
  the current point or null

#### closePath

    public void closePath()

Description copied from class: `PDFGraphicsStreamEngine`
Closes the current path.

Specified by:
  `closePath` in class `PDFGraphicsStreamEngine`

#### endPath

    public void endPath()

Description copied from class: `PDFGraphicsStreamEngine`
Ends the current path without filling or stroking it. The clipping path is updated here.

Specified by:
  `endPath` in class `PDFGraphicsStreamEngine`

#### drawImage

    public void drawImage(PDImage pdImage)
                   throws IOException

Description copied from class: `PDFGraphicsStreamEngine`
Draw the image.

Specified by:
  `drawImage` in class `PDFGraphicsStreamEngine`
Parameters:
  `pdImage` - The image to draw.
Throws:
  `IOException` - if the image could not be drawn

#### getSubsampling

    protected int getSubsampling(PDImage pdImage,
                                 AffineTransform at)

Calculates the subsampling frequency for a given PDImage based on the current transformation and its calculated transform. Extend this method if you want to use your own strategy.

Parameters:
  `pdImage` - PDImage to be drawn
  `at` - Transform that will be applied to the image when drawing
Returns:
  The rounded-down ratio of image pixels to drawn pixels. Returned value will always be >=1.

#### shadingFill

    public void shadingFill(COSName shadingName)
                     throws IOException

Description copied from class: `PDFGraphicsStreamEngine`
Fill with Shading.

Specified by:
  `shadingFill` in class `PDFGraphicsStreamEngine`
Parameters:
  `shadingName` - The name of the Shading Dictionary to use for this fill instruction.
Throws:
  `IOException` - if the path could not be filled using the given shading

#### showAnnotation

    public void showAnnotation(PDAnnotation annotation)
                        throws IOException

Description copied from class: `PDFStreamEngine`
Shows the given annotation.

Overrides:
  `showAnnotation` in class `PDFStreamEngine`
Parameters:
  `annotation` - An annotation on the current page.
Throws:
  `IOException` - If an error occurred reading the annotation

#### showForm

    public void showForm(PDFormXObject form)
                  throws IOException

Shows a form from the content stream.

Overrides:
  `showForm` in class `PDFStreamEngine`
Parameters:
  `form` - form XObject
Throws:
  `IOException` - if the form cannot be processed

#### showTransparencyGroup

    public void showTransparencyGroup(PDTransparencyGroup form)
                               throws IOException

Description copied from class: `PDFStreamEngine`
Shows a transparency group from the content stream.

Overrides:
  `showTransparencyGroup` in class `PDFStreamEngine`
Parameters:
  `form` - transparency group (form) XObject
Throws:
  `IOException` - if the transparency group cannot be processed

#### showTransparencyGroupOnGraphics

    protected void showTransparencyGroupOnGraphics(PDTransparencyGroup form,
                                                   Graphics2D graphics)
                                            throws IOException

For advanced users, to extract the transparency group into a separate graphics device.

Parameters:
  `form` - the transparency group to be extracted
  `graphics` - the target graphics device
Throws:
  `IOException` - if the transparency group could not be extracted

#### beginMarkedContentSequence

    public void beginMarkedContentSequence(COSName tag,
                                           COSDictionary properties)

Called when a marked content group begins

Overrides:
  `beginMarkedContentSequence` in class `PDFStreamEngine`
Parameters:
  `tag` - indicates the role or significance of the sequence
  `properties` - optional properties

#### endMarkedContentSequence

    public void endMarkedContentSequence()

Called when a marked content group ends

Overrides:
  `endMarkedContentSequence` in class `PDFStreamEngine`