Table of contents page numbers off after inserting the TOC field

Hello!

I am using the aspose-words-cloud-ruby gem to genereate docx files from html documents. I am running into an issue where after inserting the TOC field to the converted document the table of contents often has some incorrect page number values for the headers.
Here is some more context:

  • I am using a self-hosted container which uses the default aspose docker container
  • Given that the aspose container I am using has access to the DejaVu fonts by default, I harded-code all my text in the html to use DejaVu to rule out an unavailable font causing layout issues
  • The longer the doc the more the page numbers seem to be off

I will reply to this with a ruby script that contains code to generate an example document that reproduces the issue. This example generates a document where the final heading (titled ‘End of the document’) shows up in the table of contents as being on page 50. However when I open the document in microsoft word (ensuring that the Dejavu font is installed) the header actually shows up on page 51. I have generated longer examples (documents with hundreds of pages) where the table of contents page values are dozens of pages off. Is there any modification I should be making to ensure the page values in the table of contents are correct?

Thank you

Here is a ruby script to reproduce the issue I am seeing.
test_convert.rb.zip (2.9 KB)

@Adampog

It seems you are encountering issues with incorrect page numbers in the table of contents (TOC) after converting HTML to DOCX using the Aspose.Words library. This can occur if the TOC is not updated properly after the document generation.

To ensure that the page numbers in the TOC are accurate, follow these steps after inserting the TOC field:

  1. Update the TOC Fields: Use the UpdateFields method to populate the TOC with entries. Initially, this will display placeholders for page numbers.

  2. Update the Page Layout: After updating the fields, call the UpdatePageLayout method. This step is essential as it calculates and applies the correct page numbers to the TOC.

Here’s a simple example of how to implement this in Ruby:

require 'aspose_words_cloud'

# Initialize the API
words_api = AsposeWordsCloud::WordsApi.new

# Load your document
document_name = 'your_document.docx'
response = words_api.upload_file(File.open(document_name, 'rb'), document_name)

# Insert TOC
toc_field = '{ TOC \\o "1-3" \\h \\z \\u }'
words_api.insert_field(document_name, toc_field, 0)

# Update TOC fields
words_api.update_fields(document_name)

# Update page layout
words_api.update_page_layout(document_name)

# Save the updated document
words_api.save_as(document_name, 'updated_document.docx')

Make sure to replace 'your_document.docx' with the actual path to your document. This code will ensure that the TOC is correctly populated and that the page numbers reflect the actual layout of the document.

If you continue to experience discrepancies, check the formatting of your headings in the HTML to ensure they are recognized as headings in the DOCX format.

If you have further questions or need additional assistance, feel free to ask!

I am using update_fields in my example. However, I cannot use update_page_layout since that does not exist on the ruby gem.

Thanks for sharing example, I will check it out.

1 Like