The page numbers and formatting are incorrect after parsing the Word document using Aspose.Words

code is following.

int docPageCount = oriDoc.PageCount;
for (int index = 0; index < docPageCount; index++)
{
    Document page = oriDoc.ExtractPages(index, 1);
    PageInfo pageInfo = page.GetPageInfo(0);
    bool isLandscape = pageInfo.Landscape;
    if (!isLandscape)
    {
        foreach (Section section in page.Sections)
        {
            foreach (HeaderFooter headerFooter in section.HeadersFooters)
            {
                foreach (Node node in headerFooter.GetChildNodes(NodeType.Shape, true))
                {
                    Shape shape = (Shape)node;
                    if (shape.HasImage)
                    {
                        shape.Remove();
                    }
                }
            }
        }
    }

    string tempOutput = tempFolderName + Path.DirectorySeparatorChar + oriDoc.GetHashCode() + "_page" +
                        index +
                        ".docx";
    page.Save(tempOutput, SaveFormat.Docx);

The original file has a page count of 17, but after importing it as a Document object using Aspose, the PageCount property returns 23. Furthermore, the exported file also has a page count of 23, and the formatting of the header and tables is incorrect.

the input file:input (2).docx (89.6 KB)
the output file:24724999_withLogo.docx (71.8 KB)

@cokefree I cannot reproduce the problem on my side using the latest 23.5 version of Aspose.Words. As I can see both attached documents have 23 pages when I open them in MS Word 2019.
Also, please note, there is no page concept in MS Word documents, since they have flow nature. The consumer applications like MS Word or Open Office layout the document into pages on the fly. Document layout depends on the content, so after splitting document into pages, due to changes in document structure, “page” layout might be changed.

I sure there is 17 pages on my side.I use MS professional 2019.
image.png (58.9 KB)

@cokefree Page count returned by MS Word and by Aspose.Words depends on the language preferences. Ob my side default editing language in MS Word is English and in this case it returns 23 pages, but if change default editing language to Chinese, it returns 17 pages.
So if use the following code:

LoadOptions opt = new LoadOptions();
opt.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.ChinesePRC;
Document doc = new Document(@"C:\Temp\in.docx", opt);
Console.WriteLine(doc.PageCount);

Aspose.Words also returns 17 pages, like MS Word does when default editing language is set to Chinese.

Yes, it works.Thank you.

1 Like