Skip to content

Instantly share code, notes, and snippets.

How to Remove All Page Breaks in Word using C#. For more details: https://kb.aspose.com/words/net/how-to-remove-all-page-breaks-in-word-using-csharp/
using Aspose.Words;
class Program{
static void Main(string[] args) // Remove page breaks in a Word file using C#
{
// Set PDF license
new License().SetLicense("Aspose.Total.lic");
// Load the sample Word file having page breaks in it
Document doc = new Document("DocWithPageBreaks.docx");
// Get access to all the paragraphs
NodeCollection docParagraphs = doc
.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph currentPara in docParagraphs)
{
// Check if the page break is there before
// the paragraph
if (currentPara.ParagraphFormat.PageBreakBefore)
{
// Remove the page break from the start
currentPara.ParagraphFormat
.PageBreakBefore = false;
}
// Parse through all the runs in the paragraph
foreach (Run currentRun in currentPara.Runs)
{
// Check page break
if (currentRun.Text.Contains(ControlChar.PageBreak))
{
// Replace the page break with an empty string
currentRun.Text = currentRun.Text
.Replace(ControlChar.PageBreak, string.Empty);
}
}
}
// Save the resultant DOCX without any page break in it
doc.Save("DocxWithoutPageBreaks.docx");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment