How to hide annotations from an existing PDF document using C# and VB.NET
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can hide the annotations from an existing PDF document using C# and VB.NET.
Steps to hide the annotation from an existing PDF document programmatically
- Create a new 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 the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing
- Use the following code sample to hide the annotation from an existing PDF document.
C#
//Loads the document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/InputAnnotation.pdf"); //Gets the first page from the document PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; //Gets the annotation collection PdfLoadedAnnotationCollection annotations = page.Annotations; //Hide annotations by setting the AnnotationFlags as Hidden foreach(var annotation in annotations) { if(annotation is PdfLoadedTextMarkupAnnotation) { PdfLoadedTextMarkupAnnotation ellipseAnnotation = annotation as PdfLoadedTextMarkupAnnotation; ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden; } else if (annotation is PdfLoadedFreeTextAnnotation) { PdfLoadedFreeTextAnnotation ellipseAnnotation = annotation as PdfLoadedFreeTextAnnotation; ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden; } else if (annotation is PdfLoadedRectangleAnnotation) { PdfLoadedRectangleAnnotation ellipseAnnotation = annotation as PdfLoadedRectangleAnnotation; ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden; } } //Save the PDF document loadedDocument.Save("Sample.pdf"); //Close 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/InputAnnotation.pdf") 'Gets the first page from the document Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage) 'Gets the annotation collection Dim annotations As PdfLoadedAnnotationCollection = page.Annotations 'Hide annotations by setting the AnnotationFlags as Hidden For Each annotation In annotations If TypeOf annotation Is PdfLoadedTextMarkupAnnotation Then Dim ellipseAnnotation As PdfLoadedTextMarkupAnnotation = TryCast(annotation, PdfLoadedTextMarkupAnnotation) ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden ElseIf TypeOf annotation Is PdfLoadedFreeTextAnnotation Then Dim ellipseAnnotation As PdfLoadedFreeTextAnnotation = TryCast(annotation, PdfLoadedFreeTextAnnotation) ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden ElseIf TypeOf annotation Is PdfLoadedRectangleAnnotation Then Dim ellipseAnnotation As PdfLoadedRectangleAnnotation = TryCast(annotation, PdfLoadedRectangleAnnotation) ellipseAnnotation.AnnotationFlags = PdfAnnotationFlags.Hidden End If Next 'Save the PDF document loadedDocument.Save("Sample.pdf") 'Close 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 download from AnnotationSample.zip.
Take a moment to peruse the documentation. You can find other options like adding an annotation to the PDF document, flatten annotation, removing annotation, import, and export annotation from an existing PDF document, printing annotation, and add custom stamp using the rubber stamp annotation.
Refer to this link to explore a rich set of Syncfusion Essential® PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to hide annotations from an existing PDF document using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!