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);
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.
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.
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).
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");
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;
AFAIK you have to set the owner password to make the settings effective.
securitySettings.OwnerPassword = "owner";
http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With