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 the PDF library, you can load encrypt-only-attachment PDF documents. To access the attachments in the existing PDF document, the UserPassword is mandatory. You can provide the UserPassword in the following ways:
- Load the PDF document with a password.
- Provide a password using the OnPdfPassword event when accessing the attachments.
It is possible to access all the contents except attachments when loading the PDF document without UserPassword.
Steps to load the encrypt-only-attachment PDF documents:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
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.
See Also:
UserPassword is mandatory for encrypt only attachments and it is only supported in AES algorithms with 128bit, 256bit, and 256bit revision6 key size.
Conclusion
I hope you enjoyed learning about How to load the attachment only encrypted PDF document in C# and VB.NET.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation 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 or feedback portal. We are always happy to assist you!