Update Page Numbering of Contents Index TOC Field (Table of Contents) in Word Document & Convert to PDF using Java

Dear Sir or Madam,

I have used the latest version of aspose.words (20.9.0).

I worked with com.aspose.words.Document. I used one file as template and added some other files as chapters to the document with option USE_DESTINATION_STYLES.

Also I used my self designed fonts, that I already have set before generate the PDF file.

Then I create a pdf with the following snipet of code:

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        pdfSaveOptions.setPrettyFormat(true);
        pdfSaveOptions.setOptimizeOutput(true);
        pdfSaveOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_ALL);
        pdfSaveOptions.setPageMode(PdfPageMode.FULL_SCREEN);
        pdfSaveOptions.setTextCompression(PdfTextCompression.NONE);
        pdfSaveOptions.setUseCoreFonts(false);
        pdfSaveOptions.setUseHighQualityRendering(true);
        pdfSaveOptions.setUseBookFoldPrintingSettings(true);
        pdfSaveOptions.setPreserveFormFields(true);
document.save("filename.pdf", pdfSaveOptions);

At the end I receive a PDF file with wrong page numbers in the index section.

In the attachments you can find the files and fonts I have already leveraged, as well as the produced PDF file.
In the index section there is a second chapter in the page 33, but in the document it is actually in the page 34.
Also chapter 1.14 is in index in the page 85, however, in the document it is in the page 89.

Could you please help me?

Thanks in advanced.

TestData.zip (2.5 MB)

@Anatoli2008,

Please ZIP and attach the following resources here for testing:

  • Please also save your final output to DOCX format and provide the final DOCX Word document here for our reference.
  • Please also create a standalone simplified Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words’ JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your particular issue and provide you more information.

@awais.hafeez ,

thanks for quick answer.
All related files are now added in new zip file

Test Data.zip (2.8 MB)

@Anatoli2008,

What we understand is that the Table of Content (TOC field) is not reflecting the correct page numbers for some of the entries. Can you please also create and attach here a comparison screenshot which highlights the problematic areas in Aspose.Words 20.9 generated document?

I am afraid, your Java code is causing a few compile time errors on our end. For example, we are unable to resolve following lines:

import java.util.concurrent.atomic.AtomicReference;
AtomicReference<Boolean> isFirstSection = new AtomicReference<>(true);
isFirstSection.get()
isFirstSection.set(false);

We request you to please simplify your code and attach a minimal new code that still reproduces the same problem. Thanks for your cooperation.

Hello,

yes, the page numbers in TOC shows wrong numbers for entries.
Specially, the wrong numbers occurs after rendering specific parts in documents. After that, the pages are shifted for all next entries for 1, 2, 3 and so long.

When I do not use the fonts

pdfSaveOptions.setPreserveFormFields(true)

then I have correct page numbers, but whole text in document is smaller and not formatted.

I add in output folder a picture with page number in TOC and in red the page in document.
Also i have some reduced the code to be executable.

Thanks in advanced.

Test Data v3.zip (2.8 MB)

@Anatoli2008,

We are working on your query and will get back to you soon.

@Anatoli2008,

For the sake of any correction in Aspose.Words API, we have logged this problem in our issue tracking system with ID WORDSNET-21222. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for any inconvenience.

@Anatoli2008,

Regarding WORDSNET-21222, the issue with page numbers is reproducible on conversion of the output .docx you provided (“TestDocument.docx” in Output folder of the java app") to PDF, with Audi fonts installed from Generator-DATA\Fonts folder:

21222.out.AW.pdf (321.1 KB) vs 21222.out.msword.FontsInstalled.pdf (950.4 KB)

The issue, however, goes away if Document.UpdateFields() is called before saving to PDF. see 21222.out.AW.UpdateFields.pdf (321.0 KB)

It seems that in your application the layout was generated without taking fonts into account. It seems the issue might be that UpdateFields() that implicitly generates the layout model (in order to get the page numbers) is called before the font source with Audi fonts is specified in savePDF() method.

So please set the custom font source before UpdateFields() (or any other action that implicitly builds the layout model such as requesting Document.PageCount) is invoked and see how it goes on your end? Alternatively, UpdatePageLyaout() call can be added after setting font source in savePDF(), but this way an expensive operation will be performed twice. Please let me know if the problem goes away with the proposed changes. Thanks for your cooperation.

Hello,
the Document.UpdateFields() after setting of Fonts was the solution for our problem.
Thank you very much.

Best regards

@Anatoli2008,

Thanks for your feedback. We have completed the work on WORDSNET-21222 and closed this issue with “not a bug” status.