Learn How to Remove Pages from Word Document in C#.
Last active
August 27, 2024 13:31
-
-
Save aspose-com-gists/2bbb2dc2efef8014eee2ef319a9e3ee9 to your computer and use it in GitHub Desktop.
How to Remove Pages from Word Document in C#
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
// The following code sample shows how to remove all the blank page from a Word document using C#. | |
// Load a document | |
Document doc = new Document("Document.docx"); | |
// Remove all the blank pages | |
doc.RemoveBlankPages(); | |
// Save the updated document | |
doc.Save("Document_out.docx"); |
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
// This code sample shows how to remove a page from a Word document containing specific text using C#. | |
// Load a document | |
Document doc = new Document("Document.docx"); | |
// Text to search | |
var PageText = "Page 2"; | |
var isTextFound = false; | |
for (int page = 0; page < doc.PageCount; page++) | |
{ | |
ArrayList nodes = GetNodesByPage(page, doc); | |
// Check if this page contains specific text | |
foreach (Node node in nodes) | |
{ | |
// Check if text found | |
if (PageText == node.GetText().Trim()) | |
{ | |
isTextFound = true; | |
} | |
} | |
if(isTextFound) | |
{ | |
foreach (Node node in nodes) | |
{ | |
node.Remove(); | |
} | |
isTextFound= false; | |
} | |
nodes.Clear(); | |
} | |
// Save the updated document | |
doc.Save("Document_out.docx"); |
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
// The following code sample shows how to remove a page by its index from a Word document in C#. | |
// Load a document | |
Document doc = new Document("Document.docx"); | |
// Initializa LayoutCollector | |
LayoutCollector layoutCollector = new LayoutCollector(doc); | |
ArrayList list = new ArrayList(); | |
// Get child nodes | |
foreach (Node node in doc.GetChildNodes(NodeType.Any, true)) | |
{ | |
if (layoutCollector.GetNumPagesSpanned(node) == 0) | |
{ | |
int pageIndex = layoutCollector.GetStartPageIndex(node); | |
// Remove Page 2 | |
if (pageIndex == 2) | |
{ | |
list.Add(node); | |
} | |
} | |
} | |
foreach (Node node in list) | |
node.Remove(); | |
// Save the document | |
doc.Save("Document_out.docx"); |
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
static ArrayList GetNodesByPage(int page, Document document) | |
{ | |
ArrayList nodes = new ArrayList(); | |
LayoutCollector lc = new LayoutCollector(document); | |
foreach (Paragraph para in document.GetChildNodes(NodeType.Paragraph, true)) | |
{ | |
Console.WriteLine(); | |
if (lc.GetStartPageIndex(para) == page) | |
nodes.Add(para); | |
} | |
return nodes; | |
} |
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
// The following code sample shows how to remove page breaks in a Word document using C#. | |
// Load the document | |
Document doc = new Document("Document.docx"); | |
// Get all Paragraph child nodes | |
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true); | |
foreach (Paragraph para in paragraphs) | |
{ | |
// If the paragraph has a page break before set, then clear it. | |
if (para.ParagraphFormat.PageBreakBefore) | |
para.ParagraphFormat.PageBreakBefore = false; | |
// Check all runs in the paragraph for page breaks and remove them. | |
foreach (Run run in para.Runs) | |
{ | |
if (run.Text.Contains(ControlChar.PageBreak)) | |
run.Text = run.Text.Replace(ControlChar.PageBreak, string.Empty); | |
} | |
} | |
// Save the document | |
doc.Save("Document_out.docx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment