Free Support Forum - aspose.com

Word Combining Documents

Hello,

I am using the code below to combine two documents together:

Aspose.Words.Document dstDoc = new Aspose.Words.Document(@"F:\Temp\1.doc");

Aspose.Words.Document srcDoc = new Aspose.Words.Document(@"F:\Temp\2.doc");

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

AppendDoc(dstDoc, srcDoc);

dstDoc.Save(@"f:\temp\res.doc");

..

public void AppendDoc(Aspose.Words.Document dstDoc, Aspose.Words.Document srcDoc)

{

foreach (Aspose.Words.Section srcSection in srcDoc)

{

Aspose.Words.Section newSection = (Aspose.Words.Section)dstDoc.ImportNode(srcSection, true, Aspose.Words.ImportFormatMode.KeepSourceFormatting);

dstDoc.Sections.Add(newSection);

}

}

On occasion, the joined document has a page break between the two documents although when I view the formatting of the individual documents in word they do not have page breaks. The reason it is happening is because the PageWidth and PageHeight are very slightly different between the two documents. I have been able to resolve it by setting the PageWidth/PageHeight of the two documents to be the same - is this behaviour by design or perhaps is this a bug that I have identified. If there is another work-around you could recommend I would appreciate details.

Thank you very much for your help

Regards

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. There can’t be two sections with different PageSetups on one page. So as a workaround you can just append content of the source section to the last section of the destination document. See the following code for example.

Document dstDoc = new Document(@"Test143\in.doc");//Paper size of this document is A4

Document srcDoc = new Document(@"Test143\in2.doc");//Paper size of this document is Letter

foreach (Section srcSection in srcDoc)

{

//Import section

Section newSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);

//Append content of the src section to last section of dst document

dstDoc.LastSection.AppendContent(newSection);

}

//Save output

dstDoc.Save(@"Test143\out.doc");

Also you can use the following code.

Document dstDoc = new Document(@"Test143\in.doc");//Paper size of this document is A4

Document srcDoc = new Document(@"Test143\in2.doc");//Paper size of this document is Letter

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

foreach (Section srcSection in srcDoc)

{

srcSection.PageSetup.PageWidth = dstDoc.LastSection.PageSetup.PageWidth;

srcSection.PageSetup.PageHeight = dstDoc.LastSection.PageSetup.PageHeight;

//Import section

Section newSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);

dstDoc.Sections.Add(newSection);

}

//Save output

dstDoc.Save(@"Test143\out.doc");

I hope this could help you.

Best regards.