How to protect the attachments in existing PDF in WinForms?
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.
UserPassword is mandatory for this encryption option.
Steps to protect the attachments in an existing PDF document programmatically in C#:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET framework application from NuGet.org.
- 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
- 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
- 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.
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:
How to secure PDF file with passwords
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.