Edit PDF annotations in C#
To edit an annotation, use one of the methods starting with the SetAnnotation prefix. All these methods require the annotation index as their parameter. Any additional parameters depend on the method you want to use. These methods can change the following areas of an annotation, among others:
- Appearance
- Content
- Function properties
To edit an annotation, follow these steps:
- Create a
GdPicturePDFobject. - Load a PDF file with the
LoadFromFilemethod. - Select the PDF page where the searched annotation is located.
- Loop through all the annotations on the selected page. Use the
GetAnnotationCountmethod to get the total amount of annotations on the selected page. - Determine the searched annotation by any of its parameters, such as its title, type, or subject. Use the methods starting with the
GetAnnotationprefix. The full list is available in the get properties guide. - Modify the annotation with a method starting with the
SetAnnotationprefix. - Save the PDF document to a file with the
SaveToFilemethod.
If you want to edit an annotation you just created, save the return value of the method used to create the annotation, which is the annotation index.
To change the content of a free text annotation, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Select the PDF page.gdpicturePDF.SelectPage(1);// Get the annotation count.int annotCount = gdpicturePDF.GetAnnotationCount();// Loop through all annotations.for (int i = 0; i < annotCount; i++){ // Check if the current annotation is of the `FreeText` type. if (gdpicturePDF.GetAnnotationSubType(i) == "FreeText") { // Change the annotation content. gdpicturePDF.SetAnnotationContents(i, "New content"); }}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Select the PDF page. gdpicturePDF.SelectPage(1) ' Get the annotation count. Dim annotCount As Integer = gdpicturePDF.GetAnnotationCount() ' Loop through all annotations. For i = 0 To annotCount - 1 ' Check if the current annotation is of the `FreeText` type. If gdpicturePDF.GetAnnotationSubType(i) Is "FreeText" Then ' Change the annotation content. gdpicturePDF.SetAnnotationContents(i, "New content") End If Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End UsingUsed methods
Related topics