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
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;
// Remove page break from the last pagagraph of the document
foreach(Run run in lastSect.Body.LastParagraph.Runs)
run.Text = run.Text.Replace(ControlChar.PageBreak, " ");
// Remove empty paragraphs from the end of the document
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:
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,
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.