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

Free Support Forum - aspose.com

Delete a last page of document

Hi,

I have a word document where I am inserting a new page at last of the document using below given syntax:

document.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

Now I want to remove/delete this last page from document. Please suggest. How to delete this particular/last page from document.

Thanks in advance

Preety Chauhan

Hi Preety,


Thanks for your inquiry. The shared line of code do not inserted the section break. Please use the DocumentBuilder.InsertBreak method to insert a break of the specified type into the document.

Please try using the following code snippet to remove last empty page.

//get last section

Section lastSect = doc.LastSection;<o:p></o:p>

// Remove page break from the last pagagraph of the document<o:p></o:p>

foreach (Run run in lastSect.Body.LastParagraph.Runs)<o:p></o:p>

run.Text = run.Text.Replace(ControlChar.PageBreak, " ");<o:p></o:p>

//Remove empty paragraphs from the end of the document<o:p></o:p>

while (string.IsNullOrEmpty(lastSect.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))<o:p></o:p>

{<o:p></o:p>

lastSect.Body.LastParagraph.Remove();<o:p></o:p>

} <o:p></o:p>


Hope this helps you. Please let us know if you have any more queries.

Thanks for your reply. Now I am generating my document by inserting pagebreak using below given syntax:

documentBuilder.InsertBreak(BreakType.PageBreak);

after inserting this page break, I am inserting new page in the same document at last in first attempt. My requirement is to delete this last page in next attempt and insert different page in this document as last page.But still I am not able to remove last page with the code given by you as,

lastSect.Body.LastParagraph is null

So I am not able to delete last page.

Please suggest something so that I can delete my last page which I have inserted after pagebreak in first attempt.

Thanks in advance,

Preety

Hi Preety,


Thanks for your inquiry. Could you please attach your Word document along with code here for testing? I will investigate the issue on my side and provide you more information.