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.

1func setCustomImage(annot:PTAnnot, doc:PTPDFDoc)
2{
3 // Initialize a new PTElementWriter and PTElementBuilder
4 let writer: PTElementWriter = PTElementWriter()
5 let builder: PTElementBuilder = PTElementBuilder()
6 writer.writerBegin(with: doc.getSDFDoc()!, compress: true)
7
8 // Initialize the new image
9 let image = PTImage.create(doc.getSDFDoc(), filename: Bundle.main.path(forResource: "image", ofType: "png"))
10 guard let w = image?.getWidth(), let h = image?.getHeight() else {
11 print("Unable to get image dimensions")
12 return
13 }
14
15 // Initialize a new image element
16 let element: PTElement = builder.createImage(withCornerAndScale: image, x: 0, y: 0, hscale:Double(w), vscale: Double(h))
17
18 // Write the element
19 writer.writePlacedElement(element)
20
21 // Get the bounding box of the new element
22 let bbox: PTPDFRect = element.getBBox()
23
24 // Configure the appearance stream that will be written to the annotation
25 let new_appearance_stream: PTObj = writer.end()
26
27 // Set the bounding box to be the rect of the new element
28 new_appearance_stream.putRect(
29 "BBox",
30 x1: bbox.getX1(),
31 y1: bbox.getY1(),
32 x2: bbox.getX2(),
33 y2: bbox.getY2())
34
35 // Overwrite the annotation's appearance with the new appearance stream
36 annot.setAppearance(new_appearance_stream, annot_state: e_ptnormal, app_state: nil)
37}

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.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales