|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.faceless.pdf2.PagePainter
public class PagePainter
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);
Field Summary | |
---|---|
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 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. |
Fields inherited from interface java.awt.print.Printable |
---|
NO_SUCH_PAGE, PAGE_EXISTS |
Method Summary | |
---|---|
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 |
static boolean |
hasAnnotationState(PDFAnnotation annotation,
String state)
Deprecated. replaced by PDFAnnotation.hasAppearanceState(java.lang.String) |
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. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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.
Method Detail |
---|
public 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
text
public void setRenderingHints(RenderingHints hints)
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();
Graphics2D.setRenderingHints(java.util.Map, ?>)
,
PDFParser.writeAsTIFF(OutputStream,int,ColorModel,RenderingHints)
,
KEY_SHAPETEXT
,
KEY_COLORCONVERTRGB
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 pixel
BufferedImage
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 with
BufferedImage
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 ColorModel
x1
- 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 pixel
BufferedImage
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 ColorModel
x1
- 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 with
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.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
- how many dots per inch to draw the image. A value of 72 gives in 1 point per pixel
public int print(Graphics g, PageFormat format, int pagenum)
print
in interface Printable
public static boolean hasAnnotationState(PDFAnnotation annotation, String state)
PDFAnnotation.hasAppearanceState(java.lang.String)
annotation
- the Annotationstate
- the state to check - "N" for normal, "D" for down or "R" for rolloverpublic 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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |