".Error! Bookmark not defined." getting added

Hi ,
I’m using Aspose.Words-25.7.0.jar to convert docx to pdf. But the output pdf contains the words “.Error! Bookmark not defined” in the page 2
input.zip (808.6 KB)

which is not present in the input file. Kindly look into this issue

@rsv2

It sounds like you’re encountering an issue where the output PDF contains the message “.Error! Bookmark not defined,” which is not present in your original DOCX file. This typically happens when there are bookmarks in the document that are not properly defined or are missing their corresponding content.

Cause: The error arises from bookmarks in the DOCX file that do not have corresponding content, leading to the error message in the PDF output.

Solution: Here are some steps you can take to troubleshoot and resolve this issue:

  1. Check Bookmarks in the DOCX File: Open your DOCX file in Microsoft Word and check for any bookmarks that may not have corresponding content. You can do this by going to the “Insert” tab, selecting “Bookmark,” and reviewing the list.

  2. Update Fields: If your document contains fields that reference bookmarks, ensure that all fields are updated. In Word, you can select all (Ctrl + A) and then press F9 to update fields.

  3. Remove or Redefine Bookmarks: If you find any bookmarks that are not needed or are incorrectly defined, consider removing them or redefining them properly.

  4. Use Aspose.Words to Clean Up: You can programmatically check for and remove any undefined bookmarks using Aspose.Words. Here’s a simple code snippet to remove bookmarks that are not defined:

    Document doc = new Document("input.docx");
    BookmarkCollection bookmarks = doc.getRange().getBookmarks();
    for (int i = bookmarks.getCount() - 1; i >= 0; i--) {
        Bookmark bookmark = bookmarks.get(i);
        if (bookmark.getText().isEmpty()) {
            bookmarks.remove(bookmark.getName());
        }
    }
    doc.save("output.pdf");
    
  5. Test with a Different Version: If the issue persists, consider testing with a newer version of Aspose.Words, as there may have been bug fixes or improvements related to bookmark handling.

Next Steps: Please follow the steps outlined above to resolve the issue. If you continue to experience problems, feel free to reach out for further assistance.

Thank you, and I look forward to hearing if these steps help resolve the issue!

@Professionalize.Discourse
I was using Aspose.Words of version 22.7 where I didn’t see this issue
and I’ve been using the same input docx
I’ve tried the same input file with version 25.1.0 and 25.7.0 where the issue is created
There are multiple docx files and manually change in each file is difficult.
Can you provide the reason behind this issue which happened on the version change

@rsv2 The behavior is expected. If you convert your document to PDF using MS Word, the result is the same:
ms.pdf (468.9 KB)

You can skip updating a certain type of field, you can simply lock them or unlink. Please see the following simple code:

Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_PAGE_REF)
        f.isLocked(true);
}
doc.save("C:\\Temp\\out.pdf");

out.pdf (332.2 KB)

Or you can disable updating fields upon document rendering:

Document doc = new Document("C:\\Temp\\in.docx");
PdfSaveOptions opt = new PdfSaveOptions();
opt.setUpdateFields(false);
doc.save("C:\\Temp\\out.pdf", opt);