Free Support Forum - aspose.com

Append two document each with their own table of contents

I’m trying to combine two documents that each have their own TOC. I want the final output document to have two independent TOC as if the two documents were just pasted one after the other.


At first the paging from the first document was continuing to the second document so I set the Setup.RestartPageNumbering to true on the document I append. This fix the issue.

The second issue is that the two document have their TOC but they both contains page from the two documents. Is there a way to make the two TOC indenpendant?

Thank you
david.gauvreau:
I'm trying to combine two documents that each have their own TOC. I want the final output document to have two independent TOC as if the two documents were just pasted one after the other.

At first the paging from the first document was continuing to the second document so I set the Setup.RestartPageNumbering to true on the document I append. This fix the issue.

The second issue is that the two document have their TOC but they both contains page from the two documents. Is there a way to make the two TOC indenpendant?

Thank you

Hi Pierre,

Thanks for your inquiry. You can meet this requirement by using the following code:

Document doc1 = new Document(MyDir + @"Doc1.docx");

Document doc2 = new Document(MyDir + @"Doc2.docx");

BookmarkStart bmStart1 = new BookmarkStart(doc1, "bm1");

BookmarkEnd bmEnd1 = new BookmarkEnd(doc1, "bm1");

doc1.FirstSection.Body.FirstParagraph.PrependChild(bmStart1);

doc1.LastSection.Body.LastParagraph.AppendChild(bmEnd1);

BookmarkStart bmStart2 = new BookmarkStart(doc2, "bm2");

BookmarkEnd bmEnd2 = new BookmarkEnd(doc2, "bm2");

doc2.FirstSection.Body.FirstParagraph.PrependChild(bmStart2);

doc2.LastSection.Body.LastParagraph.AppendChild(bmEnd2);

foreach (Field field in doc1.Range.Fields)

{

if (field.Type == FieldType.FieldTOC)

{

FieldToc toc = (FieldToc)field;

toc.BookmarkName = "bm1";

}

}

foreach (Field field in doc2.Range.Fields)

{

if (field.Type == FieldType.FieldTOC)

{

FieldToc toc = (FieldToc)field;

toc.BookmarkName = "bm2";

}

}

doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);

doc1.Save(MyDir + @"16.12.0.docx");


Best regards,