Page 1 of 1

How to insert new document pages in another docx

PostPosted: Thu Aug 12, 2010 11:26 pm
by tomandersen
Hi Team,

I am trying to insert few document pages (having contents like text, tables etc.) inside another document (docx format) to a specific location.

Actually I have one report (in docx format) which is getting generated from my application. Now I want to add few more contents in the form of new document pages to the existing document, before my application generates the final report.

Please see the code I have written:

public void callXYZr(HttpServletResponse response, ByteArrayRequestEntity byteArrayRequestEntity, ServiceContext scx ,String pType) throws IOException{

File tempWordTemplate =null;
try {
tempWordTemplate = File.createTempFile("newWordTemplate", ".docm");

// call to Database for fetching word template
WordTemplateDAO templateDao = new WordTemplateDAO(scx);
byte[] binData = templateDao.fetchWordDocument(pType);


OutputStream fs = new BufferedOutputStream( new FileOutputStream(tempWordTemplate));
fs.write(binData);
fs.flush();
fs.close();

WordprocessingMLPackage wmlPack = WordprocessingMLPackage.load(tempWordTemplate);
........... some more stuff.... goes here...
}

Now i need to put few more contents (word doc pages) inside a perticular location of the above generated docx file, let say in the middle of the document.
The new content is also another doc file whcih I have converted into binarydata format to put in the document. Is this approach right?

Thanks a lot in advance :)

How can I achieve this.. Please help me

Re: How to insert new document pages in another docx

PostPosted: Fri Aug 13, 2010 5:13 am
by jason
tomandersen wrote:The new content is also another doc file whcih I have converted into binarydata format to put in the document. Is this approach right?


If this other doc is a docx file, you can open it as a WordMLPackage, get its MainDocumentPart, then find the content within that that you want, and attach that to you first package (all these steps are explained in the Getting Started guide). Things which have relationships (images, hyperlinks) need to be specially handled.

Or you could look at altChunk (which may be appropriate if you want to retain different styles in the fragment you are fetching).

Re: How to insert new document pages in another docx

PostPosted: Mon Aug 16, 2010 5:53 pm
by tomandersen
Hi Jason,
Thanks a lot for the quick reply.
However my new document is not in the form of docx they are the old .doc format.
That is why I was trying to convert it in binarydata form. The solution you have mentioned cant work for .doc??


Regards,
Andersen

Re: How to insert new document pages in another docx

PostPosted: Mon Aug 16, 2010 6:04 pm
by jason
I think you have 2 options for inserting the old binary .doc.

Your first is to insert it as an OleObjectBinaryPart. If you can add it as an OLE object in Word, and save the resulting document as a docx, you'll be able to see what the resulting structures look like. From there, you ought to be able to reproduce with some effort. (I recall that adding a PDF as an OLE object was not staightforward - see previous posts on this topic).

The other is to convert the binary .doc to a .docx. There are recent prior posts on this as well.

cheers .. Jason

Re: How to insert new document pages in another docx

PostPosted: Mon Aug 16, 2010 6:50 pm
by tomandersen
Tanks Jason.. I will look into both the possibilities you have suggested.
Thanks again.... :)

Re: How to insert new document pages in another docx

PostPosted: Tue Aug 17, 2010 5:59 pm
by tomandersen
Hello Jason,

As you have suggested one solution as below:
"If this other doc is a docx file, you can open it as a WordMLPackage, get its MainDocumentPart, then find the content within that that you want, and attach that to you first package (all these steps are explained in the Getting Started guide). Things which have relationships (images, hyperlinks) need to be specially handled."

Suppose the new document has .docx format then how can I acheive this(adding a new docx to the old docx at a perticular position) let say I want to add my new pages(in docx format) after/before a paragraph heading "XYZ is ABC" in the old docx file.

Thanks a ton in advance....

Regards,
Andersen

Re: How to insert new document pages in another docx

PostPosted: Wed Aug 18, 2010 1:51 am
by jason
You can find that position by traversing the document content, or by using XPath. See the Getting Started guide.