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