PdfCanvas

interface PdfCanvas

Platform-agnostic 2D drawing surface for one PDF page.

A PdfCanvas is obtained from PdfDriver.beginPage and is valid until the matching PdfDriver.endPage call. All coordinates are in PDF points and use a top-left origin (Y grows downward) — the platform implementations take care of flipping where the native context uses bottom-left.

The interface is deliberately small. New primitive operations should be added here only when they cannot be expressed as a composition of existing ones, and every addition must be implemented on every platform backend.

Functions

Link copied to clipboard
open fun beginTransparencyGroup(alpha: Float)

Starts a transparency group: subsequent drawing is composited at alpha opacity until the matching endTransparencyGroup. Backends without group support default to a no-op (content draws opaque).

Link copied to clipboard
open fun bookmark(title: String, level: Int, y: Float)

Adds an entry to the document's outline (the bookmark sidebar in PDF readers) pointing at the given vertical position on the current page. level nests entries: 0 is a top-level chapter, 1 a section inside the previous level-0 entry, and so on.

Link copied to clipboard
abstract fun clipPath(commands: List<PathCommand>)

Constrains all subsequent drawing to the area inside commands. Used by container decoration when the corners cannot be expressed as a single uniform cornerRadius. The path is closed implicitly; winding rule is non-zero (the platform default).

Link copied to clipboard
abstract fun clipRect(x: Float, y: Float, width: Float, height: Float)

Constrains all subsequent drawing to the given rectangle. Effective until the next restoreState call. Wrap calls in saveState / restoreState to scope clipping to a region of code.

Link copied to clipboard
abstract fun clipRoundedRect(x: Float, y: Float, width: Float, height: Float, cornerRadius: Float)

Like clipRect but with rounded corners.

Link copied to clipboard
abstract fun drawImage(bytes: ByteArray, x: Float, y: Float, width: Float, height: Float, contentScale: ContentScale, sourceTop: Float = 0.0f, sourceBottom: Float = 1.0f, allowDownScale: Boolean = true, altText: String? = null)

Draws a bitmap, optionally embedding only a vertical window of the source — sourceTop / sourceBottom are normalized (0..1) offsets used by the page-break slicer to continue a tall image across pages.

Link copied to clipboard
abstract fun drawLine(x1: Float, y1: Float, x2: Float, y2: Float, color: PdfColor, thickness: Float, style: LineStyle = LineStyle.Solid)

Strokes a straight line between two points.

Link copied to clipboard
abstract fun drawPath(commands: List<PathCommand>, fill: PdfPaint?, strokeColor: PdfColor?, strokeWidth: Float)

Draws a vector path defined by a sequence of PathCommands.

Link copied to clipboard
abstract fun drawRect(x: Float, y: Float, width: Float, height: Float, color: PdfColor)

Fills an axis-aligned rectangle.

Link copied to clipboard
abstract fun drawRoundedRect(x: Float, y: Float, width: Float, height: Float, cornerRadius: Float, color: PdfColor)

Fills an axis-aligned rectangle whose four corners are rounded with the given cornerRadius. A radius of zero is equivalent to drawRect.

Link copied to clipboard
abstract fun drawText(text: String, x: Float, y: Float, style: TextStyle)

Draws a single line of text starting at the given top-left position. Newline characters are not interpreted — pass already- wrapped lines from the layout engine.

Link copied to clipboard

Closes the group opened by beginTransparencyGroup.

Link copied to clipboard
open fun formCheckBox(name: String, x: Float, y: Float, size: Float, checked: Boolean)

Records an interactive AcroForm checkbox covering the given square.

Link copied to clipboard
open fun formTextField(name: String, x: Float, y: Float, width: Float, height: Float, value: String, multiline: Boolean, fontSizePt: Float)

Records an interactive AcroForm text input field covering the given rectangle.

Link copied to clipboard
open fun linkAnnotation(x: Float, y: Float, width: Float, height: Float, url: String)

Records a hyperlink annotation covering the given rectangle and pointing at url.

Link copied to clipboard
open fun linkToDestination(name: String, x: Float, y: Float, width: Float, height: Float)

Records an internal go-to link covering the given rectangle and jumping to the namedDestination registered under name. Forward references are fine — backends resolve names when the document is finished. Links whose name is never registered are silently inert.

Link copied to clipboard
open fun namedDestination(name: String, y: Float)

Registers a named destination at the given vertical position on the current page. Internal links created with linkToDestination (possibly on other pages, before or after this one) jump here.

Link copied to clipboard
abstract fun restoreState()

Restores the canvas state most recently pushed with saveState.

Link copied to clipboard
open fun rotate(degrees: Float, pivotX: Float, pivotY: Float)

Rotates all subsequent drawing by degrees (clockwise, in the top-left coordinate space) around the pivot point. Effective until the next restoreState — always wrap in saveState / restoreState pairs, which is what the renderer does for rotated containers.

Link copied to clipboard
abstract fun saveState()

Pushes the current canvas state (transform, clip) onto an internal stack. Pair every call with restoreState; mismatched pairs leak state across draw calls and produce garbled output.

Link copied to clipboard
abstract fun strokeRect(x: Float, y: Float, width: Float, height: Float, color: PdfColor, thickness: Float)

Strokes an axis-aligned rectangle's outline.

Link copied to clipboard
abstract fun strokeRoundedRect(x: Float, y: Float, width: Float, height: Float, cornerRadius: Float, color: PdfColor, thickness: Float)

Strokes an axis-aligned rounded-rectangle's outline.