Free Support Forum - aspose.com

Paging is not getting updated after deleting a page from word doc

I am using below code to delete page. when this code deletes fist page, then paging starts from 2 instead of 1.

                          if (pageNumber == 1)
            {
                Aspose.Words.Document finalDoc = (Aspose.Words.Document)doc.Clone(false);
                finalDoc.RemoveAllChildren();

                DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

                for (int page = 1; page <= doc.PageCount; page++)
                {
                    Aspose.Words.Document pageDoc = splitter.GetDocumentOfPage(page);
                    if (page != pageNumber)
                        finalDoc.AppendDocument(pageDoc, ImportFormatMode.KeepSourceFormatting);
                }

                finalDoc.Save(FileName);
            }

@sohilchawla,

Please call Aspose.Words.Document.UpdateFields() method before saving the final document. Following will be the line of code in your case:

finalDoc.UpdateFields();

Furthermore it is recommended to read following documentation link for your kind reference. Please let us know if you have any more queries.

Working with Fields

Its not working. Still the page number for the word doc are starting from 2.

@sohilchawla,

Thanks for your inquiry. Please set the value of PageSetup.RestartPageNumbering property as false to get the desired output. Please check the following code example.

Document doc = new Document(MyDir + @"in.docx");

int pagecount = doc.PageCount;

for (int pageNumber = 1; pageNumber <= pagecount; pageNumber++)
{
    if (pageNumber == 1)
    {
        Aspose.Words.Document finalDoc = (Aspose.Words.Document)doc.Clone(false);
        finalDoc.RemoveAllChildren();

        DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

        for (int page = 1; page <= doc.PageCount; page++)
        {
            Aspose.Words.Document pageDoc = splitter.GetDocumentOfPage(page);
            pageDoc.FirstSection.PageSetup.RestartPageNumbering = false;
            if (page != pageNumber)
                finalDoc.AppendDocument(pageDoc, ImportFormatMode.KeepSourceFormatting);
        }

        finalDoc.Save(MyDir + "18.6.docx");
    }
}

Moreover, Document.UpdateFields method does not update fields that are related to the page layout algorithms (e.g. PAGE, PAGES, PAGEREF). The page layout-related fields are updated when you render a document or call UpdatePageLayout.

Thanks Tahir, This solution worked!
I have one more query. I am further merging that word document with other word document and then converting that merged document into pdf using below code:
if(File.Exists(imputFileName))
{
Aspose.Words.Document doc = new Aspose.Words.Document(imputFileName);
Aspose.Words.Saving.PdfSaveOptions pdfSaveoption = new Aspose.Words.Saving.PdfSaveOptions
{
Compliance = PdfCompliance.Pdf15
};
doc.Save(outputFileName, pdfSaveoption);
Log.Info(“File Converted to PDF”);
}
Now problem is that the page numbers are not visible on first 3 pages and they are starting from 4th page with number 4 (which is fine because that is 4th page only). But for first 3 pages page numbers are not visible.
Is there any way to remove all page numbers from the pdf generated from above code and then providing page numbers from first page starting from 1? or to make page numbers visible on first three pages as well?

Thanks

@sohilchawla,

Thanks for your inquiry. Please call Document.UpdatePageLayout method before saving document to PDF. If you still face problem, please share following detail for testing.

  • Your input Word documents.
  • Please attach the output PDF file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.