Merge Word documents and Insert Table of Content

Hello,
I have to merge multiple word documents, insert Table of Content and convert it into PDF. I could get everything to work but the only issue is with the table of content. I need TOC on second page as the first one has the title. The following code inserts it on the first page:

DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-5\" \\h \\z \\u");

Is there a way to move it to second page?
Thanks,
Luckie

Hi Luckie,

Thanks for your inquiry.

Please use the LayoutCollector.GetStartPageIndex method to get the page number of a node. This method gets 1-based index of the page where node begins. Returns 0 if node cannot be mapped to a page.

Please use the following code example to insert the TOC field at page number. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int page = 1;
LayoutCollector lc = new LayoutCollector(doc);
// Loop though Paragraphs
for (Paragraph para :(Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (lc.getStartPageIndex(para) == 2)
    {
        builder.moveTo(para);
        builder.insertTableOfContents("\\o \"1-5\" \\h \\z \\u");
        break;
    }
}
doc.updateFields();
doc.save(MyDir + "Out.docx");

Hi Tahir,

Thanks for the reply. I tried out your suggestion. The ToC moved to the second page. But the problem now is that it’s inserted after the Heading on Page 2.
Entire Page 1 is the title which contains an image a 2 title lines and page two is the actual verbiage with headings and paragraphs.
Is there a way that the ToC can be moved above the heading on Page 2?

Hi Luckie,

Thanks for your inquiry.

First of all, please note that Aspose.Words is quite
different from the Microsoft Word’s Object Model in that it represents
the document as a tree of objects more like an XML DOM tree. If
you worked with any XML DOM library you will find it is easy to
understand and work with Aspose.Words. When you load a Word document
into Aspose.Words, it builds its DOM and all document elements and
formatting are simply loaded into memory. Please read the following
articles for more information on DOM:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
https://docs.aspose.com/words/net/logical-levels-of-nodes-in-a-document/

Secondly, You can obtain where the builder’s cursor is currently positioned at any time. The DocumentBuilder.CurrentNode property returns the node that is currently selected in this builder. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/document-builder-overview/
https://docs.aspose.com/words/net/navigation-with-cursor/

It would be great if you please share your input and expected output document with us. We will then provide you more information about your query along with code.

Hello Tahir,

Thank you for the reply. I’ve attached following three documents:

  1. testInput.docx : Input file
  2. testOutput.docx : Output file obtained using the following code.
builder.insertTableOfContents("\o "1-5" \h \z \u");
  1. testDesiredOutput.docx : output that I actually desire.
  2. testOutput2.docx : Output generated using the solution provided. (using LayoutCollector)

Hi Luckie,

Thanks for sharing the documents. Please use the following modified code to achieve your requirement. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "testInput.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector lc = new LayoutCollector(doc);
// Loop though Paragraphs
for (Paragraph para :(Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (lc.getStartPageIndex(para) == 2)
    {
        builder.moveTo(para);
        if (para.previousPreOrder(doc).getNodeType() == NodeType.RUN)
            builder.moveTo(para.previousPreOrder(doc).getParentNode());
        builder.insertBreak(BreakType.PARAGRAPH_BREAK);
        builder.insertTableOfContents("\\o \"1-5\" \\h \\z \\u");
        builder.insertBreak(BreakType.PAGE_BREAK);
        break;
    }
}
doc.updateFields();
doc.save(MyDir + "Out.docx");

Hi Tahir,

Thanks for the response. The fix you provided solved my issue. I’m new to Aspose, I appreciate the support.

We are using Aspose Words for Java and have a requirement to convert an excel document to word. Is this possible? Or at least can we embed Excel sheet as tables in a Word document?

Thank you,
Luckie

Hi Luckie,

Thanks for your inquiry.

*Luckie:

We are using Aspose Words for Java and have a requirement to convert an excel document to word. Is this possible?*

Unfortunately, there is no direct way to convert Excel to Word document. However, it is possible using Aspose.Words and Aspose.Cells. Please check following forum link for detail:
https://forum.aspose.com/t/54874

PS: Please note that Microsoft Word and Microsoft Excel documents are completely different file formats; so, some features that are available in Microsoft Word are not available in Microsoft Excel and vice versa and it is hard to convert from Word to Excel and from Excel to Word with 100% fidelity.

*Luckie:

Or at least can we embed Excel sheet as tables in a Word document?*

Unfortunately, inserting new OLE objects into Word documents and updating existing OLE objects is not supported at the moment.

We had already logged this feature request as WORDSNET-1206 in
our issue tracking system. I have linked this forum thread to the same
feature and you will be notified via this forum thread once this feature
is available.

We apologize for your inconvenience.

Hi Tahir,

Thank you for the response and support.

Thanks,
Luckie

The issues you have found earlier (filed as WORDSNET-1206) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(42)

Hello Tahir,

The solution you provided above worked for a while but now I see the following exception at line: if (lc.getStartPageIndex(para) == 2).

Exception: Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at com.aspose.words.ëZXB.ëZHZ(Unknown Source)
at com.aspose.words.ëZXB.ëZ(Unknown Source)
at com.aspose.words.LayoutCollector.ë4n(Unknown Source)
at com.aspose.words.LayoutCollector.ë4p(Unknown Source)
at com.aspose.words.LayoutCollector.getStartPageIndex(Unknown Source)
at ManualsMerge.TOCMove(ManualsMerge.java:169)
at ManualsMerge.main(ManualsMerge.java:39)

Please help.

Hi Luckie,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.6.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document along with code here for testing. I will investigate the issue on my side and provide you more information.