Page number references in TOC are lost when converting DOCX to PDF

Hello,
We are currently evaluating Aspose.Words version 24.1 for upgrade, and
noticed one defviation from the prior behavior (currently production version 21.7.0).
The page number references in table of content are getting lost after converting TOC document to PDF.
Attached is zip archive illustrating the problem, it contains:

  • tocDoc_updatedPageNumbers.docx - table of content with page references numbers;
  • 21_7_tocDoc_updatedPageNumbers.pdf - output created with Aspose.Words 21.7;
  • 22_4_tocDoc_updatedPageNumbers.pdf - output created with Aspose.Words 22.4;
  • 24_1_tocDoc_updatedPageNumbers.pdf - output created with Aspose.Words 24.1 (shows “Error! Bookmark not Defined” instead of page numbers) ;
  • TestWordTOCToPDFTransform.java - small test program that shows the issue.

This issue occurs under both Linux 7 and Windows 11 OS.
Please let us know if you need additional information.
Thank you.
PageNumbersRefsNotFound.zip (102.7 KB)

@oraspose The behavior is expected. The fields in the document are automatically updated when document is saved to PDF, since there are no PAGEREF bookmarks in the document, the errors are shown as field results. You will see exactly the same if update fields in MS Word or save document to PDF using MS Word. You can disable updating fields in the document upon saving to PDF to get the desired result:

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

We can’t disable updating fields in the document, as result is “Error! Bookmark not defined”.
Is there or could a new option be introduced that would allow disable only updating of the PAGEREF field in the table of content.
Attached is the sample TOC document which contains PAGEREF nodes with values that represent page numbers in other documents. Upon transformation to PDF we would need to have only these fields not to be updated in order to prevent “Bookmark not defined” error.
Thank you,
expectedTOCDoc.zip (139.0 KB)

@oraspose If it is required to 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 (140.5 KB)

Thank you, Alexey, that workaround resolved the issue.

1 Like