PdfEditor
Writer for edits to an existing PDF. Open one with PdfDocument.edit.
val editor = doc.edit()
editor.setInfo(title = "New Title")
editor.stampPage(doc.pages[0]) {
setFillRgb(0.8, 0.1, 0.1)
text(StandardFont.HelveticaBold, 48.0, 120.0, 400.0, "DRAFT")
}
val bytes = editor.saveIncremental()Two save modes:
saveIncremental appends changes to the original bytes (ISO 32000-1 §7.5.6). The original content is preserved verbatim, only the new/changed objects, a fresh xref section and a trailer pointing back via
/Prevare written at the end. This is the right mode for ordinary edits and the foundation for digital signing (which signs the appended byte range).saveRewritten writes a fresh, garbage-collected file containing only objects reachable from the catalog, with edits applied and unreachable objects dropped. Required for redaction, since removed content is truly gone rather than retained in the original byte prefix.
Editing encrypted documents is not yet supported (the writer would need to encrypt newly written strings/streams to match the security handler).
Functions
Stage a new /FlateDecode-compressed stream from uncompressed data (see PdfStreams.flate); returns the reference to it.
Stage a brand-new indirect object; returns the reference to it.
Reserve the next free object number (generation 0).
Rewrite page's content stream by transforming its parsed operations. The page's decoded content is parsed (see ContentStreamParser), passed to transform, re-serialized, written as a new compressed stream, and the page's /Contents is repointed at it. The original content objects are left in place (orphaned) per incremental-update semantics.
Redact a single rectangular region of page (see redactRegions).
Redact rectangular regions of page (rectangles in page user space).
Remove all text-showing operations (Tj, TJ, ', ") from page.
Produce the updated document bytes: original + appended objects + new xref section + trailer. When nothing was staged or overridden this is a verbatim copy of the original.
Serialize a fresh, self-contained PDF containing only the objects reachable from the catalog (and /Info), with staged edits applied and objects renumbered densely. Unlike saveIncremental, the original bytes are NOT retained and unreachable objects (e.g. content streams replaced by an edit) are dropped — which is what makes it the correct vehicle for redaction (the removed content is truly gone, not just superseded).
Set document metadata (/Info). Only non-null fields are changed; any existing /Info entries (standard or custom) are preserved. Updates the existing /Info object in place if the trailer references one, otherwise creates a new /Info object and points the trailer at it. Returns the reference to the (possibly newly created) /Info dictionary.
Fill a text form field: set its value (/V) and regenerate the widget's normal appearance (/AP /N) so the value is visible in any viewer. The appearance honours the field's /DA (font, size, colour) and is clipped to the field rectangle. Also clears the form's /NeedAppearances flag (if set) so viewers use the appearance we just generated.
Set or replace a trailer entry (e.g. /Root, /Info) in the new section.
Overlay content onto an existing page (a stamp/watermark) drawn by block. The existing content is preserved (wrapped in q/Q so its graphics state can't leak into the overlay), the overlay is appended in its own q/Q, and any standard fonts the overlay uses are merged into the page's /Resources under fresh, non-colliding names.
Stage a replacement for an existing object (keeps ref's generation).