Question

I have made a number of single page Forms with Open Office and exported them to PDF documents.

I my application I open up the a number of these pdf documents, fill in the form elements and combine them and save.

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.

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. Has anyone had this issue before? Is there something in the API that allows duplicate form names to display with different values?

Was it helpful?

Solution

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. This made each pages form elements unique.

OTHER TIPS

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. My approach is different, create 150 PDFs containing only first page and run this code on every single one of them.

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. This code was modified a little bit from PDFClown samples.

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.

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());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top