Category / Section
How to Lock Annotations in a PDF Document
2 mins read
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can lock the annotations in PDF documents using C# and VB.NET.
Steps to lock the annotation in PDF document programmatically
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET Framework application from Nuget.org.
- Include the following namespaces in the Program.cs file.
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Parsing;
- Use the following code sample in Program.cs to lock the annotation in a PDF document.
FileStream inputStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
//Get the page.
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;
//Create a new rectangle annotation.
PdfRectangleAnnotation rectangleAnnotation = new PdfRectangleAnnotation(new RectangleF(10, 20, 200, 100), "rectangle");
//Assign the borderWidth value.
rectangleAnnotation.Border.BorderWidth = 1;
//Assign the color.
rectangleAnnotation.Color = Color.Red;
//Assign the InnerColor.
rectangleAnnotation.InnerColor = Color.Yellow;
//Set the annotation flags to the rectangleAnnotation.
rectangleAnnotation.AnnotationFlags = PdfAnnotationFlags.Locked;
//Add the annotation to the page.
lpage.Annotations.Add(rectangleAnnotation);
//Create a file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Sample.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save a PDF document to the file stream.
loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);
A complete working sample can be downloaded from LockAnnotation.zip.
By executing the program, you will get a PDF document as follows.
Take a moment to peruse the documentation, where you can find other options like adding annotation to PDF, flattening annotation, modifying annotation, and removing annotation with code examples.
Refer here to learn more about PDF annotation.