Some test text!

Search
Hamburger Icon

iOS / Guides / Custom appearances

Create custom annotations on iOS

It's possible to modify an annotation's appearance directly using PDFNet.

The sample code below demonstrates how to replace an annotation's appearance with a custom image.

func setCustomImage(annot:PTAnnot, doc:PTPDFDoc)
{
    // Initialize a new PTElementWriter and PTElementBuilder
    let writer: PTElementWriter =  PTElementWriter()
    let builder: PTElementBuilder = PTElementBuilder()

    writer.writerBegin(with: doc.getSDFDoc()!, compress: true)

    // Initialize the new image
    let image = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "image", ofType: "png"))
    guard let w = image?.getWidth(), let h = image?.getHeight() else {
        print("Unable to get image dimensions")
        return
    }

    // Initialize a new image element
    let element: PTElement = builder.createImage(withCornerAndScale: image, x: 0, y: 0, hscale:Double(w), vscale: Double(h))

    // Write the element
    writer.writePlacedElement(element)

    // Get the bounding box of the new element
    let bbox: PTPDFRect = element.getBBox()

    // Configure the appearance stream that will be written to the annotation
    let new_appearance_stream: PTObj = writer.end()

    // Set the bounding box to be the rect of the new element
    new_appearance_stream.putRect(
        "BBox",
        x1: bbox.getX1(),
        y1: bbox.getY1(),
        x2: bbox.getX2(),
        y2: bbox.getY2())

    // Overwrite the annotation's appearance with the new appearance stream
    annot.setAppearance(new_appearance_stream, annot_state:  e_ptnormal, app_state: nil)
}

In addition to images, an annotation's appearance can be created using (nearly) everything the PDF specification allows, including vector content and text. For an example of how to create PDF vector and text content, see the ElementBuilder sample .

Warning
If you call RefreshAppearance after modifying the appearance of a `PTAnnot`, then the annotation's default appearance will be restored and any changes you have made will be lost.

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales