Package de.rototor.pdfbox.graphics2d
Class PdfBoxGraphics2D
- java.lang.Object
-
- java.awt.Graphics
-
- java.awt.Graphics2D
-
- de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D
-
public class PdfBoxGraphics2D extends Graphics2D
Graphics 2D Adapter for PDFBox.
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
ENABLE_CHILD_CREATING_DEBUG
Global Flag: If set to true the Callstack when creating a context is recorded.
-
Constructor Summary
Constructors Constructor Description PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, float pixelWidth, float pixelHeight)
Create a PDfBox Graphics2D.PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, int pixelWidth, int pixelHeight)
Create a PDfBox Graphics2D.PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, org.apache.pdfbox.pdmodel.common.PDRectangle bbox)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRenderingHints(Map<?,?> hints)
void
clearRect(int x, int y, int width, int height)
void
clip(Shape shape)
void
clipRect(int x, int y, int width, int height)
void
copyArea(int x, int y, int width, int height, int dx, int dy)
PdfBoxGraphics2D
create()
Creates a copy of this graphics object.PdfBoxGraphics2D
create(int x, int y, int width, int height)
void
dispose()
void
disposeDanglingChildGraphics()
void
draw(Shape s)
void
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
void
drawGlyphVector(GlyphVector g, float x, float y)
void
drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, ImageObserver observer)
boolean
drawImage(Image img, AffineTransform xform, ImageObserver obs)
void
drawLine(int x1, int y1, int x2, int y2)
void
drawOval(int x, int y, int width, int height)
void
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
void
drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
void
drawRenderableImage(RenderableImage img, AffineTransform xform)
void
drawRenderedImage(RenderedImage img, AffineTransform xform)
void
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
void
drawString(String str, float x, float y)
void
drawString(String str, int x, int y)
void
drawString(AttributedCharacterIterator iterator, float x, float y)
void
drawString(AttributedCharacterIterator iterator, int x, int y)
void
fill(Shape s)
void
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
void
fillOval(int x, int y, int width, int height)
void
fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
void
fillRect(int x, int y, int width, int height)
void
fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Color
getBackground()
Shape
getClip()
Rectangle
getClipBounds()
Color
getColor()
Composite
getComposite()
GraphicsConfiguration
getDeviceConfiguration()
Font
getFont()
FontMetrics
getFontMetrics(Font f)
FontRenderContext
getFontRenderContext()
Paint
getPaint()
Object
getRenderingHint(RenderingHints.Key hintKey)
RenderingHints
getRenderingHints()
Stroke
getStroke()
AffineTransform
getTransform()
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject
getXFormObject()
boolean
hit(Rectangle rect, Shape s, boolean onStroke)
void
rotate(double theta)
void
rotate(double theta, double x, double y)
void
scale(double sx, double sy)
void
setBackground(Color color)
void
setClip(int x, int y, int width, int height)
void
setClip(Shape clip)
void
setColor(Color color)
void
setColorMapper(IPdfBoxGraphics2DColorMapper colorMapper)
Set a new color mapper.void
setComposite(Composite comp)
void
setDrawControl(IPdfBoxGraphics2DDrawControl drawControl)
Set a new draw control.void
setFont(Font font)
void
setFontTextDrawer(IPdfBoxGraphics2DFontTextDrawer fontTextDrawer)
Set an optional text drawer.void
setImageEncoder(IPdfBoxGraphics2DImageEncoder imageEncoder)
Set a new image encodervoid
setPaint(Paint paint)
void
setPaintApplier(IPdfBoxGraphics2DPaintApplier paintApplier)
Set a new paint applier.void
setPaintMode()
void
setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
void
setRenderingHints(Map<?,?> hints)
void
setStroke(Stroke stroke)
void
setTransform(AffineTransform Tx)
void
setXORMode(Color c1)
XOR Mode is currently not implemented as it's not possible in PDF.void
shear(double shx, double shy)
void
transform(AffineTransform Tx)
void
translate(double tx, double ty)
void
translate(int x, int y)
-
Methods inherited from class java.awt.Graphics2D
draw3DRect, fill3DRect
-
Methods inherited from class java.awt.Graphics
drawBytes, drawChars, drawPolygon, drawRect, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString
-
-
-
-
Constructor Detail
-
PdfBoxGraphics2D
public PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, int pixelWidth, int pixelHeight) throws IOException
Create a PDfBox Graphics2D. This size is used for the BBox of the XForm. So everything drawn outside the rectangle (0x0)-(pixelWidth,pixelHeight) will be clipped.Note: pixelWidth and pixelHeight only define the size of the coordinate space within this Graphics2D. They do not affect how big the XForm is finally displayed in the PDF.
- Parameters:
document
- The document the graphics should be used to create a XForm in.pixelWidth
- the width in pixel of the drawing area.pixelHeight
- the height in pixel of the drawing area.- Throws:
IOException
- if something goes wrong with writing into the content stream of thePDDocument
.
-
PdfBoxGraphics2D
public PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, float pixelWidth, float pixelHeight) throws IOException
Create a PDfBox Graphics2D. This size is used for the BBox of the XForm. So everything drawn outside the rectangle (0x0)-(pixelWidth,pixelHeight) will be clipped.Note: pixelWidth and pixelHeight only define the size of the coordinate space within this Graphics2D. They do not affect how big the XForm is finally displayed in the PDF.
- Parameters:
document
- The document the graphics should be used to create a XForm in.pixelWidth
- the width in pixel of the drawing area.pixelHeight
- the height in pixel of the drawing area.- Throws:
IOException
- if something goes wrong with writing into the content stream of thePDDocument
.
-
PdfBoxGraphics2D
public PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, org.apache.pdfbox.pdmodel.common.PDRectangle bbox) throws IOException
- Parameters:
document
- The document the graphics should be used to create a XForm in.bbox
- Bounding Box of the graphics- Throws:
IOException
- when something goes wrong with writing into the content stream of thePDDocument
.
-
-
Method Detail
-
setColorMapper
public void setColorMapper(IPdfBoxGraphics2DColorMapper colorMapper)
Set a new color mapper.- Parameters:
colorMapper
- the color mapper which maps Color to PDColor.
-
setImageEncoder
public void setImageEncoder(IPdfBoxGraphics2DImageEncoder imageEncoder)
Set a new image encoder- Parameters:
imageEncoder
- the image encoder, which encodes a image as PDImageXForm.
-
setPaintApplier
public void setPaintApplier(IPdfBoxGraphics2DPaintApplier paintApplier)
Set a new paint applier. You should always derive your custom paint applier from theIPdfBoxGraphics2DPaintApplier
and just extend the paint mapping for custom paint.If the paint you map is a paint from a standard library and you can implement the mapping using reflection please feel free to send a pull request to extend the default paint mapper.
- Parameters:
paintApplier
- the paint applier responsible for mapping the paint correctly
-
setDrawControl
public void setDrawControl(IPdfBoxGraphics2DDrawControl drawControl)
Set a new draw control. This allows you to influence fill() and draw() operations. drawString() is only influence if the text is drawn as vector shape.- Parameters:
drawControl
- the draw control
-
setFontTextDrawer
public void setFontTextDrawer(IPdfBoxGraphics2DFontTextDrawer fontTextDrawer)
Set an optional text drawer. By default, all text is vectorized and drawn using vector shapes. To embed fonts into a PDF file it is necessary to have the underlying TTF file. The java.awt.Font class does not provide that. The FontTextDrawer must perform the java.awt.Font <=> PDFont mapping and also must perform the text layout. If it can not map the text or font correctly, the font drawing falls back to vectoring the text.- Parameters:
fontTextDrawer
- The text drawer, which can draw text using fonts
-
getXFormObject
public org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject getXFormObject()
- Returns:
- the PDAppearanceStream which resulted in this graphics
-
disposeDanglingChildGraphics
public void disposeDanglingChildGraphics()
Sometimes the users ofcreate()
don't correctlydispose()
the child graphics they create. And you may not always be able to fix this uses, as it may be in some 3rdparty library. In this case this method can help you. It will cleanup all dangling child graphics. The child graphics can not be used after that. This method is a workaround for a buggy old code. You should only use it if you have to.
Note: You can only call this method on the "main" graphics, not on a child created with
create()
-
draw
public void draw(Shape s)
- Specified by:
draw
in classGraphics2D
-
drawImage
public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
- Specified by:
drawImage
in classGraphics2D
-
drawRenderedImage
public void drawRenderedImage(RenderedImage img, AffineTransform xform)
- Specified by:
drawRenderedImage
in classGraphics2D
-
drawRenderableImage
public void drawRenderableImage(RenderableImage img, AffineTransform xform)
- Specified by:
drawRenderableImage
in classGraphics2D
-
drawString
public void drawString(String str, int x, int y)
- Specified by:
drawString
in classGraphics2D
-
drawString
public void drawString(String str, float x, float y)
- Specified by:
drawString
in classGraphics2D
-
drawString
public void drawString(AttributedCharacterIterator iterator, int x, int y)
- Specified by:
drawString
in classGraphics2D
-
drawImage
public boolean drawImage(Image img, int x, int y, ImageObserver observer)
-
drawImage
public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
-
drawImage
public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
-
drawImage
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
-
drawImage
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
-
drawImage
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
- Specified by:
drawImage
in classGraphics2D
-
drawImage
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
-
drawString
public void drawString(AttributedCharacterIterator iterator, float x, float y)
- Specified by:
drawString
in classGraphics2D
-
drawGlyphVector
public void drawGlyphVector(GlyphVector g, float x, float y)
- Specified by:
drawGlyphVector
in classGraphics2D
-
fill
public void fill(Shape s)
- Specified by:
fill
in classGraphics2D
-
hit
public boolean hit(Rectangle rect, Shape s, boolean onStroke)
- Specified by:
hit
in classGraphics2D
-
getDeviceConfiguration
public GraphicsConfiguration getDeviceConfiguration()
- Specified by:
getDeviceConfiguration
in classGraphics2D
-
setComposite
public void setComposite(Composite comp)
- Specified by:
setComposite
in classGraphics2D
-
setPaint
public void setPaint(Paint paint)
- Specified by:
setPaint
in classGraphics2D
-
setStroke
public void setStroke(Stroke stroke)
- Specified by:
setStroke
in classGraphics2D
-
setRenderingHint
public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
- Specified by:
setRenderingHint
in classGraphics2D
-
getRenderingHint
public Object getRenderingHint(RenderingHints.Key hintKey)
- Specified by:
getRenderingHint
in classGraphics2D
-
setRenderingHints
public void setRenderingHints(Map<?,?> hints)
- Specified by:
setRenderingHints
in classGraphics2D
-
addRenderingHints
public void addRenderingHints(Map<?,?> hints)
- Specified by:
addRenderingHints
in classGraphics2D
-
getRenderingHints
public RenderingHints getRenderingHints()
- Specified by:
getRenderingHints
in classGraphics2D
-
create
public PdfBoxGraphics2D create()
Creates a copy of this graphics object. Please calldispose()
always on the copy after you have finished drawing with it.
Never draw both in this copy and its parent graphics at the same time, as they all write to the same content stream. This will create a broken PDF content stream. You should get anIllegalStateException
if you do so, but better just don't try.
The copy allows you to have different transforms, paints, etc. than the parent graphics context without affecting the parent. You may also call create() on a copy, but always remember to calldispose()
in reverse order.
-
create
public PdfBoxGraphics2D create(int x, int y, int width, int height)
-
translate
public void translate(int x, int y)
- Specified by:
translate
in classGraphics2D
-
setPaintMode
public void setPaintMode()
- Specified by:
setPaintMode
in classGraphics
-
setXORMode
public void setXORMode(Color c1)
XOR Mode is currently not implemented as it's not possible in PDF. This mode is ignored.- Specified by:
setXORMode
in classGraphics
- Parameters:
c1
- the XORMode Color
-
getFontMetrics
public FontMetrics getFontMetrics(Font f)
- Specified by:
getFontMetrics
in classGraphics
-
getClipBounds
public Rectangle getClipBounds()
- Specified by:
getClipBounds
in classGraphics
-
clipRect
public void clipRect(int x, int y, int width, int height)
-
setClip
public void setClip(int x, int y, int width, int height)
-
copyArea
public void copyArea(int x, int y, int width, int height, int dx, int dy)
-
drawLine
public void drawLine(int x1, int y1, int x2, int y2)
-
fillRect
public void fillRect(int x, int y, int width, int height)
-
clearRect
public void clearRect(int x, int y, int width, int height)
-
drawRoundRect
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
- Specified by:
drawRoundRect
in classGraphics
-
fillRoundRect
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
- Specified by:
fillRoundRect
in classGraphics
-
drawOval
public void drawOval(int x, int y, int width, int height)
-
fillOval
public void fillOval(int x, int y, int width, int height)
-
drawArc
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
-
fillArc
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
-
drawPolyline
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
- Specified by:
drawPolyline
in classGraphics
-
drawPolygon
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
- Specified by:
drawPolygon
in classGraphics
-
fillPolygon
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
- Specified by:
fillPolygon
in classGraphics
-
translate
public void translate(double tx, double ty)
- Specified by:
translate
in classGraphics2D
-
rotate
public void rotate(double theta)
- Specified by:
rotate
in classGraphics2D
-
rotate
public void rotate(double theta, double x, double y)
- Specified by:
rotate
in classGraphics2D
-
scale
public void scale(double sx, double sy)
- Specified by:
scale
in classGraphics2D
-
shear
public void shear(double shx, double shy)
- Specified by:
shear
in classGraphics2D
-
transform
public void transform(AffineTransform Tx)
- Specified by:
transform
in classGraphics2D
-
setTransform
public void setTransform(AffineTransform Tx)
- Specified by:
setTransform
in classGraphics2D
-
getTransform
public AffineTransform getTransform()
- Specified by:
getTransform
in classGraphics2D
-
getPaint
public Paint getPaint()
- Specified by:
getPaint
in classGraphics2D
-
getComposite
public Composite getComposite()
- Specified by:
getComposite
in classGraphics2D
-
setBackground
public void setBackground(Color color)
- Specified by:
setBackground
in classGraphics2D
-
getBackground
public Color getBackground()
- Specified by:
getBackground
in classGraphics2D
-
getStroke
public Stroke getStroke()
- Specified by:
getStroke
in classGraphics2D
-
clip
public void clip(Shape shape)
- Specified by:
clip
in classGraphics2D
-
getFontRenderContext
public FontRenderContext getFontRenderContext()
- Specified by:
getFontRenderContext
in classGraphics2D
-
-