Delete the last Page Break

There is a requirement for me to delete the last page break that is inserted in the document section. How do I do that?

Hello

Thanks for your inquiry. Could you please attach your input document here for testing? We will check it and provide you more information.
Best regards,

Hi,
Please use the code below, it should remove the last page break in a document.

NodeCollection runs = doc.LastSection.GetChildNodes(NodeType.Run, true);
// Find the runs that contain the last page break characters in this section and remove the character.
for (int i = runs.Count - 1; i>= 0; i--)
{
    Run run = (Run) runs[i];
    if (run.Text.IndexOf(ControlChar.PageBreakChar)>= 0)
    {
        run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), 1);
        break;
    }
}

If you need any futher help then please feel free to ask.
Thanks,

Great!!! Thanks for your inputs. It works like charm.

Hello,
We use Aspose.Words 9.4
The mentioned code doesn’t work.
Do you have an advice?
Friendly Greetings,
Pieter
(The Netherlands)

Hi Pieter,
Thanks for your inquiry.
Can you attach your documents here for testing? Most likely the break in your document is actually a section break and not a page break.
Thanks,

Hello,
Hereby the document before mailmerge and the document after mailmerge.
With friendly greetings,
Pieter

Hi

Thanks for your inquiry. If it is acceptable for you , you can resolve the problem by small modification of your template (just disable “Page Break Before” option of the penultimate paragraph). Please try using the attached template.
Hope this helps.
Best regards,