Class PdfCanvas

java.lang.Object
com.codename1.pdf.PdfCanvas

public final class PdfCanvas extends Object

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 Details

    • CAP_BUTT

      public static final int CAP_BUTT
      Butt line cap: the stroke ends at the end point.
      See Also:
    • CAP_ROUND

      public static final int CAP_ROUND
      Round line cap.
      See Also:
    • CAP_SQUARE

      public static final int CAP_SQUARE
      Square line cap: the stroke extends half its width past the end.
      See Also:
    • JOIN_MITER

      public static final int JOIN_MITER
      Mitered line join.
      See Also:
    • JOIN_ROUND

      public static final int JOIN_ROUND
      Round line join.
      See Also:
    • JOIN_BEVEL

      public static final int JOIN_BEVEL
      Beveled line join.
      See Also:
  • Method Details

    • save

      public PdfCanvas save()
      Saves the graphics state: colors, line style, transform, clip and font.
    • restore

      public PdfCanvas restore()
      Restores the state saved by the matching save().
    • translate

      public PdfCanvas translate(float dx, float dy)
      Moves the origin by dx, dy.
    • scale

      public PdfCanvas scale(float sx, float sy)
      Scales later drawing by sx, sy around the origin.
    • rotate

      public PdfCanvas rotate(double radians)
      Rotates later drawing by radians clockwise around the origin.
    • transform

      public PdfCanvas 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).
    • setFillColor

      public PdfCanvas setFillColor(int rgb)
      Sets the color shapes and text are filled with, 0xRRGGBB.
    • setStrokeColor

      public PdfCanvas setStrokeColor(int rgb)
      Sets the color lines are stroked with, 0xRRGGBB.
    • setFillAlpha

      public PdfCanvas setFillAlpha(float alpha)
      Sets the opacity of fills and text, from 0 (invisible) to 1.
    • setStrokeAlpha

      public PdfCanvas setStrokeAlpha(float alpha)
      Sets the opacity of strokes, from 0 (invisible) to 1.
    • setLineWidth

      public PdfCanvas setLineWidth(float width)
      Sets the width of stroked lines in points.
    • setLineCap

      public PdfCanvas setLineCap(int cap)
      Sets the line cap, one of the CAP_ constants.
    • setLineJoin

      public PdfCanvas setLineJoin(int join)
      Sets the line join, one of the JOIN_ constants.
    • setMiterLimit

      public PdfCanvas setMiterLimit(float limit)
      Sets the miter limit of mitered joins.
    • setDash

      public PdfCanvas setDash(float[] pattern, float phase)
      Sets the dash pattern: alternating dash and gap lengths, starting phase points into the pattern. Null or empty draws solid lines.
    • moveTo

      public PdfCanvas moveTo(float x, float y)
      Starts a new subpath at (x, y).
    • lineTo

      public PdfCanvas lineTo(float x, float y)
      Adds a straight segment to (x, y).
    • curveTo

      public PdfCanvas 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).
    • quadTo

      public PdfCanvas quadTo(float cx, float cy, float x, float y)
      Adds a quadratic Bezier segment through control point (cx, cy) to (x, y).
    • closePath

      public PdfCanvas closePath()
      Closes the current subpath with a straight segment to its start.
    • rect

      public PdfCanvas rect(float x, float y, float w, float h)
      Adds a rectangle as a closed subpath.
    • roundRect

      public PdfCanvas roundRect(float x, float y, float w, float h, float radius)
      Adds a rectangle with corners rounded to radius points.
    • ellipse

      public PdfCanvas ellipse(float x, float y, float w, float h)
      Adds the ellipse inscribed in the given rectangle.
    • fill

      public PdfCanvas fill()
      Fills the current path (nonzero winding) and ends it.
    • fillEvenOdd

      public PdfCanvas fillEvenOdd()
      Fills the current path with the even-odd rule and ends it.
    • stroke

      public PdfCanvas stroke()
      Strokes the current path and ends it.
    • fillAndStroke

      public PdfCanvas fillAndStroke()
      Fills, then strokes the current path, and ends it.
    • clip

      public PdfCanvas clip()
      Intersects the clip with the current path (nonzero winding) and ends the path. Bracket with save() and restore() to undo.
    • clipEvenOdd

      public PdfCanvas clipEvenOdd()
      Intersects the clip with the current path (even-odd) and ends it.
    • fillRect

      public PdfCanvas fillRect(float x, float y, float w, float h)
      Fills a rectangle.
    • strokeRect

      public PdfCanvas strokeRect(float x, float y, float w, float h)
      Strokes the outline of a rectangle.
    • drawLine

      public PdfCanvas drawLine(float x1, float y1, float x2, float y2)
      Strokes a line from (x1, y1) to (x2, y2).
    • setFont

      public PdfCanvas setFont(PdfFont font, float size)
      Sets the font later text is drawn with.
    • getFont

      public PdfFont getFont()
      The current font, null before setFont(PdfFont, float).
    • getFontSize

      public float getFontSize()
      The current font size in points.
    • drawText

      public PdfCanvas drawText(String text, float x, float y)

      Draws text in the current font and fill color with its baseline starting at (x, y). Line breaks are not interpreted; lay out several lines with PdfLayout.

      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

      public PdfCanvas drawText(String text, float x, float y, int direction)
      Draws text as drawText(String, float, float) does, in a paragraph of direction direction, one of the PdfParagraph.DIRECTION_ constants. The text still starts at x.
    • drawImage

      public PdfCanvas drawImage(PdfImage image, float x, float y, float w, float h)
      Draws image scaled into the rectangle (x, y, w, h).
    • appendRaw

      public PdfCanvas appendRaw(String operators)
      Appends raw content-stream operators, for what this class does not cover. The text must be valid PDF content in the page's flipped coordinate system; nothing checks it.