Lock Table of Contents TOC Field & PAGEREF Numbering Fields while Combining / Appending Word DOCX Documents using Java

Hi Team,
I Hope you are doing good, I am currently using the Aspose.Words Java version, and i need to combine two Word documents , the first one is having his own table of contents, and the second one doesn’t have the table of contents.
The issue is : when i append the Second Doc to the First Doc , i get a document where i’m losing TOC numbering. I have tried all the solutions offered on similar topic but i didn’t get any result.
Can you please suggest?
Thanks.
Documents.zip (412.7 KB)

@mss_mohamed,

I am afraid, “First doc.docx” does not contain table of contents. But, I have appended the “First doc.docx” to the “Sec Doc.docx” (which actually contains the TOC field) and attached the output DOCX file here for your reference:

The numbering in above Aspose.Words for Java 21.1 generated DOCX file is same as in the source document. I have used the following Java code to produce above DOCX file:

Document doc1 = new Document("C:\\Temp\\Documents\\First doc.docx");
Document doc2 = new Document("C:\\Temp\\Documents\\Sec Doc.docx");

doc2.appendDocument(doc1, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc2.save("C:\\Temp\\Documents\\awjava-21.1.docx");

Do you see the same problem in this output DOCX? Can you please share your expected DOCX file showing the desired output. You can create this document manually by using MS Word. Please also share the piece of source code that you are using on your end.

Hey thanks for your answer,
i 'm sharing with you a picture of the TOC for the generated Final document , to visualize the main issue, so i’m using this code to append a the “Sec Doc” to the “First Doc”.
1- i get the first doc and the sec doc like you did in your example
2- The append documents : Firstdoc.appendDocument(Secdoc, ImportFormatMode.USE_DESTINATION_STYLES); (KEEP_SOURCE_FORMATTING: doesn’t resolve the issue )
3 - Save the document : Firstdoc.save(filePath, SaveFormat.DOCX);

TOC Image.zip (9.1 KB)

@mss_mohamed,

This screenshot (TOC Image.png) is taken form a document which is generated from another set of documents than the ones you shared in your first post. I see the following TOC numbering (in MS Word 2019) after appending the documents you attached in your first post:

So, please provide the actual Word documents you are getting this problem with.

hello, i’m sharing with you the First and and Second Documents and the result file :
Report.zip (812.6 KB)

@mss_mohamed,

You can Lock the Table of Contents (TOC) field and the related numbering (PAGEREF) fields in first Word document and then append the second DOCX file to it. Please check the following Java code of Aspose.Words API:

Document doc1 = new Document("C:\\Temp\\Report (2)\\First Document.docx");
Document doc2 = new Document("C:\\Temp\\Report (2)\\Second Document.docx");

for (Field field : doc1.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_TOC) {
        field.isLocked(true);
    }
}

for (Field field : doc1.getRange().getFields()) {
    if (field.getStart().getFieldType() == FieldType.FIELD_PAGE_REF) {
        FieldPageRef pageRef = (FieldPageRef) field;
        if (pageRef.getBookmarkName() != null && pageRef.getBookmarkName().startsWith("_Toc")) {
            pageRef.isLocked(true);
        }
    }
}

doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc1.save("C:\\Temp\\Report (2)\\awjava-21.1 - isLocked.docx");

Thanks for your Help, but the problem always occurs, i’m losing the Table of Content.
should i add some modifs to world documents ?

@mss_mohamed,

Another way if to just define the scopes of Table of Content TOC fields by enclosing the entire content of Section (this TOC belongs to) inside separate Bookmarks. Please try the following code:

Document doc = new Document("C:\\Temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

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

        Section sectionOfThisTOC = (Section) tocField.getStart().getAncestor(NodeType.SECTION);
        builder.moveTo(sectionOfThisTOC.getBody().getLastParagraph());

        BookmarkStart bookmarkStart = builder.startBookmark("bookmarked_Scope" + index);
        builder.endBookmark("bookmarked_Scope" + index);
        sectionOfThisTOC.getBody().getFirstParagraph().insertBefore(bookmarkStart, sectionOfThisTOC.getBody().getFirstParagraph().getFirstChild());

        tocField.setBookmarkName("bookmarked_Scope" + index);
        tocField.update();

        index++;
    }
}

doc.updateFields();

doc.save("C:\\Temp\\output.docx");

Thanks so much for your help, i appreciate i found a solution: its add the flowing code after “appendDocument”:

for (com.aspose.words.List list : this.finaldoc.getLists()) { –
list.isRestartAtEachSection(false); –
} –

@mss_mohamed,

It is great that you were able to resolve this issue on your end. Please let us know any time you may have any further queries in future.

For sure … Thanks a lot