Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 357
user948060 Avatar asked Oct 09 '13 19:10

user948060


People also ask

How do I save a PDF in read only mode?

On Windows, just right-click your PDF and select Properties > Read-only > OK. On a Mac computer, the process is a little more complex. Select the PDF and click File > Get Info. You can then set access rights under the Sharing & Permissions tab.

What does it mean when a PDF says flattening?

If a document contains transparent objects, such as images with transparency, Acrobat flattens the document before printing it. Flattening removes transparency information and converts images to a format that the printer can interpret.

How do you flatten an edited PDF?

Flatten PDFs on a PC Computer In the Print Popup, select Adobe PDF as your printer (Letter A). Then select Fit in the Page Sizing & Handling section (Letter B). This allows the Adobe PDF print driver to reconvert and scale your document down to an 8.5″ x 11″ piece of paper. Finally, click the Print button (Letter C).


3 Answers

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");
like image 174
uozuAho Avatar answered Oct 23 '22 11:10

uozuAho


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;
like image 1
Tinwor Avatar answered Oct 23 '22 11:10

Tinwor


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

securitySettings.OwnerPassword = "owner";

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

like image 1
I liked the old Stack Overflow Avatar answered Oct 23 '22 11:10

I liked the old Stack Overflow