简体   繁体   English

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

[英]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.我正在使用 PDFSharp 生成一个填充了字段的 PDF 文档。保存文档后,我希望它是只读的,也就是扁平化的。 I've tried the below, but still, when opening the PDF in Adobe, the fields are editable.我已经尝试了下面的方法,但是在 Adob​​e 中打开 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);

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).使用 PdfSharp 1.32,使用 PdfSharp.Pdf.AcroForms 设置所有字段的 ReadOnly 属性对我有用(在发布问题时这可能不可用)。 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. AFAIK 您必须设置所有者密码才能使设置生效。

securitySettings.OwnerPassword = "owner";

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

I fit uozuAho's answer for PDFsharp 1.32 and changed it to lock all fields not just text fields.我适合 uozuAho's answer for 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM