Page 1 of 1

PDF Security

Posted: Sat Jan 24, 2015 12:37 am
by kwaltman
I am trying to set the security settings on a document. But the .EncryptionSystem property object and all of it's properties on a PDFDocument are readonly. How does one set the actual properties. Ifs there another method that has to be used to set the properties?

Re: PDF Security

Posted: Sun Jan 25, 2015 2:45 am
by vladimirG
kwaltman,
you can use constructors to create new objects with desired properties. Here is an example:

Code: Select all


EncryptionSystem encSystem = new EncryptionSystem(
    EncryptionAlgorithm.RC4, 40, "userpwd", "ownerpwd", UserAccessPermissions.ExtractTextAndGraphics);

using (PdfDocument document = new PdfDocument(PdfFormat.Pdf_16, encSystem))
{
    document.Pages.Add(PaperSizeKind.A4);
    document.Save(filename);
}

encSystem.Dispose();

If you want to change security properties of an existing PDF document, do it in the same way. The following code should work:

Code: Select all


EncryptionSystem encSystem = new EncryptionSystem(
    EncryptionAlgorithm.RC4, 40, "userpwd", "ownerpwd", UserAccessPermissions.ExtractTextAndGraphics);

using (PdfDocument documentOriginal = new PdfDocument(inFilename))
using (PdfDocument documentChanged = new PdfDocument(PdfFormat.Pdf_16, encSystem))
{
    documentChanged.Pages.AddRange(documentOriginal.Pages.ToArray());
    documentChanged.Save(outFilename);
}

encSystem.Dispose();