Articles in this section
Category / Section

How to hide annotations from an existing PDF document using C# and VB.NET

5 mins read

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

  1. Create a new console application project.

Create console application project

  1. Install the Syncfusion.Pdf.Winforms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. 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

 

  1. 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:

Staring with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied