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);
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");