繁体   English   中英

PDF 小丑,具有相同字段名称的 AcroForms

[英]PDF Clown, AcroForms with same field names

我用Open Office制作了多个单页Forms,导出为PDF文档。

在我的应用程序中,我打开了 pdf 个文档中的一些,填写表单元素并将它们组合起来并保存。

我正在将一个列表打印到特定表格的每一行。 麻烦的是,如果列表超过页面的大小,我需要复制页面和剩余页面上的项目的 append 和 rest。

在具有相同名称的文档上具有多个字段名称似乎存在问题,只有第一个字段有值,具有相同名称的后续字段为空。

代码是这样的,我现在没有确切的代码。

        org.pdfclown.files.File output = new org.pdfclown.files.File();
        PageManager pageManager = new PageManager(output.getDocument());

        for(org.pdfclown.files.File pdfPage : pdfPages) {

            //fill in the form element ...
            pdfPage.getDocument().getForm().getFields().get("some_field").setValue("something");

            pageManager.add(pdfPage.getDocument());
        }

        java.io.File temp = Files.createTempFile("Test", ".pdf").toFile();
        output.save(temp, SerializationModeEnum.Standard);

我注意到当我从 OpenOffice 导出时有一个复选框允许重复的表单名称。 以前有人遇到过这个问题吗? API 中是否有允许重复的表单名称以不同的值显示的内容?

我从未真正解决过这个问题,但确实找到了替代方案。 在每一页上,我遍历所有表单元素,并通过添加“ [x]”来更改其名称,其中“ x”是页码。 这使得每个页面的表单元素都是唯一的。

无论我尝试了什么,我都无法使用您的方法解决问题。 每个页面上的所有字段都以相同的名称结尾。 生成的文件需要具有第一页的150个副本。 我的方法有所不同,创建150个仅包含第一页的PDF,并在其中的每一个上运行此代码。

public override void Run()
{
  // 1. Magic...
  string resourcePath = System.IO.Path.GetFullPath("../../../../main/res/samples/input/" + "pdf");

  // Get the list of available PDF files
  string[] filePaths = System.IO.Directory.GetFiles(resourcePath + System.IO.Path.DirectorySeparatorChar, "*.pdf");

  // Cycle through files
  for (int index = 0; index < filePaths.Length; index++)
  {
      using (File file = new File(filePaths[index]))
      {
          Document document = file.Document;

          // 2. Get the acroform!
          Form form = document.Form;
          foreach (Page page in form.Document.Pages)
          {
              foreach (var s in page.Document.Form.Fields.Values)
              {
                  s.Name = s.FullName + index.ToString();
              }

          }
          Serialize(file, file + index.ToString(), SerializationModeEnum.Incremental);

      }
  }
}

之后,只需将它们与Adobe Acrobat DC合并到一个文件中即可。 此代码已从PDFClown示例中进行了一些修改。

有点晚了,但这是我找到的解决方法。 我注意到,如果您手动编辑第一个字段中的文本,其他字段就会开始正确显示。 您可以使用 JavaScript 来实现相同的效果。

Document pdfDocument = pdfFile.Document;
Form form = pdfDocument.Form;
StringBuilder js = new StringBuilder();
js.Append("function duplicateFieldFix(fieldName){var originalText = this.getField(fieldName).value;var newText = originalText + ' ';this.getField(fieldName).value = newText;this.getField(fieldName).value = originalText;}");
if (form != null && form.Exists()) {
    foreach(Field field in form.Fields.Values) {
        string fieldName = field.Name;
        if (formData != null && fieldName != null && fieldName.Length > 0 && formData.ContainsKey(fieldName)) {
            field.Value = formData[fieldName];
            js.Append($"duplicateFieldFix('{fieldName}');");
        }
    }
    pdfDocument.Actions.OnOpen = new JavaScript(pdfDocument, js.ToString());
}

暂无
暂无

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

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