Skip to main content
 首页 » 编程设计

c#之使用 iTextSharp 在 PDF 阅读器中启用保存选项

2024年02月05日11lyhabc

我使用 iTextSharp 开发了一个可编辑的 PDF

我的代码是:

//Creating a document 
        Document document = new Document(PageSize.A4, 50, 50, 25, 25); 
 
        FileStream pdfFileStream = new FileStream("D:\\new.pdf", FileMode.Create); 
 
//Writing to PDF 
        using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream)) 
        { 
            //Opening the document for writing 
            document.Open(); 
            PdfContentByte cb = pdfWriter.DirectContent; 
 
            Paragraph para = new Paragraph("Name"); 
            document.Add(para); 
 
            //Creating the text box starts here ---> 
            TextField _text = new TextField(pdfWriter, new Rectangle(100, 806, 170, 790), "Name"); 
            _text.BackgroundColor = BaseColor.LIGHT_GRAY; 
            _text.Alignment = Element.ALIGN_CENTER; 
            //_text.Options = TextField.MULTILINE; 
            _text.Text = ""; 
            pdfWriter.AddAnnotation(_text.GetTextField()); 
            cb = pdfWriter.DirectContent; 
 
            //Creating the text box ends here ---< 
 
            //Creating the RadioButton group starts here ----> 
 
            PdfFormField _radioGroup = PdfFormField.CreateRadioButton(pdfWriter, true); 
            _radioGroup.FieldName = "Gender"; 
 
            string[] genders = { "Male", "Female" }; 
 
            RadioCheckField genderRadioCheckField; 
            PdfFormField radioGField; 
 
            for (int i = 0; i < genders.Length; i++) 
            { 
                genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(40, 806 - i * 40, 60, 788 - i * 40), null, genders[i]); 
 
                genderRadioCheckField.BackgroundColor = BaseColor.LIGHT_GRAY; 
                genderRadioCheckField.CheckType = RadioCheckField.TYPE_CIRCLE; 
 
                radioGField = genderRadioCheckField.RadioField; 
                ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(genders[i], new Font(Font.FontFamily.HELVETICA, 12)), 70, 790 - i * 40, 0); 
                _radioGroup.AddKid(radioGField); 
 
            } 
 
            pdfWriter.AddAnnotation(_radioGroup); 
            cb = pdfWriter.DirectContent; 
            //Creating the RadioButton group ends here ----< 
 
 
 
            //Closing the document for writing 
            document.Close(); 
 
            Console.WriteLine("Completed"); 
            Console.ReadLine(); 
 
        } 

现在,代码生成带有文本字段和单选按钮的可编辑 PDF。

问题是:

我使用 Adobe Reader 打开生成的 PDF 文件并在 文本字段 中输入一些文本并尝试保存它。

我在文件菜单下找不到任何保存选项,但我也尝试使用另存为--> PDF,它显示消息为:

“除非您使用的是 Adob​​e Acrobat X standard、Adobe Acrobat X pro、Adobe Acrobat X knowledge worker suite,否则本文档不允许您保存对其所做的任何更改。您将只保存副本文档。你想继续吗?”

如何在 Adob​​e PDF Reader 中启用保存选项?。我的 C# 代码有问题吗?

请您参考如下方法:

您想要 Reader 启用您的 PDF。 Reader 启用涉及使用 Adob​​e 拥有的私钥添加数字签名。当 Adob​​e Reader 检测到该签名并且签名有效时,Adobe Reader 会解锁仅在 Adob​​e Acrobat 中可用的功能(取决于您在签署文档时定义的使用权限)。

你的C#没有问题。主要问题是您要求的功能需要访问 Adobe 拥有的私钥。没有第三方产品被授予访问该 key 的权限。您只能使用 Adob​​e 软件阅读 PDF 文档。 iText、iTextSharp 或 Adob​​e 产品以外的任何其他软件使用 Reader 启用文档都是非法的。

总结:您要求的功能只有在使用 Adob​​e 软件时才可用。不允许第三方供应商提供该功能(除非他们获得 Adob​​e 的许可)。