iText7。填写PDF表单无法按预期工作

时间:2016-09-27 14:30:59

标签: c# pdf itext7

我准备使用iTextSharp来填写PDF格式,当我发现时,块上有新的孩子:iText 7.0。我自然而然地尝试了......不幸的是,它并没有顺利进行。这是一个问题。我有可编辑的PDF表单,需要以编程方式填写。我使用以下代码来完成:

string srcPdfFormPath = @"<Path to source pdf form>";
string destPdfFormPath = @"<Path to destination pdf form>";
using (PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(destPdfFormPath)))
{
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

    var fieldName = "FullName";
    var pdfField = form.GetField(fieldName);
    if (pdfField != null)
        pdfField.SetValue("John Doe", null);
    else
        Debug.WriteLine($"Cannot find following field {fieldName } on pdf form.");

    pdfDocument.Close();
}

虽然填写了所需的字段,但表单不仅已被展平(AFAIK不是预期的行为),而且当填充的pdf将在Acrobat reader中打开时,它会产生以下消息:

  

“此文档启用了Adobe Acrobat Reader DC中的扩展功能。   该文档自创建和使用扩展后已更改   功能不再可用。请联系作者   本文档的原始版本。“

这些结果不是我们拍摄的。因此,为了保留文档可编辑性并摆脱讨厌的消息,我决定在“追加模式”中打开PDF表单。这是我用过的代码:

string srcPdfFormPath = @"<Path to source pdf form>";
string destPdfFormPath = @"<Path to destination pdf form>";
using (PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(destPdfFormPath)
                         , new StampingProperties().UseAppendMode()))
{
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);

    var fieldName = "FullName";
    var pdfField = form.GetField(fieldName);
    if (pdfField != null)
        pdfField.SetValue("John Doe", null);
    else
        Debug.WriteLine($"Cannot find following field {fieldName } on pdf form.");

    pdfDocument.Close();
}

而且,唉,这次尝试也失败了。虽然不再看到讨厌的消息,但表单本身不会被填充,这是不可接受的。而对于我的生活,我无法弄清楚为什么这些行为会像马和马车一样坚持下去!

所以问题是:我在这里缺少什么?有没有办法让它使用新版的iText?

0 个答案:

没有答案