How to load the attachment only encrypted PDF document in C# and VB.NET
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 PDF library, you can load the encrypt-only-attachment PDF documents. To access the attachments in the existing PDF document, the UserPassword is mandatory.
You can provide the UserPassword in following ways:
- Load the PDF document with password.
- Provide password using the OnPdfPassword Event when accessing the attachments.
It is possible to access all the contents except attachment when loading the PDF document without UserPassword.
Steps to load the encrypt-only-attachment PDF documents:
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET framework application from NuGet.org.
C#
using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing
- The following code example explains how to load an encrypt-only-attachment document with password using Syncfusion PDF Library.
C#
//Load the PDF document PdfLoadedDocument document = new PdfLoadedDocument(@"input.pdf", "password"); //Accessing the attachments foreach (PdfAttachment attachment in document.Attachments) { FileStream stream = new FileStream(attachment.FileName, FileMode.Create); stream.Write(attachment.Data, 0, attachment.Data.Length); stream.Dispose(); } //Save the document document.Save("Output.pdf"); //Close the document document.Close(true);
VB.NET
'Load the PDF document Dim document As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf", "password") 'Accessing the attachments For Each attachment As PdfAttachment In document.Attachments Dim stream = New FileStream(attachment.FileName, FileMode.Create) stream.Write(attachment.Data, 0, attachment.Data.Length) stream.Dispose Next 'Close the document document.Close(true)
- The following code example explains how to provide the password when accessing attachments from encrypt-only-attachment document using the OnPdfPassword event.
C#
//Load the PDF document PdfLoadedDocument document = new PdfLoadedDocument(@"input.pdf"); document.OnPdfPassword += LDoc_OnPdfPassword; //Accessing the attachments foreach (PdfAttachment attachment in document.Attachments) { FileStream stream = new FileStream(attachment.FileName, FileMode.Create); stream.Write(attachment.Data, 0, attachment.Data.Length); stream.Dispose(); } document.Save("output.pdf"); //Close the document document.Close(true); //Provide the user password in event private static void LDoc_OnPdfPassword(object sender, OnPdfPasswordEventArgs args) { args.UserPassword = "password"; }
VB.NET
'Load the PDF document Dim document As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf") 'document.OnPdfPassword += LDoc_OnPdfPassword() AddHandler document.OnPdfPassword, AddressOf LDoc_OnPdfPassword 'Accessing the attachments For Each attachment As PdfAttachment In document.Attachments Dim stream As FileStream = New FileStream(attachment.FileName, FileMode.Create) stream.Write(attachment.Data, 0, attachment.Data.Length) stream.Dispose() Next 'Close the document document.Close(True) 'Provide the user password in event Private Sub LDoc_OnPdfPassword(ByVal sender As Object, ByVal args As OnPdfPasswordEventArgs) args.UserPassword = "password" End Sub
A complete working sample can be downloaded from AttachmentOnlyEncryptedSample.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 secure the pdf file with password, encryption option, protect attachments in existing document and remove the password from the protected pdf file with code example.
Click here to explore the rich set of Syncfusion Essential PDF features.
UserPassword is mandatory for encrypt only attachments and it is only supported in AES algorithms with 128bit, 256bit, and 256bit revision6 key size.