public class PagePainter extends Object implements Printable
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);
Modifier and Type | Field and Description |
---|---|
static RenderingHints.Key |
KEY_COLORCONVERTRGB
A
RenderingHints.Key which tells the rendering process to convert all colors
to RGB. |
static RenderingHints.Key |
KEY_SHAPETEXT
A
RenderingHints.Key which determines whether text is rendered as a Shape. |
static RenderingHints.Key |
KEY_SHAPETEXT_FONT
A
RenderingHints.Key which allow text that is rendered as a shape to be
assocated with a glyph. |
static RenderingHints.Key |
KEY_SHAPETEXT_STRING
A
RenderingHints.Key which allow text that is rendered as a shape to be
assocated with a glyph. |
static RenderingHints.Key |
KEY_SOFTCLIP
A
RenderingHints.Key which determines whether shapes are "soft-clipped"
when rendering. |
static 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. |
static Object |
VALUE_COLORCONVERTRGB_OFF
A Rendering Hint value for
KEY_COLORCONVERTRGB which causes the rendering process
to never convert colors to RGB. |
static Object |
VALUE_COLORCONVERTRGB_ON
A Rendering Hint value for
KEY_COLORCONVERTRGB which causes the rendering process
to always convert colors to RGB. |
static Object |
VALUE_SHAPETEXT_DEFAULT
A Rendering Hint value for
KEY_SHAPETEXT which causes the rendering process
to never draw text as shapes. |
static 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. |
static Object |
VALUE_SHAPETEXT_ON
A Rendering Hint value for
KEY_SHAPETEXT which causes the rendering process
to always draw text as shapes. |
static Object |
VALUE_SOFTCLIP_DEFAULT
A Rendering Hint value for
KEY_SOFTCLIP which causes the rendering process
to do what it feels is best |
static Object |
VALUE_SOFTCLIP_OFF
A Rendering Hint value for
KEY_SOFTCLIP which causes the rendering process
to clip normally. |
static 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. |
NO_SUCH_PAGE, PAGE_EXISTS
Modifier and Type | Method and Description |
---|---|
boolean |
drawSubImage(Graphics2D graphics,
double x1,
double y1,
double x2,
double y2,
float dpi)
Draw an area of a page onto a
Graphics2D object. |
Composite |
getAnnotationComposite()
Return the
Composite that should be used to place the
annotation painted by paintAnnotation()
onto the main page. |
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. |
BufferedImage |
getImage(float dpi,
ColorModel cm)
Draw the area inside the
ViewBox if specified, or the whole page if not
to a BufferedImage in the specified ColorModel. |
PDFPage |
getPage()
Return the PDFPage being painted by this object
|
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 |
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 |
void |
interrupt()
Interrupts and stops the current painting operation.
|
boolean |
isPainting()
Returns true if the current painting operation is
still in progress.
|
boolean |
paintAnnotation(PDFAnnotation annotation,
String state,
Graphics2D graphics,
float[] rect)
Paint a single annotation.
|
int |
print(Graphics g,
PageFormat format,
int pagenum) |
void |
setBackground(Paint background)
Set the background of any images drawn with this
PagePainter . |
void |
setBox(String boxname)
Set the default "box" to paint.
|
void |
setPageExtractor(PageExtractor extractor)
Set the
PageExtractor to extract text to. |
void |
setPaintAnnotations(boolean paintannotations)
Set whether any annotations on this page are to be painted or not.
|
void |
setRenderingHints(RenderingHints hints)
Set additional rendering hints on the Graphics object.
|
public static final RenderingHints.Key KEY_COLORCONVERTRGB
RenderingHints.Key
which tells the rendering process to convert all colors
to RGB. This is required when printing due to bugs in the sun.print
package
up to Java 1.6.setRenderingHints(java.awt.RenderingHints)
public static final Object VALUE_COLORCONVERTRGB_DEFAULT
KEY_COLORCONVERTRGB
which causes the rendering process
to convert colors to RGB only when it's deemed necessary.public static final Object VALUE_COLORCONVERTRGB_ON
KEY_COLORCONVERTRGB
which causes the rendering process
to always convert colors to RGB.public static final Object VALUE_COLORCONVERTRGB_OFF
KEY_COLORCONVERTRGB
which causes the rendering process
to never convert colors to RGB.public static final RenderingHints.Key KEY_SHAPETEXT
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.setRenderingHints(java.awt.RenderingHints)
public static final Object VALUE_SHAPETEXT_OFF
KEY_SHAPETEXT
which causes the rendering process
to draw text as shapes where we think the benefits outweigh the costs.public static final Object VALUE_SHAPETEXT_ON
KEY_SHAPETEXT
which causes the rendering process
to always draw text as shapes.public static final Object VALUE_SHAPETEXT_DEFAULT
KEY_SHAPETEXT
which causes the rendering process
to never draw text as shapes.public static final RenderingHints.Key KEY_SOFTCLIP
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.public static final Object VALUE_SOFTCLIP_ON
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.public static final Object VALUE_SOFTCLIP_OFF
KEY_SOFTCLIP
which causes the rendering process
to clip normally.public static final Object VALUE_SOFTCLIP_DEFAULT
KEY_SOFTCLIP
which causes the rendering process
to do what it feels is bestpublic static final RenderingHints.Key KEY_SHAPETEXT_FONT
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 usepublic static final RenderingHints.Key KEY_SHAPETEXT_STRING
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 textpublic void setBox(String boxname)
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.boxname
- the name of the page box to render by default - "CropBox", "MediaBox", "ArtBox" or "TrimBox"public void setBackground(Paint background)
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.background
- The background paint to draw the page on to. May be null
.public void setPaintAnnotations(boolean paintannotations)
public PDFPage getPage()
public void setPageExtractor(PageExtractor extractor)
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
textpublic void setRenderingHints(RenderingHints hints)
Set additional rendering hints on the Graphics object. When rendering a PDF to a
BufferedImage
using one of the various getImage(float)
methods, this method
can be called first to control the subtleties of the rendering process. Any hints
that are set in the supplied hints
variable are merged with the default
hints of the Graphics2D
the 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();
public BufferedImage getImage(float dpi)
ViewBox
if specified, or the whole page if not
to a BufferedImage
in the standard sRGB ColorModel.dpi
- how many dots per inch to draw the image. A value of 72 gives in 1 point per pixelBufferedImage
that is width*dpi/72
pixels wide and
height*dpi/72
pixels high, in the ColorModel returned by ColorModel.getRGBdefault()
public BufferedImage getImage(float dpi, ColorModel cm)
ViewBox
if specified, or the whole page if not
to a BufferedImage
in the specified ColorModel.dpi
- how many dots per inch to draw the image. A value of 72 gives 1 point per pixelcm
- the ColorModel
to use to create the image withBufferedImage
that is width*dpi/72
pixels wide and
height*dpi/72
pixels high using the specified ColorModel
PDFParser.BLACKANDWHITE
,
PDFParser.GRAYSCALE
,
PDFParser.RGB
,
PDFParser.RGBA
,
PDFParser.CMYK
public BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi)
BufferedImage
in the standard sRGB ColorModelx1
- 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 pixelBufferedImage
that is (x2-x1)*dpi/72
pixels wide and
(y2-y1)*dpi/72
pixels high, in the ColorModel returned by ColorModel.getRGBdefault()
public BufferedImage getSubImage(double x1, double y1, double x2, double y2, float dpi, ColorModel cm)
BufferedImage
using the specified ColorModelx1
- 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
- the ColorModel
to use to create the image withBufferedImage
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.public boolean drawSubImage(Graphics2D graphics, double x1, double y1, double x2, double y2, float dpi)
Graphics2D
object. If the setRenderingHints(java.awt.RenderingHints)
method has been called, those hints will override the equivalent hints on the
supplied graphics
object.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 Graphics2Dpublic int print(Graphics g, PageFormat format, int pagenum)
public boolean paintAnnotation(PDFAnnotation annotation, String state, Graphics2D graphics, float[] rect)
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.public Composite getAnnotationComposite()
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.public void interrupt()
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.public boolean isPainting()
interrupt
method must be called
to stop it before the painting thread is killed.Copyright © 2001-2017 Big Faceless Organization