如何将PDF保存为只读/扁平?

时间:2022-05-31 06:23:17

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.

我使用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);

4 个解决方案

#1


1  

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:

使用PdfSharp 1.32、PdfSharp.Pdf设置所有字段的ReadOnly属性对我来说都是有效的。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");

#2


0  

Time ago I have used the this properties(see below) for making the document readonly

不久前,我使用了this属性(参见下面)来使文档只读

securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = false;

#3


0  

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

必须设置所有者密码才能使设置有效。

securitySettings.OwnerPassword = "owner";

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

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

#4


0  

I fit uozuAho's answer for PDFsharp 1.32 and changed it to lock all fields not just text fields.

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

#1


1  

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:

使用PdfSharp 1.32、PdfSharp.Pdf设置所有字段的ReadOnly属性对我来说都是有效的。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");

#2


0  

Time ago I have used the this properties(see below) for making the document readonly

不久前,我使用了this属性(参见下面)来使文档只读

securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = false;

#3


0  

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

必须设置所有者密码才能使设置有效。

securitySettings.OwnerPassword = "owner";

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

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

#4


0  

I fit uozuAho's answer for PDFsharp 1.32 and changed it to lock all fields not just text fields.

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