簡體   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