Some test text!

Search
Hamburger Icon

Xamarin / Guides / Custom annotation appearance

Create custom annotations in Xamarin

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.

public void SetCustomImage(Annot annot, PDFDoc doc, string imagePath)
{
    // Initialize a new ElementWriter and ElementBuilder
    ElementWriter writer = new ElementWriter();
    ElementBuilder builder = new ElementBuilder();

    writer.Begin(doc.GetSDFDoc(), true);

    // Initialize the new image
    Image image = Image.Create(doc.GetSDFDoc(), imagePath);
    int w = image.GetImageWidth();
    int h = image.GetImageHeight();

    // Initialize a new image element
    Element element = builder.CreateImage(image, 0, 0, w, h);

    // Write the element
    writer.WritePlacedElement(element);

    // Get the bounding box of the new element
    Rect bbox = new Rect();
    element.GetBBox(bbox);

    // Configure the appearance stream that will be written to the annotation
    Obj new_appearance_stream = writer.End();

    // Set the bounding box to be the rect of the new element
    new_appearance_stream.PutRect(
        "BBox",
        bbox.x1,
        bbox.y1,
        bbox.x2,
        bbox.y2);

    // Overwrite the annotation's appearance with the new appearance stream
    annot.SetAppearance(new_appearance_stream);
}

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.

Get the answers you need: Chat with us