Class PdfCanvas
Draws the content of one PdfPage.
Coordinates are in points (1/72 inch) with the origin at the top
left of the page and y growing downwards, as in Codename One's
Graphics; text is placed by its baseline. Shapes are built as a path
and then filled, stroked or used as a clip:
PdfDocument doc = new PdfDocument();
PdfPage page = doc.addPage(PdfPageSize.A4);
PdfCanvas c = page.getCanvas();
c.setFillColor(0x1565c0);
c.fillRect(36, 36, page.getWidth() - 72, 40);
c.setFillColor(0xffffff);
c.setFont(PdfFont.HELVETICA_BOLD, 18);
c.drawText("Invoice 2031", 48, 62);
c.setStrokeColor(0x999999);
c.setLineWidth(0.5f);
c.drawLine(36, 90, page.getWidth() - 36, 90);
save() and restore() bracket changes to the color, line style,
transform, clip and font.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intButt line cap: the stroke ends at the end point.static final intRound line cap.static final intSquare line cap: the stroke extends half its width past the end.static final intBeveled line join.static final intMitered line join.static final intRound line join. -
Method Summary
Modifier and TypeMethodDescriptionAppends raw content-stream operators, for what this class does not cover.clip()Intersects the clip with the current path (nonzero winding) and ends the path.Intersects the clip with the current path (even-odd) and ends it.Closes the current subpath with a straight segment to its start.curveTo(float x1, float y1, float x2, float y2, float x3, float y3) Adds a cubic Bezier segment through control points(x1, y1)and(x2, y2)to(x3, y3).Drawsimagescaled into the rectangle(x, y, w, h).drawLine(float x1, float y1, float x2, float y2) Strokes a line from(x1, y1)to(x2, y2).Drawstextin the current font and fill color with its baseline starting at(x, y).DrawstextasdrawText(String, float, float)does, in a paragraph of directiondirection, one of thePdfParagraph.DIRECTION_constants.ellipse(float x, float y, float w, float h) Adds the ellipse inscribed in the given rectangle.fill()Fills the current path (nonzero winding) and ends it.Fills, then strokes the current path, and ends it.Fills the current path with the even-odd rule and ends it.fillRect(float x, float y, float w, float h) Fills a rectangle.getFont()The current font, null beforesetFont(PdfFont, float).floatThe current font size in points.lineTo(float x, float y) Adds a straight segment to(x, y).moveTo(float x, float y) Starts a new subpath at(x, y).quadTo(float cx, float cy, float x, float y) Adds a quadratic Bezier segment through control point(cx, cy)to(x, y).rect(float x, float y, float w, float h) Adds a rectangle as a closed subpath.restore()Restores the state saved by the matchingsave().rotate(double radians) Rotates later drawing byradiansclockwise around the origin.roundRect(float x, float y, float w, float h, float radius) Adds a rectangle with corners rounded toradiuspoints.save()Saves the graphics state: colors, line style, transform, clip and font.scale(float sx, float sy) Scales later drawing bysx,syaround the origin.setDash(float[] pattern, float phase) Sets the dash pattern: alternating dash and gap lengths, startingphasepoints into the pattern.setFillAlpha(float alpha) Sets the opacity of fills and text, from 0 (invisible) to 1.setFillColor(int rgb) Sets the color shapes and text are filled with,0xRRGGBB.Sets the font later text is drawn with.setLineCap(int cap) Sets the line cap, one of theCAP_constants.setLineJoin(int join) Sets the line join, one of theJOIN_constants.setLineWidth(float width) Sets the width of stroked lines in points.setMiterLimit(float limit) Sets the miter limit of mitered joins.setStrokeAlpha(float alpha) Sets the opacity of strokes, from 0 (invisible) to 1.setStrokeColor(int rgb) Sets the color lines are stroked with,0xRRGGBB.stroke()Strokes the current path and ends it.strokeRect(float x, float y, float w, float h) Strokes the outline of a rectangle.transform(float a, float b, float c, float d, float e, float f) Concatenates the matrix[a b c d e f]with the current transform: a point(x, y)maps to(a*x + c*y + e, b*x + d*y + f).translate(float dx, float dy) Moves the origin bydx,dy.
-
Field Details
-
CAP_BUTT
public static final int CAP_BUTTButt line cap: the stroke ends at the end point.- See Also:
-
CAP_ROUND
public static final int CAP_ROUNDRound line cap.- See Also:
-
CAP_SQUARE
public static final int CAP_SQUARESquare line cap: the stroke extends half its width past the end.- See Also:
-
JOIN_MITER
public static final int JOIN_MITERMitered line join.- See Also:
-
JOIN_ROUND
public static final int JOIN_ROUNDRound line join.- See Also:
-
JOIN_BEVEL
public static final int JOIN_BEVELBeveled line join.- See Also:
-
-
Method Details
-
save
Saves the graphics state: colors, line style, transform, clip and font. -
restore
-
translate
Moves the origin bydx,dy. -
scale
Scales later drawing bysx,syaround the origin. -
rotate
Rotates later drawing byradiansclockwise around the origin. -
transform
Concatenates the matrix[a b c d e f]with the current transform: a point(x, y)maps to(a*x + c*y + e, b*x + d*y + f). -
setFillColor
Sets the color shapes and text are filled with,0xRRGGBB. -
setStrokeColor
Sets the color lines are stroked with,0xRRGGBB. -
setFillAlpha
Sets the opacity of fills and text, from 0 (invisible) to 1. -
setStrokeAlpha
Sets the opacity of strokes, from 0 (invisible) to 1. -
setLineWidth
Sets the width of stroked lines in points. -
setLineCap
Sets the line cap, one of theCAP_constants. -
setLineJoin
Sets the line join, one of theJOIN_constants. -
setMiterLimit
Sets the miter limit of mitered joins. -
setDash
Sets the dash pattern: alternating dash and gap lengths, startingphasepoints into the pattern. Null or empty draws solid lines. -
moveTo
Starts a new subpath at(x, y). -
lineTo
Adds a straight segment to(x, y). -
curveTo
Adds a cubic Bezier segment through control points(x1, y1)and(x2, y2)to(x3, y3). -
quadTo
Adds a quadratic Bezier segment through control point(cx, cy)to(x, y). -
closePath
Closes the current subpath with a straight segment to its start. -
rect
Adds a rectangle as a closed subpath. -
roundRect
Adds a rectangle with corners rounded toradiuspoints. -
ellipse
Adds the ellipse inscribed in the given rectangle. -
fill
Fills the current path (nonzero winding) and ends it. -
fillEvenOdd
Fills the current path with the even-odd rule and ends it. -
stroke
Strokes the current path and ends it. -
fillAndStroke
Fills, then strokes the current path, and ends it. -
clip
-
clipEvenOdd
Intersects the clip with the current path (even-odd) and ends it. -
fillRect
Fills a rectangle. -
strokeRect
Strokes the outline of a rectangle. -
drawLine
Strokes a line from(x1, y1)to(x2, y2). -
setFont
-
getFont
The current font, null beforesetFont(PdfFont, float). -
getFontSize
public float getFontSize()The current font size in points. -
drawText
Draws
textin the current font and fill color with its baseline starting at(x, y). Line breaks are not interpreted; lay out several lines withPdfLayout.Text is given in reading order. Right-to-left scripts (Hebrew, Arabic) are reordered for display by the Unicode bidirectional algorithm, with the direction taken from the first strong character, and Arabic letters are joined, using the font's own contextual forms when it is a TrueType font that has them.
-
drawText
DrawstextasdrawText(String, float, float)does, in a paragraph of directiondirection, one of thePdfParagraph.DIRECTION_constants. The text still starts atx. -
drawImage
-
appendRaw
-