Articles in this section
Category / Section

How to protect the attachments in existing PDF in WinForms?

6 mins read

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 encrypt only the attachment files in an existing PDF document using the EncryptOnlyAttachments encryption option.

Note:

UserPassword is mandatory for this encryption option.

Steps to protect the attachments in an existing PDF document programmatically in C#:

  1. Create a new C# console application project.

Create a console application in Visual Studio

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET framework application from NuGet.org. Refer NuGet to the project
  2. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing; 
using Syncfusion.Pdf.Security;
using System.Drawing;

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Security;
Imports System.Drawing
  1. The Syncfusion® PDF Library supports encrypting only the attachment files in an existing PDF document using the EncryptOnlyAttachments option in PdfEncryptionOptions enum property.

C#

//Specifies encryption option
 
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;

       VB.NET

'Specifies encryption option
 
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments
 
  1. Include the following code snippet in the main method of Program.cs file to encrypt only the attachment files in an existing PDF document.

C#

//Load the PDF document
 
PdfLoadedDocument document = new PdfLoadedDocument(@"input.pdf");
 
//PDF document security
 
PdfSecurity security = document.Security;
 
//Specifies encryption key size, algorithm, and permission
 
security.KeySize = PdfEncryptionKeySize.Key256Bit;
 
security.Algorithm = PdfEncryptionAlgorithm.AES;
 
//Provide user password
 
security.UserPassword = "password";
 
//Specifies encryption option
 
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
 
//Save the document
 
document.Save("Output.pdf");
 
//Close the document
 
document.Close(true);

 

VB.NET

'Load the PDF document
 
Dim document As New PdfLoadedDocument("Input.pdf")
 
'PDF document security
 
Dim security As PdfSecurity = document.Security
 
'Specifies encryption key size, algorithm, and permission
 
security.KeySize = PdfEncryptionKeySize.Key256Bit
 
security.Algorithm = PdfEncryptionAlgorithm.AES
 
'Provide user password
 
security.UserPassword = "password"
 
'Specifies encryption option
 
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments
 
'Save the document
 
document.Save("Output.pdf")
 
'Close the document
 
document.Close(true)

 A complete working sample can be downloaded from AttachmentSample.Zip

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

ProtectExistingAttachment

Take a moment to peruse the documentation, where you will find other options like protect attachment in existing pdf document, protect an existing document, and encryption option .

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

See Also:

Protect the pdf in .NET

Working with security

How to secure PDF file with passwords

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.

 

 

 

 

 

 

 

 

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