I have docx file which is a template document(let's say it temp.docx), having header and footer. Also i have a another docx file which is having text for final document(let's say data.docx). Well My requirement is to create a document by inserting all the text and formatting information from 'data' to 'temp' file.
I tried following code:
- Code: Select all
WordprocessingMLPackage masterDoc = WordprocessingMLPackage.load(new FileInputStream(new File(masterTemplatePath)));
WordprocessingMLPackage customDoc = WordprocessingMLPackage.load(new FileInputStream(new File(customTemplatePath)));
WordprocessingMLPackage finalDoc = new WordprocessingMLPackage();
MainDocumentPart masterDocumentPart = masterDoc.getMainDocumentPart();
List masterTexts = masterDocumentPart.getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
Map<String, String> stylesUsedInMaster = masterDocumentPart.getStylesInUse();
MainDocumentPart customDocumentPart = customDoc.getMainDocumentPart();
List customTexts = customDocumentPart.getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
Map<String, String> stylesUsedInCustom = customDocumentPart.getStylesInUse();
String masterDocumentInXML = XmlUtils.marshaltoString(masterDocumentPart.getJaxbElement(),false,false,customDocumentPart.getJAXBContext());
String customDocumentInXML = XmlUtils.marshaltoString(customDocumentPart.getJaxbElement(),false,false,customDocumentPart.getJAXBContext());
masterDocumentPart.addObject(customDocumentPart.getJaxbElement());
SaveToZipFile saver = new SaveToZipFile(masterDoc);
System.out.println("Saving file to output Stream");
Boolean save = saver.save(new File(finalDocumentPath));
But with this code I am getting only tempalte document content.
Could someone provide some input on it.
Thanks in advance.