Wrong PageCount on docx file with Aspose.Words .NET

Dear support team,

I am running a simple unit test with an input docx document.
Each time, this document contains 5 pages.

But when I load it with Aspose.Words and then iterate on pages based on PageCount, there is an error since PageCount returns 4.

I’ve tried it with various documents, but each time there was an issue.

Could you explain me if I’m doing anything wrong here ?

Version of Microsoft 365 the Word document was created with : 16.0.19029.20244

Regards,


        /// <summary>
        /// Content of the license file for initialisation purposes
        /// </summary>
        private static string License = "INSERT A VALID LICENSE HERE";

        /// <summary>
        /// Get the licence content
        /// </summary>
        /// <returns>A MemoryStream for the content of the license</returns>
        private static MemoryStream GetLicence()
        {
            byte[] buf = System.Text.Encoding.UTF8.GetBytes(License);
            MemoryStream ms = new MemoryStream(buf);
            return ms;
        }

        private static bool InitWords()
        {
            try
            {
                Aspose.Words.License l = new Aspose.Words.License();
                l.SetLicense(GetLicence());
            }
            catch (Exception e)
            {
                Console.WriteLine("AsposeConverter.InitPdf", e);
                return false;
            }

            return true;
        }



        public AsposePdfTest()
        {
            InitWords();
        }

        [Fact]
        public void AsposeWords_CorrectlyIdentifiesPageCount()
        {
            var document = new Document("TestInput_PagesCount.docx");
            Assert.Equal(5, document.PageCount); // why 4 pages?

            var document2 = new Document("TestInput_PagesCount2.docx");
            Assert.Equal(5, document2.PageCount); // why 4 pages?

            var document3 = new Document("TestInput_PagesCount3.docx");
            Assert.Equal(5, document3.PageCount); // why 4 pages?
        }

TestInput_PagesCount.docx (14.1 KB)

TestInput_PagesCount2.docx (14.2 KB)

TestInput_PagesCount3.docx (14.2 KB)

@abilger

It seems you are experiencing an issue with the PageCount property returning 4 instead of the expected 5 pages for your DOCX documents when using Aspose.Words. This discrepancy can occur due to various factors, including differences in how Aspose.Words interprets the document layout compared to Microsoft Word.

Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Document Structure: Ensure that the document does not contain any hidden content or sections that might affect the page count. Elements like page breaks or section breaks can lead to discrepancies.

  2. Update Aspose.Words: Verify that you are using the latest version of Aspose.Words. Updates may include bug fixes or improvements that could resolve this issue.

  3. Load Options: When loading the document, you can specify LoadOptions to control how the document is processed. For example:

    var loadOptions = new Aspose.Words.LoadOptions();
    var document = new Document("TestInput_PagesCount.docx", loadOptions);
    
  4. Re-save the Document: Open the document in Microsoft Word and re-save it. This can sometimes correct underlying issues that affect how Aspose.Words reads the document.

  5. Check for Compatibility: Ensure that the document is compatible with the version of Aspose.Words you are using. If the document was created with a newer version of Microsoft Word, there may be features that are not fully supported.

If the issue persists after trying these steps, consider reaching out to Aspose support with the specific documents you are testing, as they may provide more targeted assistance.

@abilger There is no bug here. Your document uses Aptos, which is cloud font, so it is not installed by default and is downloaded by MS Word on demand. Aspose.Words does not download cloud fonts.
The fonts are required to build document layout. If Aspose.Words cannot find the font used in the document, the font is substituted . This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/

You can download Aptos fonts from here.

PS: After providing Aptos the returned page count is correct.

1 Like

Hi,

I checked all of those and the issue is still occuring.
Also, I am indeed using latest 25.9 version released last week.

Regarding point 5, what is the max compatibility version for Word with Aspose.Words 25.9 ?

Regards

Thanks for this information, Alexey.

Would you advise any not online font to run tests with ?

@abilger To get accurate document layout it is required to have the fonts used in your documents. Any font substitution might lead to document layout differences as described above.

1 Like