An annotation is used to mark or highlight texts, images and other visual elements on a page. Annotations can be text, image, shape, sound or even file attachments. The purpose of using annotation is to simply associate information or a note with an item on a page. A number of annotations can be displayed either in open or closed state. In the closed state, they appear on the page as a note, icon, or a box, depending on the annotation type. In the opened state, these annotations display the associated object such as a pop-up window containing associated text. For more information on annotations and its types, see PDF specification 2.0 (Section 12.5).
DsPdf offers a variety of standard annotation types, as listed below:
All the listed annotations have a dedicated class and properties in the DsPdf library which makes it easier to implement different annotations. DsPdf also allows you to specify various characteristics of annotation such as visibility, printing, etc. using Flags property that accepts the values from AnnotationFlags enum.
DsPdf allows you to add annotations to a page in the PDF document. These annotations reside in the Page object on which they are placed.
To add an annotation on a page:
C# |
Copy Code
|
---|---|
var textAnnot = new TextAnnotation() { Contents = "This is an annotation in red color.", Name = "Text Annotation", Rect = new RectangleF(72, 72, 72 * 2, 72), Color = Color.Red, }; //Add the text annotation page.Annotations.Add(textAnnot); |
To get the annotations from a page:
C# |
Copy Code
|
---|---|
//Get Annotation AnnotationCollection acol = doc.Pages[0].Annotations; // Display the property values Console.WriteLine("Annotation Type: {0}", acol[0].Name); |
To modify the annotation, you can set the properties of the type of annotation you used on a page. For instance, setting Contents property of AnnotationBase class and Color property of the TextAnnotation class modifies the existing content and color of the annotation.
C# |
Copy Code
|
---|---|
//Modify annotation textAnnot.Color = Color.BlueViolet; textAnnot.Contents = "This is a Text annotation."; |
To delete all the annotations from a page, use the Clear method. Apart from this, RemoveAt method can be used to remove a particular annotation by specifying its index value.
C# |
Copy Code
|
---|---|
// Delete all annotations page.Annotations.Clear(); // Delete a particular annotation page.Annotations.RemoveAt(0); |
For more information about how to implement annotations using DsPdf, see DsPdf sample browser.