Annotate on Images in C# .NET
Adding annotations to images is possible using XMP annotations with the XML-based syntax. 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.
To learn more about XMP annotations, refer to the XMP annotations guide.
To add an XMP annotation to an image, use the following steps:
-
Create an
AnnotationManager
object. -
Load an image to the
AnnotationManager
object with theInitFromFile
method. -
Use any method from the
AnnotationManager
class that starts withAdd...
to add an XMP annotation. Alternatively, create a new annotation by using the specific annotation class — for example, theAnnotationRubberStamp
class. -
Optional: Set the XMP annotation’s properties. For more information, refer to the guide covering XMP annotation object properties.
-
Save the newly created annotation with the
SaveAnnotationsToPage
method. -
Optional: Flatten the XMP annotation into the image with the
BurnAnnotationsToPage
method. Flattened annotations are no longer editable. -
Save the image with the
SaveDocumentToJPEG
method.
To add a stamp annotation and flatten it into an image, use the following code:
using AnnotationManager annotationManager = new AnnotationManager(); // Load an image to the `AnnotationManager` object. annotationManager.InitFromFile(@"C:\temp\source.jpg"); // Create a new `AnnotationEllipse` object. AnnotationRubberStamp annotStamp = annotationManager.AddRubberStampAnnot(GdPictureColor.Blue, 2, 1, 4, 2, "APPROVED"); // Rotate the annotation clockwise by 30 degrees. annotStamp.Rotation = -30; // Set the `Author` property of the annotation. annotStamp.Author = "PSPDFKit"; // 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(@"C:\temp\output.jpg", 75);
Using annotationManager As AnnotationManager = New AnnotationManager() ' Load an image to the `AnnotationManager` object. annotationManager.InitFromFile("C:\temp\source.jpg") ' Create a new `AnnotationEllipse` object. Dim annotStamp As AnnotationRubberStamp = annotationManager.AddRubberStampAnnot(GdPictureColor.Blue, 2, 1, 4, 2, "APPROVED") ' Rotate the annotation clockwise by 30 degrees. annotStamp.Rotation = -30 ' Set the `Author` property of the annotation. annotStamp.Author = "PSPDFKit" ' 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("C:\temp\output.jpg", 75) End Using