Working with Password Protected PDF files
The code below shows you how to set a password for a PDF file.
This sample requires a Tools license.
C#
using System; using SolidFramework.Plumbing; using SolidFramework.Pdf; namespace PDFPasswords { class Program { [STAThread] static void Main(string[] args) { // Call your Solid Documents License SolidFramework.License.Import(@"C:\MyFolder\license.xml"); // Set the location of your the file you want to convert String pdfPath = @"C:\YourFolder\yourpdf.pdf"; //*Setting Owner and User Passwords*// // Create a new PdfDocument Object called document and assign the selected PDF file to it using (PdfDocument document = new PdfDocument(pdfPath)) { // Open the file document.Open(); // Create a new Security Handler object and Assign the Password string values //Note: Inorder to set passwords an Encryption Algorithm must be set PdfPasswordSecurityHandler securityHandler = new PdfPasswordSecurityHandler(); securityHandler.OpenPassword = "user"; securityHandler.OwnerPassword = "owner"; // Assign an encryption algorithm //Note: In order to set the an encryption algorithm, passwords must be set on the document securityHandler.EncryptionAlgorithm = EncryptionAlgorithm.Aes256BitsX; // Write the changes back into the document document.SaveProtected(securityHandler, OverwriteMode.ForceOverwrite); } } } }
C++
#include "stdafx.h" #include "SolidFramework.h" using namespace std; int main() { // Call your Solid Documents License SolidFramework::License::Import(L"c:\\yourfolder\\license.xml"); // Add the PDF file to convert wstring pdfPath(L"c:\\myfolder\\yourpdf.pdf"); //*PDF Encryption*// // Create a new PdfDocument Object called document and assign the selected PDF file to it SolidFramework::Pdf::PdfDocumentPtr document(new SolidFramework::Pdf::PdfDocument(pdfPath)); // Open the file document->Open(); // Create a new Security Handler object and Assign the Password string values //Note: Inorder to set passwords an Encryption Algorithm must be set SolidFramework::Pdf::PdfPasswordSecurityHandlerPtr securityHandler(new SolidFramework::Pdf::PdfPasswordSecurityHandler()); securityHandler->setOpenPassword(L"user"); securityHandler->setOwnerPassword(L"owner"); // Assign an encryption algorithm //Note: In order to set the an encryption algorithm, passwords must be set on the document securityHandler->setEncryptionAlgorithm(SolidFramework::Pdf::EncryptionAlgorithm::Aes256Bits); // Write the changes back into the document document->SaveProtected(securityHandler, SolidFramework::Plumbing::OverwriteMode::ForceOverwrite); securityHandler->Dispose(); document->Close(); document->Dispose(): return 0; }
VB.Net
Imports SolidFramework.Plumbing Imports SolidFramework.Pdf Module PDFPasswords Sub Main() ' Call your Solid Documents License SolidFramework.License.Import("C:\MyFolder\license.xml") 'Define a variable for your source file Dim sPdfPath As String ' Define a PDF Document object Dim myDocument As SolidFramework.Pdf.PdfDocument ' Define a new Security Handler Object Dim mySecurityHandler As SolidFramework.Pdf.PdfPasswordSecurityHandler ' Set values to your variables sPdfPath = ("C:\YourFolder\filepdf.pdf") myDocument = New PdfDocument(sPdfPath) mySecurityHandler = New PdfPasswordSecurityHandler ' Open the file myDocument.Open(sPdfPath) ' Note: Inorder to set passwords an Encryption Algorithm must be set mySecurityHandler.OpenPassword = "user" mySecurityHandler.OwnerPassword = "owner" ' Assign an encryption algorithm ' Note: In order to set the an encryption algorithm, passwords must be set on the document mySecurityHandler.EncryptionAlgorithm = EncryptionAlgorithm.Aes256BitsX ' Write the changes back into the document myDocument.SaveProtected(mySecurityHandler, OverwriteMode.ForceOverwrite) ' Clean up myDocument.Close() myDocument.Dispose() End Sub End Module