Adding Security to PDF files

This sample illustrates how to add password protection and encrypt to PDF files. It also illustrates how to restrict what users can do with the file.

You may wish to view the video Passwords, Permissions and Pdfs  which explains some of the concepts associated with adding Security to PDFs.

It is recommended that you have already reviewed the Getting Started sample, since that includes Licensing and Framework initialization code required to make this sample run.

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
using System; using SolidFramework.Pdf; namespace CSharp_Tutorials { public static partial class Tutorials { public static bool SecurePdfFile(string pdfPath, string outputPath) { // Create a PdfDocument and open the PDF file using (var document = new PdfDocument(pdfPath)) { document.Open(); // Create a Security Handler var securityHandler = new PdfPasswordSecurityHandler(); // Set an owner password and an open password securityHandler.OwnerPassword = "owner"; securityHandler.OpenPassword = "open"; // Set the encryption algorithm to use securityHandler.EncryptionAlgorithm = EncryptionAlgorithm.Aes256Bits; // Optional: Restrict permissions securityHandler.Permissions = AccessPermissions.ContentEditing | AccessPermissions.Extracting; Console.WriteLine("Adding security to " + outputPath); // Set the path to save to document.Path = outputPath; // Save the PdfDocument with the new security handler document.SaveProtected(securityHandler, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite); } Console.WriteLine("Successfully added security to " + outputPath); Console.WriteLine(); return true; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Imports System Imports SolidFramework.Pdf Namespace VBNet_Tutorials Partial Module Tutorials Function SecurePdfFile(ByVal pdfPath As String, ByVal outputPath As String) As Boolean ' Create a PdfDocument And open the PDF file Using document As PdfDocument = New PdfDocument(pdfPath) document.Open() ' Create a Security Handler Dim securityHandler As PdfPasswordSecurityHandler = New PdfPasswordSecurityHandler() ' Set an owner password And an open password securityHandler.OwnerPassword = "owner" securityHandler.OpenPassword = "open" ' Set the encryption algorithm to use securityHandler.EncryptionAlgorithm = EncryptionAlgorithm.Aes256Bits ' Optional Restrict permissions securityHandler.Permissions = AccessPermissions.ContentEditing Or AccessPermissions.Extracting Console.WriteLine("Adding security to " & outputPath) ' Set the path to save to document.Path = outputPath ' Save the PdfDocument with the New security handler document.SaveProtected(securityHandler, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite) End Using Console.WriteLine("Successfully added security to " & outputPath) Console.WriteLine() Return True End Function End Module End Namespace
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "Tutorials.h" using namespace SolidFramework::Pdf; bool SecurePdfFile(const wstring & pdfPath, const wstring & outputPath) { // Create a PdfDocument and open the PDF file auto document = make_shared<PdfDocument>(pdfPath); document->Open(); // Create a Security Handler auto securityHandler = make_shared<PdfPasswordSecurityHandler>(); // Set an owner password and an open password securityHandler->SetOwnerPassword(L"owner"); securityHandler->SetOpenPassword(L"open"); // Set the encryption algorithm to use securityHandler->SetEncryptionAlgorithm(EncryptionAlgorithm::Aes256Bits); // Optional: Restrict permissions securityHandler->SetPermissions(AccessPermissions::ContentEditing | AccessPermissions::Extracting); wcout << L"Adding security to " << outputPath << endl; // Set the path to save to document->SetPath(outputPath); // Save the PdfDocument with the new security handler document->SaveProtected(securityHandler, SolidFramework::Plumbing::OverwriteMode::ForceOverwrite); wcout << L"Successfully added security to " << outputPath << endl << endl; return true; }

Previous sample Samples Next sample