How to remove the last page from word document using Aspose.Word

Hi Team,

Let us think of a scenario where I am accessing a word document using Aspose.Word, which is having 5 or 6 pages( let us assume that the total page count is not known to me).

I want to delete the last page of that word document and save again.

Could you please let me know how to delete the last page using Aspose.Word?

Please revert in case of any issue.
Thanks,
Rajesh

Hi Rajesh,


Thanks for your inquiry. I suggest you please use PageSplitter code to achieve your requirements. Please check “PageSplitter” example project in Aspose.Words for .NET examples repository at GitHub.

Following code example shows how to remove last page of document. Hope this helps you.

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

LayoutCollector layoutCollector = new LayoutCollector(doc);

DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);

Document pageDoc = splitter.GetDocumentOfPageRange(1, doc.PageCount - 1);

pageDoc.Save(MyDir + "Out.docx");

Hi Tahir,

I have same issue, need to delete last page. I copied PageSplitter class to my project and used above code for deletion, but I received object reference not set error on

at SectionSplitter.VisitParagraphStart(Paragraph paragraph) in PageSplitter.cs:line 353
at Aspose.Words.Paragraph.r3ka8zscesabkstb24kjjv9sml9wq7pw (DocumentVisitor )
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Section.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptChildren(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.AcceptCore(DocumentVisitor visitor)
at Aspose.Words.Document.Accept(DocumentVisitor visitor)

Thanks,
Kris

@kpendoti,

Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.