Free Support Forum - aspose.com

Merging Word Document- Updating TOC from Aspose results in one "Error! Bookmark not Defined"

I am not programmatically creating contents, I am taking some documents and merging them together using aspose. The first page is a title page, it has the titlepage and an empty TOC page after it. The next pages are sections. The merge works as expected, before saving the Aspose.Document, I do a doc.updateFields()



When I open the document, there is one item in the TOC that has “Error! Bookmark not defined”. I can right-click the TOC in Word and update and it is corrected. I am wondering if there might be a bug in aspose causing this or if there is anything I can do to work around this issue.






import com.aspose.words.Document;

import com.aspose.words.FileFormatInfo;

import com.aspose.words.FileFormatUtil;

import com.aspose.words.ImportFormatMode;

import com.aspose.words.LoadFormat;

import com.aspose.words.SectionStart;





public class DocConcepting {

public static void main(String[] args) throws Exception {

Document doc = new Document(“SampleDocuments/TitlePage.docx”);

//DocumentBuilder builder = new DocumentBuilder(doc);

String[] docList = new String[] {“Section1.docx”,“Section2.docx”,“Page1.docx”, “Page2.docx”, “SomeText.doc”,“TextOnly.txt”};

for(int i=0;i<docList.length; i++) {

Document appendDoc = new Document(“SampleDocuments/” + docList[i]);



// if(docList[i].endsWith(“txt”)) {

// // See what the text conversion looks like for our list data

// appendDoc.save(“SampleDocuments/ConvertedText.docx”);

// }

appendDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

appendDoc.getFirstSection().getHeadersFooters().linkToPrevious(true);



doc.appendDocument(appendDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

}



doc.updateFields();

doc.save(“Output/MergeOut.docx”);

System.out.println(“Done!”);

}

Hi Michael,


Thanks for your inquiry. Please add following line before calling updateFields method:

doc.updatePageLayout();

I hope, this helps.

Best regards,

Works well, thank you for the response