How to Lock Annotations in a PDF Document
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.
:::Info
Note: Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or 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 a trail message.
:::
Conclusion
I hope you enjoyed learning about how to lock annotations in a PDF document.
You can refer to our ASP.NET Core 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 ASP.NET Core 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!