We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document.PageCount returns Incorrect Value | Extract Pages from DOCX using .NET

i have a docx file (please see the attachment), i use the below code to get pagecount:

var doc = new Aspose.Words.Document(fileFullName);

int pageCount = doc.PageCount;

for (int page = 0; page < pageCount; page++)
{
    // Save each page as a separate document.
    var extractedPage = doc.ExtractPages(page, 1);
    var toFileName = Path.Combine(@"O:\", $"SplitDocument.PageByPage_{ page + 1}.docx");
    extractedPage.Save(toFileName);
}

the pagecount property returns 2 which is incorrect, and i separated the document to 2 docs file please also see the attments.

i dont know what’s wrong with my codes, can you please help me to find out why the pagecount property is not incorrect.

thank you very much.

SplitDocument.PageByPage_1.docx (16.9 KB)
SplitDocument.PageByPage_2.docx (16.4 KB)

common_FOOTER-Final.docx (22.9 KB)

@vs6060_qq_com

Please note that Aspose.Words requires TrueType fonts when rendering document to fixed-page formats (JPEG, PNG, PDF or XPS) and calling Document.PageCount or Document.ExtractPages method. You need to install fonts that are used in your document on the machine where you are extracting pages of document. Please refer to the following articles:

Using TrueType Fonts
Manipulating and Substitution TrueType Fonts

hi manzoor:
thank you for the reply, to follow your suggestion,i did the below steps
0) prepare my test document.common_FOOTER-Final.docx (20.3 KB)

  1. got the all font names provided by FontSettings.DefaultInstance by the below codes:

        var fontSettings = FontSettings.DefaultInstance;
         var defaultFontNames = new List<string>();
         fontSettings.GetFontsSources().ToList().ForEach(t =>
         {
             var thisFontNames = t.GetAvailableFonts().Select(t => t.FullFontName).ToList();
             defaultFontNames.AddRange(thisFontNames);
         });
    

    i got a name list ,please see the attachment(defaultnames.docx) (19.6 KB)

  2. got the document missing fonts:

         var fileFullName = @"common_FOOTER-Final.docx";
         var doc = new Document(fileFullName);
         var documentFontNames = doc.FontInfos.Select(t => new
         {
             Name = t.Name,
             AltName = t.AltName
         }).ToList();
    
         var fontSettings = FontSettings.DefaultInstance;
    
         var defaultFontNames = new List<string>();
         fontSettings.GetFontsSources().ToList().ForEach(t =>
         {
             var thisFontNames = t.GetAvailableFonts().Select(t => t.FullFontName).ToList();
             defaultFontNames.AddRange(thisFontNames);
         });
         var notFoundNames = documentFontNames.Where(t => !defaultFontNames.Contains(t.Name)).ToList();
         notFoundNames = notFoundNames.Where(t => !string.IsNullOrEmpty(t.AltName) && !defaultFontNames.Contains(t.AltName)).ToList();
    

the values of “notFoundNames” was:
Name = “等线”, AltName = "DengXian"
"隶书", AltName = "微软雅黑"

  1. i checked the default font name list , found the similar fonts,:

DengXian Regular
DengXian Bold
DengXian Light
Microsoft YaHei

  1. i though ,the next should be using " Substitute features", by the bloew

         var fileFullName = @"common_FOOTER-Final.docx";
         var fontSettings = FontSettings.DefaultInstance;
         fontSettings.SubstitutionSettings.FontConfigSubstitution.Enabled = true;
         var xml = @"
             <TableSubstitutionSettings xmlns=""Aspose.Words""> 
                 <SubstitutesTable> 
                     <Item OriginalFont=""等线"" SubstituteFonts=""DengXian Regular"" /> 
                     <Item OriginalFont=""隶书"" SubstituteFonts=""Microsoft YaHei"" /> 
                     <Item OriginalFont=""微软雅黑"" SubstituteFonts=""Microsoft YaHei"" /> 
                 </SubstitutesTable> 
             </TableSubstitutionSettings>";
         var bytes = Encoding.UTF8.GetBytes(xml);
         using var stream = new MemoryStream(bytes);
         fontSettings.SubstitutionSettings.TableSubstitution.Load(stream);
         var loadOptions = new LoadOptions();
         loadOptions.FontSettings = fontSettings;
         var doc = new Aspose.Words.Document(fileFullName, loadOptions);
         doc.UpdatePageLayout();
         doc.UpdateTableLayout();
         int pageCount = doc.PageCount;
         for (int page = 0; page < pageCount; page++)
         {
             // Save each page as a separate document.
             var extractedPage = doc.ExtractPages(page, 1);
             var toFileName = Path.Combine(@"O:\", $"SplitDocument.PageByPage_{ page + 1}.docx");
             extractedPage.Save(toFileName);
         }
    

but i still got 2 pages.

could you please help me point out where the prombles are.

thank you very much.

@vs6060_qq_com

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-24858. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

thank you ,manzoor, i am looking forward this issue get resolved.

hi ,

please any update of this issue?

@vs6060_qq_com

Your issue has been resolved and its fix will be available in the next version of Aspose.Words i.e. 21.8. Hopefully, this release will be available at the start of August 2021.

okay,sounds good, please let me know when the new version 21.8 is released.