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

时间:2013-10-09 19:06:33

标签: c# asp.net-mvc pdfsharp

我正在使用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 个答案:

答案 0 :(得分:2)

设置所有字段'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");

答案 1 :(得分:1)

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

答案 2 :(得分:0)

之前我曾使用这些属性(见下文)使文档只读

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

securitySettings.OwnerPassword = "owner";

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