Merging two word Documents into one Word Document

1
Hi I have to generate a word document in mendix in which the whole first page has a static image where as from second page onwards there is text. Requirement: First page - image with no margins.                          From second page onwards - left, top and bottom margins should be there as its text What I tried:  When I tried to generate everything in a single word document, what is happening is, the image page(first page) is also getting generated with left, bottom and top          margins as I have configured the properties for it to leave margin spaces. Here, my requirement is not fulfilled.   So, do I have to print the first page without margins in one word document and text pages in other word document and merge these two? Or is there any other way I can fulfill my requirement? Or if at all I have to merge the Documents, how do I do it?
asked
1 answers
2

I don't see an option in the document templates directly, so you might investigate the word merge module from the appstore:

https://appstore.home.mendix.com/link/app/2026/Appronto/Word-Document-Merge-4-Mendix

Alternatively you could try to implement your own merging in java, see below for some starter code that will merge 2 docx files when given as inputstreams:

public static void merge(InputStream src1, InputStream src2, OutputStream dest) throws Exception {
	    OPCPackage src1Package = OPCPackage.open(src1);
	    OPCPackage src2Package = OPCPackage.open(src2);
	    XWPFDocument src1Document = new XWPFDocument(src1Package);        
	    CTBody src1Body = src1Document.getDocument().getBody();
	    XWPFDocument src2Document = new XWPFDocument(src2Package);
	    CTBody src2Body = src2Document.getDocument().getBody();        
	    appendBody(src1Body, src2Body);
	    src1Document.write(dest);
	}

	private static void appendBody(CTBody src, CTBody append) throws Exception {
	    XmlOptions optionsOuter = new XmlOptions();
	    optionsOuter.setSaveOuter();
	    String appendString = append.xmlText(optionsOuter);
	    String srcString = src.xmlText();
	    String prefix = srcString.substring(0,srcString.indexOf(">")+1);
	    String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
	    String sufix = srcString.substring( srcString.lastIndexOf("<") );
	    String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
	    CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);
	    src.set(makeBody);
	}

Be aware that this will add the content of two word files into 1, but needs work on the margins for page 1, so not there yet but maybe a starting point for your app.

answered