Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Edit Annotations ​

The steps to edit an annotation are as follows:

  1. Obtain the page object where the annotation needs to be edited.

  2. Access the list of annotations on that page.

  3. Locate the desired annotation in the annotation list and convert it.

  4. Set the properties to the annotation object.

  5. Update the annotation appearance to display it on the document.

This example shows how to edit annotations:

swift
var annotations: [CPDFAnnotation] = []

for i in 0..<document?.pageCount ?? 0 {
    // Iterate through all pages.
    if let page = document?.page(at: i) {
        // Iterate through all the annotations on the page.
        annotations += page.annotations
        for annotation in annotations {
            // Process every annotations here.
        }
    }
}
objective-c
NSMutableArray *annotations = [NSMutableArray array];
for (int i=0; i<document.pageCount; i++) {
    // Loop through all anntation
    CPDFPage *page = [document pageAtIndex:i];
    [annotations addObjectsFromArray:[page annotations]];
    for (CPDFAnnotation *annotation in annotations) {
        
    }
}

Control visibility, printing, and editing with flags ​

CPDFAnnotationFlags is a bitmask. Use the individual setters to change one setting; setFlags(_:) replaces the entire mask.

FlagEffect when set
HiddenNeither displayed, hit-tested, nor printed, even if Print is set.
NoViewNeither displayed nor hit-tested on screen. Can still print if Print is set and Hidden is clear.
PrintAllows printing. Clearing this bit does not change screen visibility. On iOS use isPrint() / setPrint(_:), not the legacy shouldPrint / setShouldPrint: selectors.
ReadOnlyAn ordinary annotation cannot be selected, moved, resized, deleted, or edited, including its contents. Read-only links still allow navigation. For widgets, use the form field's own read-only setting.
LockedUsers can select the annotation and edit its contents, but cannot move, resize, delete, or change its other properties.
LockedContentsPrevents edits to the contents only. By itself it does not prevent moving, resizing, deleting, or changing other properties.

The last three flags restrict user actions in Swift Tools, not writes made by your app through the SDK. Restrictions combine: an ordinary annotation with both Locked and LockedContents remains selectable, but even its contents cannot be edited.

Migrating to 3.2.1: Previously setHidden(true) set NoView. It now sets Hidden, preserves other bits, and suppresses printing. isHidden() returns true if either bit is set; setHidden(false) clears both. For screen-only hiding, use setNoView(_:). Like setPrint(_:) and setLockedContents(_:), it changes only its own bit.

swift
// Make an annotation print-only without changing its lock flags.
annotation.setHidden(false)
annotation.setNoView(true)
annotation.setPrint(true)

Store application data ​

Use the following fields on an annotation that still belongs to a page. They persist when the PDF is saved and do not change its appearance.

FieldHow to write and read it
StringsetCustomValue(_:forKey:) stores UTF-8 text; customValue(forKey:) reads it. Writing "" clears the value but keeps the key. Both an empty value and a missing key read as nil.
Array of stringssetCustomArray(_:forKey:) replaces the array rather than appending. Pass [] to clear it; customArray(forKey:) returns [] if no array is stored. For numbers, booleans, nested arrays, or dictionaries, serialize the data to a string such as JSON instead.
extensionInfoA fixed SDK text field, suitable for JSON that your app parses. Assigning replaces the whole field; read and merge first if multiple modules share it. Set nil or "" to clear it; it then reads as nil. Do not access it with the custom key methods: the encodings differ.

Prefix keys with your app's name (for example, Review_Status). Do not use empty keys, a leading /, standard PDF annotation or form keys (such as Rect, Contents, AP, or V), or the reserved prefixes CPDF and PSO. Neither keys nor values (including array elements) may contain NUL: text is truncated there. The SDK does not check reserved names; overwriting a standard key may damage the annotation. String and array setters return false for an empty key or an annotation that no longer belongs to a page. Keys cannot be removed or enumerated.

swift
func markForReview(_ annotation: CPDFAnnotation) -> Bool {
    guard annotation.setCustomValue("pending", forKey: "Review_Status"),
          annotation.setCustomArray(["legal", "finance"], forKey: "Review_Teams") else {
        return false
    }
    annotation.extensionInfo = #"{"schema":1}"#
    return annotation.customValue(forKey: "Review_Status") == "pending"
        && annotation.customArray(forKey: "Review_Teams").contains("legal")
}