We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add Table of Contents in Word Document | Define Scope of Multiple TOC Fields using Bookmarks in Java | Append Merge Word Documents

Hi Team,

I want to add 2 bookmarks here to generate a table of content for two different sections.
Please help me to add separate bookmarks.

In the attached sample documents there are two separate tables of contents.

I want to add two different bookmarks for both the table of contents and also want to update that particular toc after insertion of bookmarks to the section.

Please find the attached sample document for reference.

Actual_generated_document.zip (40.6 KB)

I am able to insert one bookmark for the last section with the help of below code snippet.Its specific to TOC 2.

I want similar way to insert a bookmark for TOC 1.
It will be really helpful for me if you provide me the solution as soon as possible.

Document doc = new Document(“E:\Temp\Sample_documents\Actual_generated_document.docx”);

com.aspose.words.DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();

BookmarkStart bookmarkStart = builder.startBookmark(“bookmarked_Scope”);
builder.endBookmark(“bookmarked_Scope”);

doc.getLastSection().getBody().getFirstParagraph().insertBefore(bookmarkStart, doc.getLastSection().getBody().getFirstParagraph().getFirstChild());

for (Field field : doc.getLastSection().getBody().getRange().getFields()) {
if (field.getType() == FieldType.FIELD_TOC) {
FieldToc tocField = (FieldToc) field;
tocField.setBookmarkName(“bookmarked_Scope”);
tocField.update();
break;
}
}

doc.save(“E:\Temp\Sample_documents\awjava-20.3.docx”);

@upratik,

The following Java code will define the scopes of all the Table of Content TOC fields in Word document by enclosing the entire content of the Sections (this TOC field belongs to) inside separate Bookmarks.

Document doc = new Document("E:\\Temp\\Actual_generated_document\\Actual_generated_document.docx");
com.aspose.words.DocumentBuilder builder = new DocumentBuilder(doc);

int i = 1;
for (Field field : doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_TOC) {
        FieldToc tocField = (FieldToc) field;

        Section tocSection = (Section) tocField.getStart().getAncestor(NodeType.SECTION);
        if (tocSection != null) {
            builder.moveTo(tocSection.getBody().getLastChild());

            BookmarkStart bookmarkStart = builder.startBookmark("bookmarked_Scope" + i);
            builder.endBookmark("bookmarked_Scope" + i);

            tocSection.getBody().getFirstParagraph().insertBefore(bookmarkStart, tocSection.getBody().getFirstParagraph().getFirstChild());

            tocField.setBookmarkName("bookmarked_Scope" + i);
            // tocField.update();
        }

        i++;
    }
}

doc.updateFields();

doc.save("E:\\Temp\\Actual_generated_document\\awjava-20.3.docx"); 

Hope, this helps.

@awais.hafeez

Thank a lot for your quick response and the above solution.

But now I am facing a new scenario that I am explaining below.

Step 1: I have three documents i.e. Document1, Document2, and Document3.

Step 2: I’ll have to merge two documents i.e. Document1 and Document2 and create
the final merged document named “merged_document.docx”.

Step 3: Now I’ll have to add bookmarks for these two documents i.e.
merged_document.docx. and Document3

Step 4: After adding the bookmarks I want to generate a table of content for both the
documents i.e. merged_document.docx. and Document3 separately.

Step 5: I’ll have to merge these two documents which will contain separate TOC based
on their bookmarks and this will be the final document “Final_document.docx”.

This is the overall scenario that I want to achieve. As I am new to this document things so I am facing the issue to create bookmarks for the documents and to generate TOC based on Bookmarks.

Currently, I am using the below code snippet to generate the table of content but it does not generate a table of content based on bookmarks.

builder.insertTableOfContents("\o “1-3” \h \z \u");

I want your help to figure out the above scenario it will be really helpful for me to enhance my learning of Aspose

I am attaching the three sample documents with my query on which i want to process the above steps.

Bookmarks_docuemnts.zip (33.0 KB)

Regards,
Uddeshya Pratik

@upratik,

We are checking this scenario and will get back to you soon.

@awais.hafeez

It will be really helpful if you get some time to look this scenario.

Thanks ,
uddeshya Pratik

@upratik,

You can build logic on the following code to meet this requirement:

Document doc1 = new Document("E:\\Temp\\Bookmarks_docuemnts\\Document1.docx");
Document doc2 = new Document("E:\\Temp\\Bookmarks_docuemnts\\Document2.docx");
Document doc3 = new Document("E:\\Temp\\Bookmarks_docuemnts\\Document3.docx");

// create merged_document.docx in memory
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);

com.aspose.words.DocumentBuilder builder1 = new DocumentBuilder(doc1);
com.aspose.words.DocumentBuilder builder3 = new DocumentBuilder(doc3);

// generate a table of content for both the documents
builder1.moveToDocumentStart();
builder3.moveToDocumentStart();
FieldToc toc1 = (FieldToc) builder1.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
FieldToc toc3 = (FieldToc) builder3.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
toc1.setBookmarkName("bookmarked_Scope1");
toc3.setBookmarkName("bookmarked_Scope3");

// Bookmark merged_document.docx
builder1.moveTo(doc1.getLastSection().getBody().getLastChild());
BookmarkStart bookmarkStart = builder1.startBookmark("bookmarked_Scope1");
builder1.endBookmark("bookmarked_Scope1");
doc1.getFirstSection().getBody().getFirstParagraph().insertBefore(bookmarkStart, doc1.getFirstSection().getBody().getFirstParagraph().getFirstChild());

// Bookmark Document3.docx
builder3.moveTo(doc3.getLastSection().getBody().getLastChild());
BookmarkStart bookmarkStart3 = builder3.startBookmark("bookmarked_Scope3");
builder3.endBookmark("bookmarked_Scope3");
doc3.getFirstSection().getBody().getFirstParagraph().insertBefore(bookmarkStart3, doc3.getFirstSection().getBody().getFirstParagraph().getFirstChild());

doc1.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc1.updateFields();
doc1.save("E:\\Temp\\Bookmarks_docuemnts\\Final_document.docx");

@awais.hafeez

Thanks a lot.It is working as the requirements :slight_smile:

Just a query can we add a custom title also in the table of content.

Please find the attached screenshot for more clarity

toc.PNG (5.0 KB)
.

@upratik,

Sure, you can add custom Titles above the Table of Content TOC fields and apply different font formatting to them by adding the following Java code to my previous code:

...
...
// generate a table of content for both the documents
builder1.moveToDocumentStart();
builder3.moveToDocumentStart();

builder1.getFont().setSize(20);
builder1.getFont().setBold(true);
builder1.writeln("PROTOCOL TABLE OF CONTENTS 1");
builder1.getFont().clearFormatting();

builder3.getFont().setSize(20);
builder3.getFont().setBold(true);
builder3.writeln("PROTOCOL TABLE OF CONTENTS 2");
builder3.getFont().clearFormatting();

FieldToc toc1 = (FieldToc) builder1.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
FieldToc toc3 = (FieldToc) builder3.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
toc1.setBookmarkName("bookmarked_Scope1");
toc3.setBookmarkName("bookmarked_Scope3");
...
...

Hope, this helps.