Class PdfContentByte
- Direct Known Subclasses:
PdfTemplate
PdfContentByte is an object containing the user positioned
text and graphic contents of a page. It knows how to apply the proper font encoding.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe alignment is centerstatic final intThe alignment is leftstatic final intThe alignment is rightprotected ByteBufferThis is the actual contentstatic final intA possible line cap valuestatic final intA possible line join valueprotected PdfDocumentThis is the PdfDocumentprotected intThe separator between commands.protected org.openpdf.text.pdf.PdfContentByte.GraphicStateThis is the GraphicState in useprotected List<org.openpdf.text.pdf.PdfContentByte.GraphicState> The list were we save/restore the stateprotected PdfWriterThis is the writer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds anImageto the page.voidAdds anImageto the page.voidAdds anImageto the page.voidaddTemplate(PdfTemplate template, double a, double b, double c, double d, double e, double f) Adds a template to this content using double matrices.voidaddTemplate(PdfTemplate template, float x, float y) Adds a template to this content.voidaddTemplate(PdfTemplate template, float a, float b, float c, float d, float e, float f) Adds a template to this content.voidarc(float x1, float y1, float x2, float y2, float startAng, float extent) Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2, starting at startAng degrees and covering extent degrees.voidStarts the writing of text.static List<float[]> bezierArc(float x1, float y1, float x2, float y2, float startAng, float extent) Generates an array of bezier curves to draw an arc.protected voidCheck if we have a valid PdfWriter.voidCloses the current subpath by appending a straight line segment from the current point to the starting point of the subpath.voidconcatCTM(float a, float b, float c, float d, float e, float f) Concatenate a matrix to the current transformation matrix.createTemplate(float width, float height) Creates a new template.voidcurveTo(float x1, float y1, float x2, float y2, float x3, float y3) Appends a Bêzier curve to the path, starting from the current point.voidellipse(float x1, float y1, float x2, float y2) Draws an ellipse inscribed within the rectangle x1,y1,x2,y2.voidendText()Ends the writing of text and makes the current font invalid.voideoFill()Fills the path, using the even-odd rule to determine the region to fill.voidFills the path, using the even-odd rule to determine the region to fill and strokes it.voidfill()Fills the path, using the non-zero winding number rule to determine the region to fill.voidFills the path using the non-zero winding number rule to determine the region to fill and strokes it.Gets the internal buffer.Gets thePdfDocumentin use by this object.Gets thePdfWriterin use by this object.voidlineTo(float x, float y) Appends a straight line segment from the current point (x, y).voidmoveTo(float x, float y) Move the current point (x, y), omitting any connecting line segment.voidnewPath()Ends the path without filling or stroking it.voidrectangle(float x, float y, float w, float h) Adds a rectangle to the current path.voidAdds a border (complete or partially) to the current path..voidreset()Makes thisPdfContentByteempty.voidreset(boolean validateContent) Makes thisPdfContentByteempty.voidChanges the current gray tint for filling paths to black.voidChanges the current gray tint for stroking paths to black.voidChanges the current color for filling paths to black.voidChanges the current color for stroking paths to black.voidRestores the graphic state.voidroundRectangle(float x, float y, float w, float h, float r) Adds a round rectangle to the current path.voidChecks for any dangling state: Mismatched save/restore state, begin/end text, begin/end layer, or begin/end marked content sequence.voidSaves the graphic state.voidsetColorFill(Color color) Sets the fill color.voidsetColorStroke(Color color) Sets the stroke color.voidsetFontAndSize(BaseFont bf, float size) Set the font and the size for the subsequent text writing.voidsetGrayFill(float gray, float alpha) Changes the currentgray tint for filling paths (device dependent colors!).voidsetGrayStroke(float gray, float alpha) Changes the currentgray tint for stroking paths (device dependent colors!).voidSets the graphic statevoidsetLineCap(int style) Changes the Line cap style.voidsetLineDash(float phase) Changes the value of the line dash pattern.voidsetLineDash(float unitsOn, float unitsOff, float phase) Changes the value of the line dash pattern.voidsetLineJoin(int style) Changes the Line join style.voidsetLineWidth(float w) Changes the line width.voidsetRGBColorFill(int red, int green, int blue, int alpha) Changes the current color for filling paths (device dependent colors!).voidsetRGBColorStroke(int red, int green, int blue, int alpha) Changes the current color for stroking paths (device dependent colors!).voidsetTextMatrix(float x, float y) Changes the text matrix.voidsetTextMatrix(float a, float b, float c, float d, float x, float y) Changes the text matrix.voidShows thetext.voidshowTextBasic(String text) Shows thetext.voidstroke()Strokes the path.byte[]Returns the PDF representation of thisPdfContentByte-object.toString()Returns theStringrepresentation of thisPdfContentByte-object.voidvariableRectangle(Rectangle rect) Adds a variable width border to the current path.
-
Field Details
-
ALIGN_CENTER
public static final int ALIGN_CENTERThe alignment is center- See Also:
-
ALIGN_LEFT
public static final int ALIGN_LEFTThe alignment is left- See Also:
-
ALIGN_RIGHT
public static final int ALIGN_RIGHTThe alignment is right- See Also:
-
LINE_CAP_BUTT
public static final int LINE_CAP_BUTTA possible line cap value- See Also:
-
LINE_JOIN_MITER
public static final int LINE_JOIN_MITERA possible line join value- See Also:
-
This is the actual content
-
writer
This is the writer -
pdf
This is the PdfDocument -
state
protected org.openpdf.text.pdf.PdfContentByte.GraphicState stateThis is the GraphicState in use -
stateList
The list were we save/restore the state -
separator
protected int separatorThe separator between commands.
-
-
Constructor Details
-
PdfContentByte
Constructs a newPdfContentByte-object.- Parameters:
wr- the writer associated to this content
-
-
Method Details
-
bezierArc
public static List<float[]> bezierArc(float x1, float y1, float x2, float y2, float startAng, float extent) Generates an array of bezier curves to draw an arc.(x1, y1) and (x2, y2) are the corners of the enclosing rectangle. Angles, measured in degrees, start with 0 to the right (the positive X axis) and increase counter-clockwise. The arc extends from startAng to startAng+extent. I.e. startAng=0 and extent=180 yields an openside-down semi-circle.
The resulting coordinates are of the form float[]{x1,y1,x2,y2,x3,y3, x4,y4} such that the curve goes from (x1, y1) to (x4, y4) with (x2, y2) and (x3, y3) as their respective Bezier control points.
Note: this code was taken from ReportLab (www.reportlab.org), an excellent PDF generator for Python (BSD license: http://www.reportlab.org/devfaq.html#1.3 ).
- Parameters:
x1- a corner of the enclosing rectangley1- a corner of the enclosing rectanglex2- a corner of the enclosing rectangley2- a corner of the enclosing rectanglestartAng- starting angle in degreesextent- angle extent in degrees- Returns:
- a list of float[] with the bezier curves
-
toString
Returns theStringrepresentation of thisPdfContentByte-object. -
getInternalBuffer
Gets the internal buffer.- Returns:
- the internal buffer
-
toPdf
Returns the PDF representation of thisPdfContentByte-object.- Parameters:
writer- thePdfWriter- Returns:
- a
bytearray with the representation
-
setLineCap
public void setLineCap(int style) Changes the Line cap style.The line cap style specifies the shape to be used at the end of open subpaths when they are stroked.
Allowed values are LINE_CAP_BUTT, LINE_CAP_ROUND and LINE_CAP_PROJECTING_SQUARE.- Parameters:
style- a value
-
setLineDash
public void setLineDash(float phase) Changes the value of the line dash pattern.The line dash pattern controls the pattern of dashes and gaps used to stroke paths. It is specified by an array and a phase. The array specifies the length of the alternating dashes and gaps. The phase specifies the distance into the dash pattern to start the dash.
- Parameters:
phase- the value of the phase
-
setLineDash
public void setLineDash(float unitsOn, float unitsOff, float phase) Changes the value of the line dash pattern.The line dash pattern controls the pattern of dashes and gaps used to stroke paths. It is specified by an array and a phase. The array specifies the length of the alternating dashes and gaps. The phase specifies the distance into the dash pattern to start the dash.
- Parameters:
unitsOn- the number of units that must be 'on'unitsOff- the number of units that must be 'off'phase- the value of the phase
-
setLineJoin
public void setLineJoin(int style) Changes the Line join style.The line join style specifies the shape to be used at the corners of paths that are stroked.
Allowed values are LINE_JOIN_MITER (Miter joins), LINE_JOIN_ROUND (Round joins) and LINE_JOIN_BEVEL (Bevel joins).- Parameters:
style- a value
-
setLineWidth
public void setLineWidth(float w) Changes the line width.The line width specifies the thickness of the line used to stroke a path and is measured in user space units.
- Parameters:
w- a width
-
setGrayFill
public void setGrayFill(float gray, float alpha) Changes the currentgray tint for filling paths (device dependent colors!).Sets the color space to DeviceGray (or the DefaultGray color space), and sets the gray tint to use for filling paths.
- Parameters:
gray- a value between 0 (black) and 1 (white)
-
resetGrayFill
public void resetGrayFill()Changes the current gray tint for filling paths to black. -
setGrayStroke
public void setGrayStroke(float gray, float alpha) Changes the currentgray tint for stroking paths (device dependent colors!).Sets the color space to DeviceGray (or the DefaultGray color space), and sets the gray tint to use for stroking paths.
- Parameters:
gray- a value between 0 (black) and 1 (white)
-
resetGrayStroke
public void resetGrayStroke()Changes the current gray tint for stroking paths to black. -
resetRGBColorFill
public void resetRGBColorFill()Changes the current color for filling paths to black. Resetting using gray color to keep the backward compatibility. -
resetRGBColorStroke
public void resetRGBColorStroke()Changes the current color for stroking paths to black. Resetting using gray color to keep the backward compatibility. -
moveTo
public void moveTo(float x, float y) Move the current point (x, y), omitting any connecting line segment.- Parameters:
x- new x-coordinatey- new y-coordinate
-
lineTo
public void lineTo(float x, float y) Appends a straight line segment from the current point (x, y). The new current point is (x, y).- Parameters:
x- new x-coordinatey- new y-coordinate
-
curveTo
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) Appends a Bêzier curve to the path, starting from the current point.- Parameters:
x1- x-coordinate of the first control pointy1- y-coordinate of the first control pointx2- x-coordinate of the second control pointy2- y-coordinate of the second control pointx3- x-coordinate of the ending point (= new current point)y3- y-coordinate of the ending point (= new current point)
-
rectangle
public void rectangle(float x, float y, float w, float h) Adds a rectangle to the current path.- Parameters:
x- x-coordinate of the starting pointy- y-coordinate of the starting pointw- widthh- height
-
variableRectangle
Adds a variable width border to the current path. Only use ifRectangle.isUseVariableBorders= true.- Parameters:
rect- aRectangle
-
rectangle
Adds a border (complete or partially) to the current path..- Parameters:
rectangle- aRectangle
-
closePath
public void closePath()Closes the current subpath by appending a straight line segment from the current point to the starting point of the subpath. -
newPath
public void newPath()Ends the path without filling or stroking it. -
stroke
public void stroke()Strokes the path. -
fill
public void fill()Fills the path, using the non-zero winding number rule to determine the region to fill. -
eoFill
public void eoFill()Fills the path, using the even-odd rule to determine the region to fill. -
fillStroke
public void fillStroke()Fills the path using the non-zero winding number rule to determine the region to fill and strokes it. -
eoFillStroke
public void eoFillStroke()Fills the path, using the even-odd rule to determine the region to fill and strokes it. -
addImage
Adds anImageto the page. TheImagemust have absolute positioning.- Parameters:
image- theImageobject- Throws:
DocumentException- if theImagedoes not have absolute positioning
-
addImage
Adds anImageto the page. TheImagemust have absolute positioning. The image can be placed inline.- Parameters:
image- theImageobjectinlineImage-trueto place this image inline,falseotherwise- Throws:
DocumentException- if theImagedoes not have absolute positioning
-
addImage
public void addImage(Image image, float a, float b, float c, float d, float e, float f, boolean inlineImage) throws DocumentException Adds anImageto the page. The positioning of theImageis done with the transformation matrix. To position animageat (x,y) use addImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline.- Parameters:
image- theImageobjecta- an element of the transformation matrixb- an element of the transformation matrixc- an element of the transformation matrixd- an element of the transformation matrixe- an element of the transformation matrixf- an element of the transformation matrixinlineImage-trueto place this image inline,falseotherwise- Throws:
DocumentException- on error
-
reset
public void reset()Makes thisPdfContentByteempty. Callsreset( true ) -
reset
public void reset(boolean validateContent) Makes thisPdfContentByteempty.- Parameters:
validateContent- will callsanityCheck()if true.- Since:
- 2.1.6
-
beginText
public void beginText()Starts the writing of text. -
endText
public void endText()Ends the writing of text and makes the current font invalid. -
saveState
public void saveState()Saves the graphic state.saveStateandrestoreStatemust be balanced. -
restoreState
public void restoreState()Restores the graphic state.saveStateandrestoreStatemust be balanced. -
setFontAndSize
Set the font and the size for the subsequent text writing.- Parameters:
bf- the fontsize- the font size in points
-
showText
Shows thetext.- Parameters:
text- the text to write
-
showTextBasic
Shows thetext. LayoutProcessor is not used.- Parameters:
text- the text to write
-
setTextMatrix
public void setTextMatrix(float a, float b, float c, float d, float x, float y) Changes the text matrix.Remark: this operation also initializes the current point position.
- Parameters:
a- operand 1,1 in the matrixb- operand 1,2 in the matrixc- operand 2,1 in the matrixd- operand 2,2 in the matrixx- operand 3,1 in the matrixy- operand 3,2 in the matrix
-
setTextMatrix
public void setTextMatrix(float x, float y) Changes the text matrix. The first four parameters are {1,0,0,1}.Remark: this operation also initializes the current point position.
- Parameters:
x- operand 3,1 in the matrixy- operand 3,2 in the matrix
-
concatCTM
public void concatCTM(float a, float b, float c, float d, float e, float f) Concatenate a matrix to the current transformation matrix.- Parameters:
a- an element of the transformation matrixb- an element of the transformation matrixc- an element of the transformation matrixd- an element of the transformation matrixe- an element of the transformation matrixf- an element of the transformation matrix
-
arc
public void arc(float x1, float y1, float x2, float y2, float startAng, float extent) Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2, starting at startAng degrees and covering extent degrees. Angles start with 0 to the right (+x) and increase counter-clockwise.- Parameters:
x1- a corner of the enclosing rectangley1- a corner of the enclosing rectanglex2- a corner of the enclosing rectangley2- a corner of the enclosing rectanglestartAng- starting angle in degreesextent- angle extent in degrees
-
ellipse
public void ellipse(float x1, float y1, float x2, float y2) Draws an ellipse inscribed within the rectangle x1,y1,x2,y2.- Parameters:
x1- a corner of the enclosing rectangley1- a corner of the enclosing rectanglex2- a corner of the enclosing rectangley2- a corner of the enclosing rectangle
-
createTemplate
Creates a new template.Creates a new template that is nothing more than a form XObject. This template can be included in this
PdfContentByteor in another template. Templates are only written to the output when the document is closed permitting things like showing text in the first page that is only defined in the last page.- Parameters:
width- the bounding box widthheight- the bounding box height- Returns:
- the created template
-
addTemplate
Adds a template to this content.- Parameters:
template- the templatea- an element of the transformation matrixb- an element of the transformation matrixc- an element of the transformation matrixd- an element of the transformation matrixe- an element of the transformation matrixf- an element of the transformation matrix
-
addTemplate
Adds a template to this content.- Parameters:
template- the templatex- the x location of this templatey- the y location of this template
-
addTemplate
public void addTemplate(PdfTemplate template, double a, double b, double c, double d, double e, double f) Adds a template to this content using double matrices.- Parameters:
template- the templatea- an element of the transformation matrixb- an element of the transformation matrixc- an element of the transformation matrixd- an element of the transformation matrixe- an element of the transformation matrixf- an element of the transformation matrix
-
setRGBColorFill
public void setRGBColorFill(int red, int green, int blue, int alpha) Changes the current color for filling paths (device dependent colors!).Sets the color space to DeviceRGB (or the DefaultRGB color space), and sets the color to use for filling paths.
This method is described in the 'Portable Document Format Reference Manual version 1.3' section 8.5.2.1 (page 331).
Following the PDF manual, each operand must be a number between 0 (minimum intensity) and 1 (maximum intensity). This method however accepts only integers between 0x00 and 0xFF.
- Parameters:
red- the intensity of redgreen- the intensity of greenblue- the intensity of blue
-
setRGBColorStroke
public void setRGBColorStroke(int red, int green, int blue, int alpha) Changes the current color for stroking paths (device dependent colors!).Sets the color space to DeviceRGB (or the DefaultRGB color space), and sets the color to use for stroking paths.
This method is described in the 'Portable Document Format Reference Manual version 1.3' section 8.5.2.1 (page 331).
Following the PDF manual, each operand must be a number between 0 (minimum intensity) and 1 (maximum intensity). This method however accepts only integers between 0x00 and 0xFF.- Parameters:
red- the intensity of redgreen- the intensity of greenblue- the intensity of blue
-
setColorStroke
Sets the stroke color.colorcan be anExtendedColor.- Parameters:
color- the color
-
setColorFill
Sets the fill color.colorcan be anExtendedColor.- Parameters:
color- the color
-
checkWriter
protected void checkWriter()Check if we have a valid PdfWriter. -
getPdfWriter
Gets thePdfWriterin use by this object.- Returns:
- the
PdfWriterin use by this object
-
getPdfDocument
Gets thePdfDocumentin use by this object.- Returns:
- the
PdfDocumentin use by this object
-
roundRectangle
public void roundRectangle(float x, float y, float w, float h, float r) Adds a round rectangle to the current path.- Parameters:
x- x-coordinate of the starting pointy- y-coordinate of the starting pointw- widthh- heightr- radius of the arc corner
-
setGState
Sets the graphic state- Parameters:
gstate- the graphic state
-
sanityCheck
public void sanityCheck()Checks for any dangling state: Mismatched save/restore state, begin/end text, begin/end layer, or begin/end marked content sequence. If found, this function will throw. This function is called automatically during a reset() (from Document.newPage() for example), and before writing itself out in toPdf(). One possible cause: not calling myPdfGraphics2D.dispose() will leave dangling saveState() calls.- Throws:
IllegalPdfSyntaxException- (a runtime exception)- Since:
- 2.1.6
-