Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

iOS / Guides / Custom appearances


PDFTron is now Apryse, learn more here.

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")

    // 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

    // 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
        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 .

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.

Get the answers you need: Support