How to save a PDF as read-only / flattened?
Asked Answered
A

4

6

I'm using PDFSharp to generate a PDF document with fields filled in. When the doc is saved, I'd like it to be read-only, aka flattened. I've tried the below, but still, when opening the PDF in Adobe, the fields are editable.

   using (PdfDocument form = PdfReader.Open(outputFormLocation , PdfDocumentOpenMode.Modify))
        {
            //do stuff...
            //Save
            PdfSecuritySettings securitySettings = form.SecuritySettings;
            securitySettings.PermitFormsFill = false;
            securitySettings.PermitModifyDocument = false;
            securitySettings.PermitPrint = true;

            form.Save(outputFormLocation);
Agata answered 9/10, 2013 at 19:6 Comment(1)
Did you set an owner password? Do not set a user password to get a PDF that can be opened without specifying a password.Schermerhorn
F
3

Setting all fields' ReadOnly property works for me using PdfSharp 1.32, using PdfSharp.Pdf.AcroForms (this may not have been available at the time the question was posted). For example:

PdfDocument document = PdfReader.Open("file.pdf", PdfDocumentOpenMode.Modify);
PdfAcroForm form = document.AcroForm;
PdfAcroField.PdfAcroFieldCollection fields = form.Fields;
string[] names = fields.Names;

for (int idx = 0; idx < names.Length; idx++)
{
    string fqName = names[idx];
    PdfAcroField field = fields[fqName];
    PdfTextField txtField;

    if ((txtField = field as PdfTextField) != null)
    {
        txtField.ReadOnly = true;
    }
}
document.Save("file.pdf");
Fraternize answered 19/11, 2014 at 3:8 Comment(0)
C
1

Time ago I have used the this properties(see below) for making the document readonly

securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = false;
Chondroma answered 9/10, 2013 at 19:18 Comment(1)
Thanks, but I just tried that, and got the same results. In Adobe, I can edit the fields.Agata
S
1

AFAIK you have to set the owner password to make the settings effective.

securitySettings.OwnerPassword = "owner";

http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx

Schermerhorn answered 10/10, 2013 at 8:10 Comment(1)
That didn't work. The only thing it changed was that in the titlebar of Adobe, after the name, there's a (SECURED) tag. I was still able to type a new value into one of the fields, and save the changes back to the same document, without inputting a password. I need to be able to flatten/disable editing completely.Agata
A
1

I fit uozuAho's answer for PDFsharp 1.32 and changed it to lock all fields not just text fields.

PdfDocument document = PdfReader.Open("file.pdf", PdfDocumentOpenMode.Modify);
PdfAcroForm form = document.AcroForm;
string[] names = form.Fields.Names;
for (int idx = 0; idx < names.Length; idx++)
{
    string fqName = names[idx];
    PdfAcroField field = form.Fields[fqName];
    field.ReadOnly = true;
}
document.Save("file.pdf");
Allain answered 7/9, 2016 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.