I'm trying to replace the document.xml for a given document. I have the following code based on the ImportForeignPart sample. It generates the resulting document however there is a problem when trying to open the document. Further investigation revealed that only the document.xml file exists under /word directory in the output document (all other files and directories are missing). Would appreciate your help on this.
- Code: Select all
WordprocessingMLPackage template = WordprocessingMLPackage.load(new File(this.getServletContext().getRealPath("/") + "/template.docx"));
InputStream in = new FileInputStream(this.getServletContext().getRealPath("/") + "/[Content_Types].xml");
ContentTypeManager externalCtm = new ContentTypeManager();
externalCtm.parseContentTypesFile(in);
in = new FileInputStream(this.getServletContext().getRealPath("/") + "/document.xml");
Part foreignPart = Load.getRawPart(in, externalCtm, "word/document.xml", null);
template.getMainDocumentPart().addTargetPart(foreignPart);
template.save(new File(this.getServletContext().getRealPath("/") + "/my_new_report.docx"));