Package com.tom_roush.pdfbox.rendering
Class PDFRenderer
- java.lang.Object
-
- com.tom_roush.pdfbox.rendering.PDFRenderer
-
public class PDFRenderer extends Object
Renders a PDF document to a Bitmap. This class may be overridden in order to perform custom rendering.
-
-
Field Summary
Fields Modifier and Type Field Description protected PDDocumentdocument
-
Constructor Summary
Constructors Constructor Description PDFRenderer(PDDocument document)Creates a new PDFRenderer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected PageDrawercreatePageDrawer(PageDrawerParameters parameters)Returns a new PageDrawer instance, using the given parameters.AnnotationFiltergetAnnotationsFilter()Return the AnnotationFilter.RenderDestinationgetDefaultDestination()floatgetImageDownscalingOptimizationThreshold()booleanisGroupEnabled(PDOptionalContentGroup group)Indicates whether an optional content group is enabled.booleanisSubsamplingAllowed()Value indicating if the renderer is allowed to subsample images before drawing, according to image dimensions and requested scale.BitmaprenderImage(int pageIndex)Returns the given page as an RGB image at 72 DPIBitmaprenderImage(int pageIndex, float scale)Returns the given page as an RGB image at the given scale.BitmaprenderImage(int pageIndex, float scale, ImageType imageType)Returns the given page as an RGB or ARGB image at the given scale.BitmaprenderImage(int pageIndex, float scale, ImageType imageType, RenderDestination destination)Returns the given page as an RGB or ARGB image at the given scale.BitmaprenderImageWithDPI(int pageIndex, float dpi)Returns the given page as an RGB image at the given DPI.BitmaprenderImageWithDPI(int pageIndex, float dpi, ImageType imageType)Returns the given page as an RGB image at the given DPI.voidrenderPageToGraphics(int pageIndex, Paint paint, Canvas canvas)Renders a given page to a Canvas instance at 72 DPI.voidrenderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scale)Renders a given page to a Canvas instance.voidrenderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scaleX, float scaleY)Renders a given page to a Canvas instance.voidrenderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scaleX, float scaleY, RenderDestination destination)Renders a given page to a Canvas instance.voidsetAnnotationsFilter(AnnotationFilter annotationsFilter)Set the AnnotationFilter.voidsetDefaultDestination(RenderDestination defaultDestination)voidsetImageDownscalingOptimizationThreshold(float imageDownscalingOptimizationThreshold)Set the image downscaling optimization threshold.voidsetSubsamplingAllowed(boolean subsamplingAllowed)Sets a value instructing the renderer whether it is allowed to subsample images before drawing.
-
-
-
Field Detail
-
document
protected final PDDocument document
-
-
Constructor Detail
-
PDFRenderer
public PDFRenderer(PDDocument document)
Creates a new PDFRenderer.- Parameters:
document- the document to render
-
-
Method Detail
-
getAnnotationsFilter
public AnnotationFilter getAnnotationsFilter()
Return the AnnotationFilter.- Returns:
- the AnnotationFilter
-
setAnnotationsFilter
public void setAnnotationsFilter(AnnotationFilter annotationsFilter)
Set the AnnotationFilter.Allows to only render annotation accepted by the filter.
- Parameters:
annotationsFilter- the AnnotationFilter
-
isSubsamplingAllowed
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:
- true if subsampling of images is allowed, false otherwise.
-
setSubsamplingAllowed
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- The new value indicating if subsampling is allowed.
-
getDefaultDestination
public RenderDestination getDefaultDestination()
- Returns:
- the defaultDestination
-
setDefaultDestination
public void setDefaultDestination(RenderDestination defaultDestination)
- Parameters:
defaultDestination- the defaultDestination to set
-
getImageDownscalingOptimizationThreshold
public float getImageDownscalingOptimizationThreshold()
- Returns:
- get the image downscaling optimization threshold. See
getImageDownscalingOptimizationThreshold()for details.
-
setImageDownscalingOptimizationThreshold
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-
-
renderImage
public Bitmap renderImage(int pageIndex) throws IOException
Returns the given page as an RGB image at 72 DPI- Parameters:
pageIndex- the zero-based index of the page to be converted.- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderImage
public Bitmap 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- the zero-based index of the page to be convertedscale- the scaling factor, where 1 = 72 DPI- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderImageWithDPI
public Bitmap renderImageWithDPI(int pageIndex, float dpi) throws IOException
Returns the given page as an RGB image at the given DPI.- Parameters:
pageIndex- the zero-based index of the page to be converteddpi- the DPI (dots per inch) to render at- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderImageWithDPI
public Bitmap renderImageWithDPI(int pageIndex, float dpi, ImageType imageType) throws IOException
Returns the given page as an RGB image at the given DPI.- Parameters:
pageIndex- the zero-based index of the page to be converteddpi- the DPI (dots per inch) to render atimageType- the type of image to return- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderImage
public Bitmap 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- the zero-based index of the page to be convertedscale- the scaling factor, where 1 = 72 DPIimageType- the type of image to return- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderImage
public Bitmap 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- the zero-based index of the page to be convertedscale- the scaling factor, where 1 = 72 DPIimageType- the type of image to returndestination- controlling visibility of optional content groups- Returns:
- the rendered page image
- Throws:
IOException- if the PDF cannot be read
-
renderPageToGraphics
public void renderPageToGraphics(int pageIndex, Paint paint, Canvas canvas) throws IOExceptionRenders a given page to a Canvas instance at 72 DPI.Read
renderPageToGraphics(int, Graphics2D, float, float, RenderDestination)before using this.- Parameters:
pageIndex- the zero-based index of the page to be convertedpaint- the Paint that will be used to draw the pagecanvas- the Canvas on which to draw the page- Throws:
IOException- if the PDF cannot be read
-
renderPageToGraphics
public void renderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scale) throws IOExceptionRenders a given page to a Canvas instance.Read
renderPageToGraphics(int, Graphics2D, float, float, RenderDestination)before using this.- Parameters:
pageIndex- the zero-based index of the page to be convertedpaint- the Paint that will be used to draw the pagecanvas- the Canvas on which to draw the pagescale- the scaling factor, where 1 = 72 DPI- Throws:
IOException- if the PDF cannot be read
-
renderPageToGraphics
public void renderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scaleX, float scaleY) throws IOExceptionRenders a given page to a Canvas instance.Read
renderPageToGraphics(int, Graphics2D, float, float, RenderDestination)before using this.- Parameters:
pageIndex- the zero-based index of the page to be convertedpaint- the Paint that will be used to draw the pagecanvas- the Canvas on which to draw the pagescaleX- the scale to draw the page at for the x-axis, where 1 = 72 DPIscaleY- the scale to draw the page at for the y-axis, where 1 = 72 DPI- Throws:
IOException- if the PDF cannot be read
-
renderPageToGraphics
public void renderPageToGraphics(int pageIndex, Paint paint, Canvas canvas, float scaleX, float scaleY, RenderDestination destination) throws IOExceptionRenders a given page to a Canvas instance.- Parameters:
pageIndex- the zero-based index of the page to be convertedpaint- the Paint that will be used to draw the pagecanvas- the Canvas on which to draw the pagescaleX- the scale to draw the page at for the x-axis, where 1 = 72 DPIscaleY- the scale to draw the page at for the y-axis, where 1 = 72 DPIdestination- controlling visibility of optional content groups- Throws:
IOException- if the PDF cannot be read
-
isGroupEnabled
public boolean isGroupEnabled(PDOptionalContentGroup group)
Indicates whether an optional content group is enabled.- Parameters:
group- the group- Returns:
- true if the group is enabled
-
createPageDrawer
protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException
Returns a new PageDrawer instance, using the given parameters. May be overridden.- Throws:
IOException
-
-