XMP annotations in C# .NET
Nutrient .NET SDK (formerly GdPicture.NET) enables you to create XMP annotations with the XML-based syntax. They can be added to PDF, JPG, and TIFF files. The XMP annotations are handled by the AnnotationManager class. The list below shows all possible XMP annotation types:
- Text annotation
- Rubber stamp
- Geometric — An annotation represented as a geometric figure (line, arrow, ellipse, polygon, and so on).
- Free hand — A freely drawn line.
- Comment annotation — An annotation linked to an already existing annotation.
- Sticky note — A text annotation confined in a colored rectangular area with borders.
Adding an XMP annotation
To add an XMP annotation, follow the steps below:
- Create an
AnnotationManagerobject. - Load a file to the
AnnotationManagerobject. For more information, refer to the guide on loading a file to theAnnotationManagerobject. - Optional: Specify the page where to add the XMP annotation with the
SelectPagemethod. This step is only required for PDF and TIFF files. - Use any method from the
AnnotationManagerclass that starts withAdd...to add an XMP annotation. Alternatively, create a new annotation by using the specific annotation class — for example, theAnnotationEllipseclass. - Optional: Set the XMP annotation’s properties.
- Save the newly created annotation to the page with the
SaveAnnotationsToPagemethod. - Optional: Flatten the XMP annotation into the file with the
BurnAnnotationsToPagemethod. Flattened annotations are no longer editable. - Save the file. For more information, refer to the guide on saving a file from the
AnnotationManagerobject.
To add an ellipse XMP annotation and flatten it into a JPG file, use the following code:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;using GdPicture14.Annotations;using GdPicture14.Imaging;
LicenseManager license = new LicenseManager();license.RegisterKEY("");{using AnnotationManager annotationManager = new AnnotationManager();// Load an image to the `AnnotationManager` object.annotationManager.InitFromFile(@"input.jpg");// Create a new `AnnotationEllipse` object.AnnotationEllipse annotEllipse = annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2);// Set the `Author` property of the annotation.annotEllipse.Author = "Nutrient";// Save the annotation to the image.annotationManager.SaveAnnotationsToPage();// Flatten the annotation.annotationManager.BurnAnnotationsToPage(false);// Save the image with the annotation to a file.annotationManager.SaveDocumentToJPEG(@"output.jpg", 75);}Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14Imports GdPicture14.AnnotationsImports GdPicture14.Imaging
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using annotationManager As AnnotationManager = New AnnotationManager() ' Load an image to the `AnnotationManager` object. annotationManager.InitFromFile("input.jpg") ' Create a new `AnnotationEllipse` object. Dim annotEllipse As AnnotationEllipse = annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2) ' Set the `Author` property of the annotation. annotEllipse.Author = "Nutrient" ' Save the annotation to the image. annotationManager.SaveAnnotationsToPage() ' Flatten the annotation. annotationManager.BurnAnnotationsToPage(False) ' Save the image with the annotation to a file. annotationManager.SaveDocumentToJPEG("output.jpg", 75)End UsingXMP annotation object properties
Each XMP annotation contains generic annotation properties and sometimes contains properties specific to the annotation type. To get these properties, use the GetAnnotationPropertyValue method. To modify them, use the SetAnnotationPropertyValue method.
If you created an XMP annotation with the specific annotation class — for example, the AnnotationEllipse class — you can access its properties directly from the used annotation object.
To set the author of a newly created XMP annotation, use the following code:
{using AnnotationManager annotationManager = new AnnotationManager();// Load an image to the `AnnotationManager` object.annotationManager.InitFromFile(@"input.jpg");// Add an ellipse annotation.annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2);// Set the `Author` property of the annotation.annotationManager.SetAnnotationPropertyValue(0, "Author", "Nutrient");// Save the annotation to the image.annotationManager.SaveAnnotationsToPage();// Save the image with the annotation to a file.annotationManager.SaveDocumentToJPEG(@"output.jpg", 75);}Using annotationManager As AnnotationManager = New AnnotationManager() ' Load an image to the `AnnotationManager` object. annotationManager.InitFromFile("input.jpg") ' Add an ellipse annotation. annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2) ' Set the `Author` property of the annotation. annotationManager.SetAnnotationPropertyValue(0, "Author", "Nutrient") ' Save the annotation to the image. annotationManager.SaveAnnotationsToPage() ' Save the image with the annotation to a file. annotationManager.SaveDocumentToJPEG("output.jpg", 75)End UsingFinding an existing XMP annotation
There are three different ways to find an existing XMP annotation:
- Get its index number.
- Get the whole XMP annotation object.
- Get the annotation’s XML content in a string form.
The method you use depends on which operations you perform on the annotation.
Getting the index number
To get the XMP annotation’s index number, follow the steps below:
- Create an
AnnotationManagerobject. - Load a file to the
AnnotationManagerobject. For more information, refer to the guide on loading a file to theAnnotationManagerobject. - Optional: Specify the page where to add the XMP annotation with the
SelectPagemethod. This step is only required for PDF and TIFF files. - Get the total amount of annotations with the
GetAnnotationCountmethod. - Loop through all annotations.
- Find the annotation that meets your search parameters. For example, use the
GetAnnotationTypeor theGetAnnotationPropertyValuemethods.
{using AnnotationManager annotationManager = new AnnotationManager();// Load an image to the `AnnotationManager` object.annotationManager.InitFromFile(@"input_xmp_annotations.jpg");// Get the total amount of XMP annotations.int annotCount = annotationManager.GetAnnotationCount();for(int i = 0; i < annotCount; i++){ // Check if the current annotation is a rubber stamp. if(annotationManager.GetAnnotationType(i) == Annotation.GdPictureAnnotationType.AnnotationTypeRubberStamp) { // Get the current annotation index number. Console.WriteLine("Index Number: {0}", i); }}}Using annotationManager As AnnotationManager = New AnnotationManager() ' Load an image to the `AnnotationManager` object. annotationManager.InitFromFile("input_xmp_annotations.jpg") ' Get the total amount of XMP annotations. Dim annotCount As Integer = annotationManager.GetAnnotationCount() For i = 0 To annotCount - 1 ' Check if the current annotation is a rubber stamp. If annotationManager.GetAnnotationType(i) = Annotation.GdPictureAnnotationType.AnnotationTypeRubberStamp Then ' Get the current annotation index number. Console.WriteLine("Index Number: {0}", i) End If NextEnd UsingGetting the XMP annotation object
To get an existing XMP annotation object, use the GetAnnotationFromIdx method. It requires the annotation’s index number.
The following example changes the author property of all rubber stamp annotations and saves the image:
{using AnnotationManager annotationManager = new AnnotationManager();// Load an image to the `AnnotationManager` object.annotationManager.InitFromFile(@"input_xmp_annotations.jpg");// Get the total amount of XMP annotations.int annotCount = annotationManager.GetAnnotationCount();for(int i = 0; i < annotCount; i++){ // Check if the current annotation is a rubber stamp. if(annotationManager.GetAnnotationType(i) == Annotation.GdPictureAnnotationType.AnnotationTypeRubberStamp) { // Save the current annotation to an object. Annotation annot = annotationManager.GetAnnotationFromIdx(i); // Change the `Author` property. annot.Author = "Nutrient"; }}// Save the annotations to the image.annotationManager.SaveAnnotationsToPage();// Save the image with the annotations to a file.annotationManager.SaveDocumentToJPEG(@"output.jpg", 75);}Using annotationManager As AnnotationManager = New AnnotationManager() ' Load an image to the `AnnotationManager` object. annotationManager.InitFromFile("input_xmp_annotations.jpg") ' Get the total amount of XMP annotations. Dim annotCount As Integer = annotationManager.GetAnnotationCount() For i = 0 To annotCount - 1 ' Check if the current annotation is a rubber stamp. If annotationManager.GetAnnotationType(i) = Annotation.GdPictureAnnotationType.AnnotationTypeRubberStamp Then ' Save the current annotation to an object. Dim annot As Annotation = annotationManager.GetAnnotationFromIdx(i) ' Change the `Author` property. annot.Author = "Nutrient" End If Next ' Save the annotations to the image. annotationManager.SaveAnnotationsToPage() ' Save the image with the annotations to a file. annotationManager.SaveDocumentToJPEG("output.jpg", 75)End UsingGetting the annotation XML content
To get the annotation’s XML content, use the GetAnnotationXML method. It requires the annotation’s index number.
The following example adds an ellipse annotation to the first page of a PDF document, gets its XML content, and creates new annotations on the rest of the pages based on the copied XML code:
{using AnnotationManager annotationManager = new AnnotationManager();// Load a PDF document to the `AnnotationManager` object.annotationManager.InitFromFile(@"input.pdf");// Select the first PDF page.annotationManager.SelectPage(1);// Add an ellipse annotation to copy.annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2);annotationManager.SaveAnnotationsToPage();// Get the total number of XMP annotations.int annotCount = annotationManager.GetAnnotationCount();string annotXML = "";for (int i = 0; i < annotCount; i++){ // Check if the current annotation is of the ellipse type. if (annotationManager.GetAnnotationType(i) == Annotation.GdPictureAnnotationType.AnnotationTypeEllipse) { // Get the XML content of the current annotation. annotXML = annotationManager.GetAnnotationXML(i); break; }}// Get the PDF page count.int pageCount = annotationManager.PageCount;// Loop through all PDF pages.for (int p= 2; p <= pageCount; p++){ // Select the next page. annotationManager.SelectPage(p); // Add the annotation from XML. annotationManager.AddAnnotationFromXML(annotXML); // Save the annotation to the current page. annotationManager.SaveAnnotationsToPage();}annotationManager.SaveDocumentToPDF(@"output.pdf");}Using annotationManager As AnnotationManager = New AnnotationManager() ' Load a PDF document to the `AnnotationManager` object. annotationManager.InitFromFile("input.pdf") ' Select the first PDF page. annotationManager.SelectPage(1) ' Add an ellipse annotation to copy. annotationManager.AddEllipseAnnot(GdPictureColor.Aqua, 2, 1, 4, 2) annotationManager.SaveAnnotationsToPage() ' Get the total amount of XMP annotations. Dim annotCount As Integer = annotationManager.GetAnnotationCount() Dim annotXML = "" For i = 0 To annotCount - 1 ' Check if the current annotation is of the ellipse type. If annotationManager.GetAnnotationType(i) = Annotation.GdPictureAnnotationType.AnnotationTypeEllipse Then ' Get the XML content of the current annotation. annotXML = annotationManager.GetAnnotationXML(i) Exit For End If Next ' Get the PDF page count. Dim pageCount As Integer = annotationManager.PageCount ' Loop through all PDF pages. For p = 2 To pageCount ' Select the next page. annotationManager.SelectPage(p) ' Add the annotation from XML. annotationManager.AddAnnotationFromXML(annotXML) ' Save the annotation to the current page. annotationManager.SaveAnnotationsToPage() Next annotationManager.SaveDocumentToPDF("output.pdf")End Using