How to remove all annotations from an existing PDF document
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can remove all annotations from an existing PDF document.
Steps to remove all annotations from an existing PDF document
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from nuget.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Use the following code sample to remove all annotations from an existing PDF document.
C#
// Loads the document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Annotation.pdf");
foreach (var page in loadedDocument.Pages)
{
// Gets the page from PDF document
PdfLoadedPage loadedPage = page as PdfLoadedPage;
// Gets the annotation collection
PdfLoadedAnnotationCollection annotations = loadedPage.Annotations;
// Remove the annotation from annotation collection
for (int i = annotations.Count - 1; i >= 0; i--)
{
annotations.RemoveAt(i);
}
}
// Saves the document
loadedDocument.Save("Sample.pdf");
// Closes the document
loadedDocument.Close(true);
// This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf");
VB.NET
' Loads the document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Annotation.pdf")
For Each page In loadedDocument.Pages
' Gets the page from PDF document
Dim loadedPage As PdfLoadedPage = TryCast(page, PdfLoadedPage)
' Gets the annotation collection
Dim annotations As PdfLoadedAnnotationCollection = loadedPage.Annotations
' Remove the annotation from annotation collection
For i As Integer = annotations.Count - 1 To 0 Step -1
annotations.RemoveAt(i)
Next
Next
' Saves the document
loadedDocument.Save("Sample.pdf")
' Closes the document
loadedDocument.Close(True)
' This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf")
A complete working sample can be downloaded from RemoveAnnotationSample.zip.
Take a moment to peruse the documentation. You can add the annotation to PDF document, flatten, and modifying the annotations, importing, or exporting annotation from FDF or XFDF file, adding comments, and review status to the PDF annotation, and printing annotation.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.