Encrypting PDF files
This sample illustrates how to encrypt PDF files.
This sample requires a Tools license.
C#
using System;
using SolidFramework.Plumbing;
using SolidFramework.Pdf;
namespace PDFEncryption
{
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";
//*PDF Encryption*//
// 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:\MyFolder\\license.xml");
// Add the PDF file to convert
wstring pdfPath(L"c:\\YourFolder\\yourfile.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);
document->Close();
document->Dispose();
return 0;
}
VB.Net
Imports SolidFramework.Plumbing
Imports SolidFramework.Pdf
Module PDFEncryption
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)
' Assign an encryption algorithm
mySecurityHandler.EncryptionAlgorithm = EncryptionAlgorithm.Aes256BitsX
' Note: In order to set the an encryption algorithm, passwords must be set on the document
mySecurityHandler.OpenPassword = "user"
mySecurityHandler.OwnerPassword = "owner"
' Write the changes back into the document
myDocument.SaveProtected(mySecurityHandler, OverwriteMode.ForceOverwrite)
' Clean up
myDocument.Close()
myDocument.Dispose()
End Sub
End Module
