Articles in this section
Category / Section

How to Set a Created PDF File to Read-Only Using C# and VB.NET

8 mins read

The Syncfusion Essential® PDF library is a comprehensive and high-performance .NET PDF library that allows developers to create, read, and edit PDF documents programmatically without relying on Adobe. This article will guide you through securing a PDF document, making it read-only using C# and VB.NET

Steps to secure a PDF document and make it read-only:

  1. Create a Console Application: Set up a new console application project.

  2. Install Required Packages: Add the Syncfusion.Pdf.WinForms NuGet package from NuGet.org to your project.


    WF_NuGet_package
  3. Set Up Your Environment: In the Program.cs file, include these namespaces.

C#

using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;

VB.NET

Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Security

 

  1. Configure Security in a PDF DocumentUtilize the following code example to apply security settings, making the PDF read-only.

C#

// Load an existing PDF document from disk
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");

// Access the security settings of the PDF document
PdfSecurity security = loadedDocument.Security;

// Set the encryption algorithm to AES (Advanced Encryption Standard)
security.Algorithm = PdfEncryptionAlgorithm.AES;

// Set the encryption key size to 128-bit for a balance of security and performance
security.KeySize = PdfEncryptionKeySize.Key128Bit;

// Set the owner password (required to enforce permissions)
security.OwnerPassword = "Syncfusion";

//It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.AccessibilityCopyContent;

// Set an optional user password (required to open the PDF)
security.UserPassword = "password";

// Save the updated and secured PDF document to a new file
loadedDocument.Save("Output.pdf");

// Close and dispose of the loaded PDF document to release resources
loadedDocument.Close(true);

VB.NET

' Load an existing PDF document from disk
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")

' Access the security settings of the PDF document
Dim security As PdfSecurity = loadedDocument.Security

' Set the encryption algorithm to AES (Advanced Encryption Standard)
security.Algorithm = PdfEncryptionAlgorithm.AES

' Set the encryption key size to 128-bit for a good balance of security and performance
security.KeySize = PdfEncryptionKeySize.Key128Bit

' Set the owner password (required to enforce document permissions)
security.OwnerPassword = "Syncfusion"

' It allows printing and accessibility copy content.
security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.AccessibilityCopyContent
' Optionally, set a user password to require a password to open the document
security.UserPassword = "password"

' Save the secured PDF document to a new file
loadedDocument.Save("Output.pdf")

' Close and dispose of the PDF document to release resources
loadedDocument.Close(True)
Additional Information:
Document Permissions: The PdfPermissionsFlags enumeration is used to define what actions are permitted on a secured PDF document.

A complete working sample is available for download from GitHub.

By executing the program, the output PDF document will be generated as shown below:


 

Take a moment to peruse the User Guide documentation for advanced options like encryption techniques and password management.

Note:
From version 16.2.0.x, include a Syncfusion® license key in your projects for non-trial usage. Learn more about generating and registering a Syncfusion® license here.

 

Conclusion
I hope you enjoyed learning about how to convert MHTML files to PDF documents in ASP.NET Core PDF.

You can refer to our ASP.NET Core PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core PDF example 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 check out 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, Direct-Trac, or feedback portal. We are always happy to assist you!

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