Free Support Forum - aspose.com

Merging two documents

Hi,

I have the following scenario I would like to achieve. I have two documents. One document has 3 pages with footers indicating the total number of pages (which is 3). Another document for example has 5 pages with its own custom footers and headers.

I would like to merge these two document into one, first document following by the second one. I also would like first document footers to show 8 pages total (instead of initial 3). The final combined document I need in .doc and .pdf formats.

Is there a way I might be able to do it in Aspose.Words?

Thanks,
Olga

Hi Olga,


Thanks for your inquiry. Please use the Document.appendDocument method to append the specified document to the end of this document. I suggest you please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsjava/Joining+and+Appending+Documents

If the NUMPAGES value is not correct in output Pdf file, please call Document.updatePageLayout method before saving the document to Pdf. This method formats a document into pages and updates the page number related fields in the document such as PAGE, PAGES, PAGEREF and REF. The up-to-date page layout information is required for a correct rendering of the document to fixed-page formats.

This method is automatically invoked when you first convert a document to PDF, XPS, image or print it. However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering again.

Moreover, you can use Document.updateFields method to update the values of fields in the whole document. Please use the following code snippet to achieve your requirements. Hope this helps you. If the problem still remains, please attach your input Word documents here for testing. I will investigate the issue on my side and provide you more information.


<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> docA = new Document(MyDir

  • “A.docx”);<o:p></o:p>

Document docB = new Document(MyDir + "B.docx");

docA.appendDocument(docB, ImportFormatMode.KEEP_SOURCE_FORMATTING);

docA.updateFields();

docA.save(MyDir + "out.docx");