Setting Permissions for a PDF file

The code below shows you how to set permissions for a PDF file.

This sample requires a Tools license.

C#
using System;
using SolidFramework.Plumbing;
using SolidFramework.Pdf;

namespace PDFPermissions
{
    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 Permissions*//

            // 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
                PdfPasswordSecurityHandler securityHandler = new PdfPasswordSecurityHandler();

                // Assign the Permissions to the PDF Using the Security Handler
                securityHandler.Permissions = AccessPermissions.None;

                // Note: In order to change document permissions you must set a security password                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\\yourpdf.pdf");

    // 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
    SolidFramework::Pdf::PdfPasswordSecurityHandlerPtr securityHandler(new SolidFramework::Pdf::PdfPasswordSecurityHandler());

    // Assign the Permissions to the PDF Using the Security Handler
    securityHandler->setPermissions(SolidFramework::Pdf::AccessPermissions::None);

    // Note: In order to change document permissions you must set an owner security password
    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();
    securityHandler->Dispose();

    return 0;
}
VB.Net
Imports SolidFramework.Plumbing
Imports SolidFramework.Pdf

Module PDFPermissions

    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 the permissions to the PDF using the security handler
        mySecurityHandler.Permissions = AccessPermissions.None

        ' Note: inorder to set permissions owner password and encryption must be set
        mySecurityHandler.OwnerPassword = "owner"
        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