Top Description Fields Constructors Methods
org.apache.pdfbox.rendering

public Class PDFRenderer

extends Object
Class Inheritance
Imports
java.awt.Color, .DisplayMode, .Graphics2D, .GraphicsConfiguration, .GraphicsDevice, .RenderingHints, java.awt.image.BufferedImage, java.io.IOException, org.apache.pdfbox.cos.COSName, org.apache.pdfbox.pdmodel.PDDocument, .PDPage, .PDPageTree, .PDResources, org.apache.pdfbox.pdmodel.common.PDRectangle, org.apache.pdfbox.pdmodel.graphics.blend.BlendMode, org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup, .PDOptionalContentProperties, org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState, org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter

Renders a PDF document to an AWT BufferedImage. This class may be overridden in order to perform custom rendering.
Author
John Hewson

Field Summary

Modifier and TypeField and Description
private AnnotationFilter
annotationFilter

Default annotations filter, returns all annotations

private RenderDestination
protected final PDDocument
private float
private BufferedImage
private final PDPageTree
private RenderingHints
private boolean

Constructor Summary

AccessConstructor and Description
public
PDFRenderer(PDDocument
the document to render
document
)

Creates a new PDFRenderer.

Method Summary

Modifier and TypeMethod and Description
private RenderingHints
protected PageDrawer

Returns:

a new PageDrawer instance
createPageDrawer
(PageDrawerParameters
parameters to be used when creating the PageDrawer instance
parameters
)

Returns a new PageDrawer instance, using the given parameters.

public AnnotationFilter

Returns:

the AnnotationFilter
getAnnotationsFilter
()

Return the AnnotationFilter.

public RenderDestination

Returns:

the defaultDestination
getDefaultDestination
()

public float

Returns:

get the image downscaling optimization threshold. See getImageDownscalingOptimizationThreshold() for details.
getImageDownscalingOptimizationThreshold
()

pack-priv BufferedImage
getPageImage()

Returns the image to which the current page is being rendered.

public RenderingHints

Returns:

the rendering hints or null if none are set.
getRenderingHints
()

Get the rendering hints.

private boolean
private boolean
public boolean

Returns:

true if the group is enabled
isGroupEnabled
(PDOptionalContentGroup
the group
group
)

Indicates whether an optional content group is enabled.

public boolean

Returns:

true if subsampling of images is allowed, false otherwise.
isSubsamplingAllowed
()

Value indicating if the renderer is allowed to subsample images before drawing, according to image dimensions and requested scale.

public BufferedImage

Returns:

the rendered page image
renderImage
(int
the zero-based index of the page to be converted.
pageIndex
)

Returns the given page as an RGB image at 72 DPI

public BufferedImage

Returns:

the rendered page image
renderImage
(int
the zero-based index of the page to be converted
pageIndex
,
float
the scaling factor, where 1 = 72 DPI
scale
)

Returns the given page as an RGB image at the given scale.

public BufferedImage

Returns:

the rendered page image
renderImage
(int
the zero-based index of the page to be converted
pageIndex
,
float
the scaling factor, where 1 = 72 DPI
scale
,
ImageType
the type of image to return
imageType
)

Returns the given page as an RGB or ARGB image at the given scale.

public BufferedImage

Returns:

the rendered page image
renderImage
(int
the zero-based index of the page to be converted
pageIndex
,
float
the scaling factor, where 1 = 72 DPI
scale
,
ImageType
the type of image to return
imageType
,
RenderDestination
controlling visibility of optional content groups
destination
)

Returns the given page as an RGB or ARGB image at the given scale.

public BufferedImage

Returns:

the rendered page image
renderImageWithDPI
(int
the zero-based index of the page to be converted
pageIndex
,
float
the DPI (dots per inch) to render at
dpi
)

Returns the given page as an RGB image at the given DPI.

public BufferedImage

Returns:

the rendered page image
renderImageWithDPI
(int
the zero-based index of the page to be converted
pageIndex
,
float
the DPI (dots per inch) to render at
dpi
,
ImageType
the type of image to return
imageType
)

Returns the given page as an RGB image at the given DPI.

public void
renderPageToGraphics(int
the zero-based index of the page to be converted
pageIndex
,
Graphics2D
the Graphics2D on which to draw the page
graphics
)

Renders a given page to an AWT Graphics2D instance at 72 DPI.

public void
renderPageToGraphics(int
the zero-based index of the page to be converted
pageIndex
,
Graphics2D
the Graphics2D on which to draw the page
graphics
,
float
the scaling factor, where 1 = 72 DPI
scale
)

Renders a given page to an AWT Graphics2D instance.

public void
renderPageToGraphics(int
the zero-based index of the page to be converted
pageIndex
,
Graphics2D
the Graphics2D on which to draw the page
graphics
,
float
the scale to draw the page at for the x-axis, where 1 = 72 DPI
scaleX
,
float
the scale to draw the page at for the y-axis, where 1 = 72 DPI
scaleY
)

Renders a given page to an AWT Graphics2D instance.

public void
renderPageToGraphics(int
the zero-based index of the page to be converted
pageIndex
,
Graphics2D
the Graphics2D on which to draw the page
graphics
,
float
the scale to draw the page at for the x-axis, where 1 = 72 DPI
scaleX
,
float
the scale to draw the page at for the y-axis, where 1 = 72 DPI
scaleY
,
RenderDestination
controlling visibility of optional content groups
destination
)

Renders a given page to an AWT Graphics2D instance.

public void
setAnnotationsFilter(AnnotationFilter
the AnnotationFilter
annotationsFilter
)

Set the AnnotationFilter.

public void
setDefaultDestination(RenderDestination
the defaultDestination to set
defaultDestination
)

public void
setImageDownscalingOptimizationThreshold(float
image downscaling optimization threshold
imageDownscalingOptimizationThreshold
)

Set the image downscaling optimization threshold.

public void
setRenderingHints(RenderingHints
rendering hints to be used to influence rendering quality and speed
renderingHints
)

Set the rendering hints.

public void
setSubsamplingAllowed(boolean
The new value indicating if subsampling is allowed.
subsamplingAllowed
)

Sets a value instructing the renderer whether it is allowed to subsample images before drawing.

private void
transform(Graphics2D graphics, int rotationAngle, PDRectangle cropBox, float scaleX, float scaleY)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

annotationFilterback to summary
private AnnotationFilter annotationFilter

Default annotations filter, returns all annotations

defaultDestinationback to summary
private RenderDestination defaultDestination
documentback to summary
protected final PDDocument document
imageDownscalingOptimizationThresholdback to summary
private float imageDownscalingOptimizationThreshold
pageImageback to summary
private BufferedImage pageImage
pageTreeback to summary
private final PDPageTree pageTree
renderingHintsback to summary
private RenderingHints renderingHints
subsamplingAllowedback to summary
private boolean subsamplingAllowed

Constructor Detail

PDFRendererback to summary
public PDFRenderer(PDDocument document)

Creates a new PDFRenderer.

Parameters
document:PDDocument

the document to render

Method Detail

createDefaultRenderingHintsback to summary
private RenderingHints createDefaultRenderingHints(Graphics2D graphics)
createPageDrawerback to summary
protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException

Returns a new PageDrawer instance, using the given parameters. May be overridden.

Parameters
parameters:PageDrawerParameters

parameters to be used when creating the PageDrawer instance

Returns:PageDrawer

a new PageDrawer instance

Exceptions
IOException:
id the PageDrawer instance could not be created
getAnnotationsFilterback to summary
public AnnotationFilter getAnnotationsFilter()

Return the AnnotationFilter.

Returns:AnnotationFilter

the AnnotationFilter

getDefaultDestinationback to summary
public RenderDestination getDefaultDestination()
Returns:RenderDestination

the defaultDestination

getImageDownscalingOptimizationThresholdback to summary
public float getImageDownscalingOptimizationThreshold()
Returns:float

get the image downscaling optimization threshold. See getImageDownscalingOptimizationThreshold() for details.

getPageImageback to summary
pack-priv BufferedImage getPageImage()

Returns the image to which the current page is being rendered. May be null if the page is rendered to a Graphics2D object instead of a BufferedImage.

getRenderingHintsback to summary
public RenderingHints getRenderingHints()

Get the rendering hints.

Returns:RenderingHints

the rendering hints or null if none are set.

hasBlendModeback to summary
private boolean hasBlendMode(PDPage page)
isBitonalback to summary
private boolean isBitonal(Graphics2D graphics)
isGroupEnabledback to summary
public boolean isGroupEnabled(PDOptionalContentGroup group)

Indicates whether an optional content group is enabled.

Parameters
group:PDOptionalContentGroup

the group

Returns:boolean

true if the group is enabled

isSubsamplingAllowedback to summary
public boolean isSubsamplingAllowed()

Value indicating if the renderer is allowed to subsample images before drawing, according to image dimensions and requested scale. Subsampling may be faster and less memory-intensive in some cases, but it may also lead to loss of quality, especially in images with high spatial frequency.

Returns:boolean

true if subsampling of images is allowed, false otherwise.

renderImageback to summary
public BufferedImage renderImage(int pageIndex) throws IOException

Returns the given page as an RGB image at 72 DPI

Parameters
pageIndex:int

the zero-based index of the page to be converted.

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderImageback to summary
public BufferedImage renderImage(int pageIndex, float scale) throws IOException

Returns the given page as an RGB image at the given scale. A scale of 1 will render at 72 DPI.

Parameters
pageIndex:int

the zero-based index of the page to be converted

scale:float

the scaling factor, where 1 = 72 DPI

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderImageback to summary
public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException

Returns the given page as an RGB or ARGB image at the given scale.

Parameters
pageIndex:int

the zero-based index of the page to be converted

scale:float

the scaling factor, where 1 = 72 DPI

imageType:ImageType

the type of image to return

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderImageback to summary
public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType, RenderDestination destination) throws IOException

Returns the given page as an RGB or ARGB image at the given scale.

Parameters
pageIndex:int

the zero-based index of the page to be converted

scale:float

the scaling factor, where 1 = 72 DPI

imageType:ImageType

the type of image to return

destination:RenderDestination

controlling visibility of optional content groups

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderImageWithDPIback to summary
public BufferedImage renderImageWithDPI(int pageIndex, float dpi) throws IOException

Returns the given page as an RGB image at the given DPI.

Parameters
pageIndex:int

the zero-based index of the page to be converted

dpi:float

the DPI (dots per inch) to render at

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderImageWithDPIback to summary
public BufferedImage renderImageWithDPI(int pageIndex, float dpi, ImageType imageType) throws IOException

Returns the given page as an RGB image at the given DPI.

Parameters
pageIndex:int

the zero-based index of the page to be converted

dpi:float

the DPI (dots per inch) to render at

imageType:ImageType

the type of image to return

Returns:BufferedImage

the rendered page image

Exceptions
IOException:
if the PDF cannot be read
renderPageToGraphicsback to summary
public void renderPageToGraphics(int pageIndex, Graphics2D graphics) throws IOException

Renders a given page to an AWT Graphics2D instance at 72 DPI.

Read renderPageToGraphics(int, Graphics2D, float, float, RenderDestination) before using this.

Parameters
pageIndex:int

the zero-based index of the page to be converted

graphics:Graphics2D

the Graphics2D on which to draw the page

Exceptions
IOException:
if the PDF cannot be read
renderPageToGraphicsback to summary
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scale) throws IOException

Renders a given page to an AWT Graphics2D instance.

Read renderPageToGraphics(int, Graphics2D, float, float, RenderDestination) before using this.

Parameters
pageIndex:int

the zero-based index of the page to be converted

graphics:Graphics2D

the Graphics2D on which to draw the page

scale:float

the scaling factor, where 1 = 72 DPI

Exceptions
IOException:
if the PDF cannot be read
renderPageToGraphicsback to summary
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY) throws IOException

Renders a given page to an AWT Graphics2D instance.

Read renderPageToGraphics(int, Graphics2D, float, float, RenderDestination) before using this.

Parameters
pageIndex:int

the zero-based index of the page to be converted

graphics:Graphics2D

the Graphics2D on which to draw the page

scaleX:float

the scale to draw the page at for the x-axis, where 1 = 72 DPI

scaleY:float

the scale to draw the page at for the y-axis, where 1 = 72 DPI

Exceptions
IOException:
if the PDF cannot be read
renderPageToGraphicsback to summary
public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scaleX, float scaleY, RenderDestination destination) throws IOException

Renders a given page to an AWT Graphics2D instance.

Known problems:

  • rendering of PDF files with transparencies is not supported on Ubuntu, see PDFBOX-4581 and JDK-6689349. Rendering will not abort, but the pages will be rendered incorrectly.
  • Clipping the Graphics2D will not work properly, see PDFBOX-4583.
If you encounter these problems, then you should render into an image by using the renderImage methods.
Parameters
pageIndex:int

the zero-based index of the page to be converted

graphics:Graphics2D

the Graphics2D on which to draw the page

scaleX:float

the scale to draw the page at for the x-axis, where 1 = 72 DPI

scaleY:float

the scale to draw the page at for the y-axis, where 1 = 72 DPI

destination:RenderDestination

controlling visibility of optional content groups

Exceptions
IOException:
if the PDF cannot be read
setAnnotationsFilterback to summary
public void setAnnotationsFilter(AnnotationFilter annotationsFilter)

Set the AnnotationFilter.

Allows to only render annotation accepted by the filter.

Parameters
annotationsFilter:AnnotationFilter

the AnnotationFilter

setDefaultDestinationback to summary
public void setDefaultDestination(RenderDestination defaultDestination)
Parameters
defaultDestination:RenderDestination

the defaultDestination to set

setImageDownscalingOptimizationThresholdback to summary
public void setImageDownscalingOptimizationThreshold(float imageDownscalingOptimizationThreshold)

Set the image downscaling optimization threshold. This must be a value between 0 and 1. When rendering downscaled images and rendering hints are set to bicubic+quality and the scaling is smaller than the threshold, a more quality-optimized but slower method will be used. The default is 0.5 which is a good compromise.

Parameters
imageDownscalingOptimizationThreshold:float

image downscaling optimization threshold

setRenderingHintsback to summary
public void setRenderingHints(RenderingHints renderingHints)

Set the rendering hints. Use this to influence rendering quality and speed. If you don't set them yourself or pass null, PDFBox will decide at runtime depending on the destination.

Parameters
renderingHints:RenderingHints

rendering hints to be used to influence rendering quality and speed

setSubsamplingAllowedback to summary
public void setSubsamplingAllowed(boolean subsamplingAllowed)

Sets a value instructing the renderer whether it is allowed to subsample images before drawing. The subsampling frequency is determined according to image size and requested scale. Subsampling may be faster and less memory-intensive in some cases, but it may also lead to loss of quality, especially in images with high spatial frequency.

Parameters
subsamplingAllowed:boolean

The new value indicating if subsampling is allowed.

transformback to summary
private void transform(Graphics2D graphics, int rotationAngle, PDRectangle cropBox, float scaleX, float scaleY)