)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set the created PDF file to read only in C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: February 4, 2015 |
Last Revised Date: May 14, 2019

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.

Using this library, you can secure a PDF document by providing the Security details to it in C# and VB.NET. This sample uses the following PDF document and explains how to make it read only.

Secure the PDF document

Steps to secure an existing PDF document programmatically in C#:

  1. Create a C# console application project. Create a console application in Visual Studio
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org. Refer NuGet package to the project
  3. Syncfusion Essential PDF supports different security options like encryption Algorithm, KeySize, OwnerPassword, Permissions, and UserPassword.

C#

//Set the documents permission settings
PdfSecurity security = loadedDocument.Security;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.KeySize = PdfEncryptionKeySize.Key128Bit;
security.OwnerPassword = "Syncfusion";
security.Permissions = PdfPermissionsFlags.EditAnnotations | PdfPermissionsFlags.Print;
security.UserPassword = "123";

 

VB.NET

'Set the documents permission settings
Dim security As PdfSecurity = loadedDocument.Security
security.Algorithm = PdfEncryptionAlgorithm.AES
security.KeySize = PdfEncryptionKeySize.Key128Bit
security.OwnerPassword = "Syncfusion"
security.Permissions = PdfPermissionsFlags.EditAnnotations Or PdfPermissionsFlags.Print
security.UserPassword = "123"

 

  1. Include the following namespaces in Program.cs file.

C#

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

 

VB.NET

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

 

  1. Include the following complete code snippet in main method of Program.cs file to secure an existing PDF document.

C#

//Load an existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("PdfTemplate.pdf");
 
//Provide security details to PDF
PdfSecurity security = loadedDocument.Security;
security.OwnerPassword = "Syncfusion";
security.Permissions = PdfPermissionsFlags.Default;
 
//Save the PDF document
loadedDocument.Save("ReadOnly.pdf");
 
//Close the instance of PdfLoadedDocument
loadedDocument.Close();

 

VB.NET

'Load an existing PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("PdfTemplate.pdf")
 
'Provide security details to PDF
Dim security As PdfSecurity = loadedDocument.Security
security.OwnerPassword = "Syncfusion"
security.Permissions = PdfPermissionsFlags.Default
 
'Save the PDF document
loadedDocument.Save("ReadOnlyPDF.pdf")
 
'Close the instance of PdfLoadedDocument
loadedDocument.Close()

 

A complete work sample to set the created PDF document to read only can be downloaded from ReadOnlyPDF.zip.

By executing the program, you will get the secured PDF document as follows.

Secured PDF document

You can also secure a new PDF document by providing the same security options.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
 
//Set the documents permission settings
PdfSecurity security = document.Security;
security.Algorithm = PdfEncryptionAlgorithm.AES;
security.OwnerPassword = "Syncfusion";

 

VB.NET

'Create a New PDF document
Dim document As PdfDocument = New PdfDocument
 
'Set the documents permission settings
Dim security As PdfSecurity = document.Security
security.Algorithm = PdfEncryptionAlgorithm.AES
security.OwnerPassword = "Syncfusion"

 

Take a moment to peruse the documentation, where you will find other options like working with RC4 and AES encryptions, changing the password, changing the permission, remove user password, and more.

Click here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to encrypt a PDF document.

Note:

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

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon