Last active
June 2, 2023 04:25
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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