Class PdfBoxGraphics2D
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private static class
private class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Color
private final AffineTransform
(package private) final org.apache.pdfbox.pdmodel.common.PDRectangle
private final Graphics2D
private BufferedImage
private Shape
private IPdfBoxGraphics2DColorMapper
(package private) final IPdfBoxGraphics2DColorMapper.IColorMapperEnv
private Composite
private final org.apache.pdfbox.pdmodel.PDPageContentStream
private final PdfBoxGraphics2D.CopyInfo
private final List
<PdfBoxGraphics2D.CopyInfo> private org.apache.pdfbox.pdmodel.PDDocument
private IPdfBoxGraphics2DDrawControl
private final IPdfBoxGraphics2DDrawControl.IDrawControlEnv
static boolean
Global Flag: If set to true the Callstack when creating a context is recorded.private static final boolean
Internal Debug flag.private static final boolean
Interal debugflag to see if an unkown stroke is mappedprivate Font
private final IPdfBoxGraphics2DFontTextDrawer.IFontTextDrawerEnv
private IPdfBoxGraphics2DFontTextDrawer
private boolean
Do we currently have an active path on the content stream, which has not been closed?private IPdfBoxGraphics2DImageEncoder
(package private) final IPdfBoxGraphics2DImageEncoder.IPdfBoxGraphics2DImageEncoderEnv
private Paint
private IPdfBoxGraphics2DPaintApplier
private final PdfBoxGraphics2D.PaintApplyResult
private final PdfBoxGraphics2D.PaintEnvImpl
private final Map
<RenderingHints.Key, Object> private int
private Stroke
private AffineTransform
private final org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject
private Color
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
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) (package private)
PdfBoxGraphics2D
(org.apache.pdfbox.pdmodel.PDDocument document, org.apache.pdfbox.pdmodel.common.PDRectangle bbox, PdfBoxGraphics2D parentGfx) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRenderingHints
(Map<?, ?> hints) applyPaint
(Paint paintToApply, Shape shapeToDraw) applyPaint
(Shape shapeToDraw) private void
applyShadingAsColor
(org.apache.pdfbox.pdmodel.graphics.shading.PDShading shading) private void
applyStroke
(Stroke strokeToApply) Internal usage only!private float
calculateTransformedLength
(float length, AffineTransform tf) private void
void
clearRect
(int x, int y, int width, int height) void
void
clipRect
(int x, int y, int width, int height) private void
private void
void
copyArea
(int x, int y, int width, int height, int dx, int dy) create()
Creates a copy of this graphics object.create
(int x, int y, int width, int height) void
dispose()
private static void
void
void
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
drawInMarkedContentSequence
(org.apache.pdfbox.cos.COSName tagName, IPdfBoxGraphics2DMarkedContentDrawer drawer) Draw on the Graphics2D and enclose the drawing command with a BMC/EMC content marking pair.void
drawInMarkedContentSequence
(org.apache.pdfbox.cos.COSName tagName, org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList properties, IPdfBoxGraphics2DMarkedContentDrawer drawer) Draw on the Graphics2D and enclose the drawing command with a BDC/EMC content marking pair.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
drawRect
(int x, int y, int width, int height) 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) private void
drawStringUsingShapes
(AttributedCharacterIterator iterator, float x, float y) private void
drawStringUsingText
(AttributedCharacterIterator iterator, float x, float y) private void
fill
(boolean useEvenOdd) void
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) private String
private static String
getClip()
getColor()
private AffineTransform
getFont()
getPaint()
getRenderingHint
(RenderingHints.Key hintKey) org.apache.pdfbox.pdmodel.PDResources
Sometimes you need to access the PDResources and add special resources to it for some stuff (e.g.org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject
*AFTER* you have disposed() this Graphics2D you can access the XFormboolean
(package private) void
internalClip
(boolean useEvenOdd) Perform a clip, but only if we really have an active clipping pathprivate static boolean
isFinite
(float f) Float#isFinite() is JDK 8+.private static boolean
isFinite
(float[] coords, int count) (package private) void
Set an internal flag that some path - which may be added from the paint applyer to the content stream or by walkShape() - is on the content stream.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
void
void
setColorMapper
(IPdfBoxGraphics2DColorMapper colorMapper) Set a new color mapper.void
setComposite
(Composite comp) void
setDrawControl
(IPdfBoxGraphics2DDrawControl drawControl) Set a new draw control.void
void
setFontTextDrawer
(IPdfBoxGraphics2DFontTextDrawer fontTextDrawer) Set an optional text drawer.void
setImageEncoder
(IPdfBoxGraphics2DImageEncoder imageEncoder) Set a new image encodervoid
void
setPaintApplier
(IPdfBoxGraphics2DPaintApplier paintApplier) Set a new paint applier.void
void
setRenderingHint
(RenderingHints.Key hintKey, Object hintValue) void
setRenderingHints
(Map<?, ?> hints) void
void
void
setXORMode
(Color c1) XOR Mode is currently not implemented as it's not possible in PDF.void
shear
(double shx, double shy) (package private) static <T> T
Internal helper functionvoid
void
translate
(double tx, double ty) void
translate
(int x, int y) private void
walkAndFillFromApplyPaintResult
(Shape shapeToFill, PdfBoxGraphics2D.PaintApplyResult result) private void
walkAndFillShape
(Shape shapeToFill) private boolean
Walk the path and return true if we need to use the even odd winding rule.Methods inherited from class java.awt.Graphics2D
draw3DRect, fill3DRect
Methods inherited from class java.awt.Graphics
drawBytes, drawChars, drawPolygon, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString
-
Field Details
-
xFormObject
private final org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject xFormObject -
calcGfx
-
contentStream
private final org.apache.pdfbox.pdmodel.PDPageContentStream contentStream -
calcImage
-
document
private org.apache.pdfbox.pdmodel.PDDocument document -
baseTransform
-
transform
-
imageEncoder
-
colorMapper
-
paintApplier
-
fontTextDrawer
-
drawControl
-
paint
-
stroke
-
xorColor
-
font
-
composite
-
clipShape
-
backgroundColor
-
copyInfo
-
bbox
final org.apache.pdfbox.pdmodel.common.PDRectangle bbox -
saveCounter
private int saveCounter -
copyList
-
ENABLE_CHILD_CREATING_DEBUG
public static boolean ENABLE_CHILD_CREATING_DEBUGGlobal Flag: If set to true the Callstack when creating a context is recorded.Note: Setting this to true will slow down the library. Use this only for debugging.
-
drawControlEnv
-
ENABLE_DEBUG_UNKOWN_STROKE
private static final boolean ENABLE_DEBUG_UNKOWN_STROKEInteral debugflag to see if an unkown stroke is mapped- See Also:
-
imageEncoderEnv
-
fontDrawerEnv
-
paintEnv
-
colorMapperEnv
-
paintApplyResult
-
renderingHints
-
ENABLE_DEBUG_INTERNAL_CLIP
private static final boolean ENABLE_DEBUG_INTERNAL_CLIPInternal Debug flag.- See Also:
-
hasPathOnStream
private boolean hasPathOnStreamDo we currently have an active path on the content stream, which has not been closed?We need this flag to avoid to clip twice if both the plaint applyer needs to clip, and we have some clipping. If at the end we try to clip with an empty path, then Acrobat Reader does not like that and draws nothing.
-
-
Constructor Details
-
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
.
-
PdfBoxGraphics2D
PdfBoxGraphics2D(org.apache.pdfbox.pdmodel.PDDocument document, org.apache.pdfbox.pdmodel.common.PDRectangle bbox, PdfBoxGraphics2D parentGfx) throws IOException - Throws:
IOException
-
PdfBoxGraphics2D
- Throws:
IOException
-
-
Method Details
-
setColorMapper
Set a new color mapper.- Parameters:
colorMapper
- the color mapper which maps Color to PDColor.
-
setImageEncoder
Set a new image encoder- Parameters:
imageEncoder
- the image encoder, which encodes an image as PDImageXForm.
-
setPaintApplier
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
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
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
-
getResources
public org.apache.pdfbox.pdmodel.PDResources getResources()Sometimes you need to access the PDResources and add special resources to it for some stuff (e.g. patterns of embedded PDFs or simmilar). For that you need the PDResources associated with the XForm.It's identlical with getXFormObject().getResources(), with the difference beeing that you can access it while the Graphics2D is not yet disposed.
- Returns:
- the PDResources of the resulting XForm
-
getXFormObject
public org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject getXFormObject()*AFTER* you have disposed() this Graphics2D you can access the XForm- Returns:
- the PDFormXObject which resulted in this graphics
-
gatherContext
-
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()
-
disposeCopies
-
dispose
public void dispose() -
draw
- Specified by:
draw
in classGraphics2D
-
applyStroke
Internal usage only!- Parameters:
strokeToApply
- the stroke we should apply on the stream- Throws:
IOException
-
calculateTransformedLength
-
getCurrentEffectiveTransform
-
drawImage
- Specified by:
drawImage
in classGraphics2D
-
drawRenderedImage
- Specified by:
drawRenderedImage
in classGraphics2D
-
drawRenderableImage
- Specified by:
drawRenderableImage
in classGraphics2D
-
drawString
- Specified by:
drawString
in classGraphics2D
-
drawString
- Specified by:
drawString
in classGraphics2D
-
drawString
- Specified by:
drawString
in classGraphics2D
-
drawImage
-
drawImage
-
drawImage
-
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
- 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) -
drawStringUsingShapes
-
drawString
- Specified by:
drawString
in classGraphics2D
-
drawStringUsingText
private void drawStringUsingText(AttributedCharacterIterator iterator, float x, float y) throws IOException, FontFormatException - Throws:
IOException
FontFormatException
-
contentStreamSaveState
- Throws:
IOException
-
contentStreamRestoreState
- Throws:
IOException
-
drawGlyphVector
- Specified by:
drawGlyphVector
in classGraphics2D
-
fill
- Specified by:
fill
in classGraphics2D
-
walkAndFillFromApplyPaintResult
private void walkAndFillFromApplyPaintResult(Shape shapeToFill, PdfBoxGraphics2D.PaintApplyResult result) throws IOException - Throws:
IOException
-
walkAndFillShape
- Throws:
IOException
-
fill
- Throws:
IOException
-
applyShadingAsColor
private void applyShadingAsColor(org.apache.pdfbox.pdmodel.graphics.shading.PDShading shading) throws IOException - Throws:
IOException
-
applyPaint
- Throws:
IOException
-
applyPaint
private PdfBoxGraphics2D.PaintApplyResult applyPaint(Paint paintToApply, Shape shapeToDraw) throws IOException - Throws:
IOException
-
hit
- Specified by:
hit
in classGraphics2D
-
getDeviceConfiguration
- Specified by:
getDeviceConfiguration
in classGraphics2D
-
setComposite
- Specified by:
setComposite
in classGraphics2D
-
setPaint
- Specified by:
setPaint
in classGraphics2D
-
setStroke
- Specified by:
setStroke
in classGraphics2D
-
setRenderingHint
- Specified by:
setRenderingHint
in classGraphics2D
-
getRenderingHint
- Specified by:
getRenderingHint
in classGraphics2D
-
setRenderingHints
- Specified by:
setRenderingHints
in classGraphics2D
-
addRenderingHints
- Specified by:
addRenderingHints
in classGraphics2D
-
getRenderingHints
- Specified by:
getRenderingHints
in classGraphics2D
-
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. -
drawInMarkedContentSequence
public void drawInMarkedContentSequence(org.apache.pdfbox.cos.COSName tagName, IPdfBoxGraphics2DMarkedContentDrawer drawer) Draw on the Graphics2D and enclose the drawing command with a BMC/EMC content marking pair. See the PDF Spec about "Marked Content" for details.- Parameters:
tagName
- A COSName for to tag the marked contentdrawer
- is called with a (child) graphics to draw on. Please do *not* dispose() this graphics. Just draw on it. Any state changes on the given graphics will be reset after the drawing is finished
-
drawInMarkedContentSequence
public void drawInMarkedContentSequence(org.apache.pdfbox.cos.COSName tagName, org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList properties, IPdfBoxGraphics2DMarkedContentDrawer drawer) Draw on the Graphics2D and enclose the drawing command with a BDC/EMC content marking pair. See the PDF Spec about "Marked Content" for details.- Parameters:
tagName
- A COSName for to tag the marked contentproperties
- The properties to put by the marked sequencedrawer
- is called with a (child) graphics to draw on. Please do *not* dispose() this graphics. Just draw on it. Any state changes on the given graphics will be reset after the drawing is finished
-
create
-
translate
public void translate(int x, int y) - Specified by:
translate
in classGraphics2D
-
getColor
-
setColor
-
setPaintMode
public void setPaintMode()- Specified by:
setPaintMode
in classGraphics
-
setXORMode
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
-
getFont
-
setFont
-
getFontMetrics
- Specified by:
getFontMetrics
in classGraphics
-
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) -
getClip
-
setClip
-
internalClip
Perform a clip, but only if we really have an active clipping path- Parameters:
useEvenOdd
- true when we should use the evenOdd rule.- Throws:
IOException
-
isFinite
private static boolean isFinite(float f) Float#isFinite() is JDK 8+. We just copied the trivial implementation here. When we require JDK 8+ we can just drop this method and replace it bei Float#isFinite() -
isFinite
private static boolean isFinite(float[] coords, int count) - Returns:
- true when all required values are finite
-
markPathIsOnStream
void markPathIsOnStream()Set an internal flag that some path - which may be added from the paint applyer to the content stream or by walkShape() - is on the content stream. We can then safely clip() if there is a path on the content stream. -
walkShape
Walk the path and return true if we need to use the even odd winding rule.- Returns:
- true if we need to use the even odd winding rule
- Throws:
IOException
-
checkNoCopyActive
private void checkNoCopyActive() -
gatherDebugCopyInfo
-
throwException
Internal helper function- Parameters:
e
- exception to rethrow
-
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) -
drawRect
public void drawRect(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
- Specified by:
transform
in classGraphics2D
-
setTransform
- Specified by:
setTransform
in classGraphics2D
-
getTransform
- Specified by:
getTransform
in classGraphics2D
-
getPaint
- Specified by:
getPaint
in classGraphics2D
-
getComposite
- Specified by:
getComposite
in classGraphics2D
-
setBackground
- Specified by:
setBackground
in classGraphics2D
-
getBackground
- Specified by:
getBackground
in classGraphics2D
-
getStroke
- Specified by:
getStroke
in classGraphics2D
-
clip
- Specified by:
clip
in classGraphics2D
-
getFontRenderContext
- Specified by:
getFontRenderContext
in classGraphics2D
-