简体   繁体   English

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

[英]PDF Clown, AcroForms with same field names

I have made a number of single page Forms with Open Office and exported them to PDF documents.我用Open Office制作了多个单页Forms,导出为PDF文档。

I my application I open up the a number of these pdf documents, fill in the form elements and combine them and save.在我的应用程序中,我打开了 pdf 个文档中的一些,填写表单元素并将它们组合起来并保存。

In am printing out a List onto each row of a particular form.我正在将一个列表打印到特定表格的每一行。 The trouble is that if the list exceeds the size of the page, I need to duplicate the page and append the rest of the items on the remaining page.麻烦的是,如果列表超过页面的大小,我需要复制页面和剩余页面上的项目的 append 和 rest。

There appears to be a problem having multiple field names on a document with the same name, only the first field has a value, subsequent fields with the same name are blank.在具有相同名称的文档上具有多个字段名称似乎存在问题,只有第一个字段有值,具有相同名称的后续字段为空。

The code is something like this, I dont have the exact code with me right now.代码是这样的,我现在没有确切的代码。

        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);

I noticed that when I export from OpenOffice there is a checkbox to allow duplicate form names.我注意到当我从 OpenOffice 导出时有一个复选框允许重复的表单名称。 Has anyone had this issue before?以前有人遇到过这个问题吗? Is there something in the API that allows duplicate form names to display with different values? API 中是否有允许重复的表单名称以不同的值显示的内容?

I never actually solved this problem but I did work out an alternative. 我从未真正解决过这个问题,但确实找到了替代方案。 On each page I iterated over all the form elements and changed their names by adding "[x]" where the 'x' is the page number. 在每一页上,我遍历所有表单元素,并通过添加“ [x]”来更改其名称,其中“ x”是页码。 This made each pages form elements unique. 这使得每个页面的表单元素都是唯一的。

I couldn't solve my problem using Your method no matter what I tried. 无论我尝试了什么,我都无法使用您的方法解决问题。 All of the fields on every page ended up with the same name. 每个页面上的所有字段都以相同的名称结尾。 The resulting file needed to have 150 copies of the first page. 生成的文件需要具有第一页的150个副本。 My approach is different, create 150 PDFs containing only first page and run this code on every single one of them. 我的方法有所不同,创建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);

      }
  }
}

After that just combine them in one single file with Adobe Acrobat DC. 之后,只需将它们与Adobe Acrobat DC合并到一个文件中即可。 This code was modified a little bit from PDFClown samples. 此代码已从PDFClown示例中进行了一些修改。

A little late but here is a workaround I found.有点晚了,但这是我找到的解决方法。 I noticed that if you manually edit the text in the first field, the other fields start displaying correctly.我注意到,如果您手动编辑第一个字段中的文本,其他字段就会开始正确显示。 You can do this with JavaScript to achieve the same effect.您可以使用 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