Page 1 of 1

Header and Footer Not Present in completed document

PostPosted: Thu Jan 17, 2019 3:07 am
by bobbybobstein
Hello,

I am using a simple method based off the marshalling sample to replace some variables in a docx template. I am also using the same method to add in a List as well as a Table based off an ArrayList input. When I create the new document from the template without adding in the List and Table, the document copies perfectly, including the header and footer formatting and images. However, when I add in the Table and List components, I lose everything from my header and footer in the completed document. After looking at both the template and output file, it appears that the xml remains unchanged. What could be causing the Table and List to make the header/footer not visible within the output file?

In help in this matter would be greatly appreciated, I will attach my method code and will gladly supply any other resources.

Code: Select all

   public void FillDocument() {
     
   }
   
   public void createDoc(String company, String currentdateWithSlashes, String startDateMMYY, String endDateMMYY,
         String currentDate, String msaDate, String startDate, String endDate, String streetAddress,
         String cityStateAndPostal, String invoiceEmail, List<Resource> resourceList, List<Task> taskList) throws Exception{
         // Define input and output file destinations
         org.docx4j.wml.ObjectFactory foo = Context.getWmlObjectFactory();
         
         String inputFilePath = "Template.docx";
         String outputFilePath = "Output.docx";
         
         // Load Template
         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputFilePath));
         
         // Get main document xml
         MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
         
         // Prepare variables to be replaced
         VariablePrepare.prepare(wordMLPackage);
         
         // provide placeholders and replacements for variable replacement
         HashMap<String, String> mappings = new HashMap<String, String>();
         mappings.put("company", company);
         mappings.put("dateWithSlashes", currentdateWithSlashes);
         mappings.put("startDateMMYY", startDateMMYY);
         mappings.put("endDateMMYY", endDateMMYY);
         mappings.put("currentDate", currentDate);
         mappings.put("msaDate", msaDate);
         mappings.put("startDate", startDate);
         mappings.put("endDate", endDate);
         mappings.put("companyNameWithAddress", company + " at " + streetAddress + " " + cityStateAndPostal);
         mappings.put("invoiceEmail", invoiceEmail);
         
         documentPart.variableReplace(mappings);
         
////         // Create and insert task List and Resource table
         Body bulletList = new CreateTaskList().createIt(taskList);
         documentPart.getJaxbElement().getContent().add(13, bulletList);
         JAXBElement table = new CreateResourceTable().createIt(resourceList);
         documentPart.getJaxbElement().getContent().add(23, table);
         
         // Save File
         File exportFile = new File(outputFilePath);
         wordMLPackage.save(exportFile);

   }

Re: Header and Footer Not Present in completed document

PostPosted: Mon Jan 21, 2019 6:20 am
by jason
most likely you are adding a new sectPr or conflicting relIds?

You can attach your docx or email it to me...

Re: Header and Footer Not Present in completed document

PostPosted: Fri Jan 25, 2019 2:56 am
by bobbybobstein
So I created the list as a Body. Which was giving me the problem as it tried to enter it. On second look, the table was working fine. So I rewrote inserting the list as paragraphs and it worked beautifully. Thanks for the help.