如何将PDF保存为只读/拼合

本文关键字:只读 拼合 保存 PDF | 更新日期: 2023-09-27 18:34:49

我正在使用PDFSharp生成一个填写了字段的PDF文档。保存文档后,我希望它是只读的,也就是扁平的。我已经尝试了以下内容,但是在Adobe中打开PDF时,这些字段仍然是可编辑的。

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

如何将PDF保存为只读/拼合

设置所有字段的 ReadOnly 属性对我有用,使用 PdfSharp 1.32,使用 PdfSharp.Pdf.AcroForms(在发布问题时可能不可用(。例如:

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

很久以前,我使用了此属性(见下文(使文档只读

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 您必须设置所有者密码才能使设置生效。

securitySettings.OwnerPassword = "owner";

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

我符合uozuAho对PDFsharp 1.32的回答,并将其更改为锁定所有字段,而不仅仅是文本字段。

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