简体   繁体   English

在C#中将AcroForm PDF转换为普通PDF

[英]AcroForm PDF to normal PDF in c#

I have an Acroform PDF (a PDF which can be edited) but I'm using an API to sign the PDF which requires that the PDF is a normal one and never an Acroform one. 我有一个Acroform PDF(可以编辑的PDF),但是我正在使用API​​对PDF进行签名,这要求PDF是正常的,而不是Acroform。

Is there any way to transform an AcroForm PDF to a normal one? 有什么方法可以将AcroForm PDF转换为普通PDF?

I tried making it Read-Only but even though it cannot be edited it still is an Acroform PDF. 我尝试将其设置为只读,但即使无法编辑,也仍然是Acroform PDF。

In answer to my comment, I assume you are using iTextSharp, even though you do not specify. 为了回答我的评论,我假设您正在使用iTextSharp,即使您未指定。 Using iTextSharp, I believe you need to Flatten the form when you are done. 相信使用iTextSharp,您需要在完成后展平表格。 Here is a simple example: 这是一个简单的示例:

public void GeneratePDF(string filePath, List<PDFField> modifiedFields)
        {
            var pdfReader = new PdfReader(filePath);
            var folderStructure = filePath.Split('\\');
            if (folderStructure.Length == 0) return;
            var currentFileName = folderStructure.Last();
            var newFilePath = string.Format("{0}{1}", Constants.SaveFormsPath,
                currentFileName.Replace(".pdf", DateTime.Now.ToString("MMddyyhhmmss") + ".pdf"));
            var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFilePath, FileMode.Create));
            foreach (var field in modifiedFields.Where(f=>f.Value != null))
            {
                pdfStamper.AcroFields.SetField(field.Name, field.Value);
            }
            pdfStamper.FormFlattening = true;
            pdfStamper.Close();
        }

Ignoring the parts about the filename, it boils down to passing in some key value list regarding the field values to set. 忽略有关文件名的部分,归结为传递一些与要设置的字段值有关的键值列表。 This could be where you do your signature piece, and then setting the FormFlattening property on the stamper to true. 这可能是您进行签名的地方,然后将压模上的FormFlattening属性设置为true。

Here is another SO post where they used a similiar technique for a slightly different issue, it may be of help: How to flatten already filled out PDF form using iTextSharp 这是另一篇SO帖子,他们在其中使用了类似的技术来解决稍有不同的问题,这可能会有所帮助: 如何使用iTextSharp展平已填写的PDF表单

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

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