Hi ALL:
I have a doucment with Empty Pages .
I would like to remove those empty pages ,and return a no empty pages document .
Please let me know ,which is the optimal solution for this.
Thanks,
Jaswanth
Input : Document with empty pages .
Expected Output : Document Without Empty Pages .
Hi Jaswanth,
for your inquiry. Please use the following code example to achieve your
requirements. I suggest you please read about LayoutCollector and LayoutEnumerator classes from here:
Document doc = new Document(MyDir + "in.docx");
foreach (Section section in doc.Sections)
{
if (section.ToString(SaveFormat.Text).Trim() == String.Empty)
section.Remove();
}
String PageText = “”;
LayoutCollector lc = new LayoutCollector(doc);
int pages = lc.GetStartPageIndex(doc.LastSection.Body.LastParagraph);
for (int i = 1; i <= pages; i++)
{
ArrayList nodes = GetNodesByPage(i, doc);
foreach (Paragraph para in nodes)
{
PageText += para.ToString(SaveFormat.Text).Trim();
}
//Empty Page
if (PageText == "")
{
foreach (Node node in nodes)
{
node.Remove();
}
}
nodes.Clear();
PageText = "";
}
doc.Save(MyDir + "Out.docx");
//Get Paragraph nodes by page number
private ArrayList GetNodesByPage(int page, Document document)
{
ArrayList nodes = new ArrayList();
LayoutCollector lc = new LayoutCollector(document);
foreach (Paragraph para in document.GetChildNodes(NodeType.Paragraph, true))
{
if (lc.GetStartPageIndex(para) == page || para.IsEndOfSection)
nodes.Add(para);
}
return nodes;
<span style=“font-size:9.5pt;line-height:200%;font-family:Consolas;mso-fareast-font-family:
“Times New Roman”;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA”> }
Does this code maintain the page breaks ???of the pages which we have content .or will it remove all the page breaks ???
Hi Jaswanth,
for your inquiry. I have modified the code according to your requirements. Hope this helps you. If you still face problem, please share your input document here for our reference. We will then provide you more information about your query along with code.
Document doc = new Document(MyDir + "in.docx");
doc.UpdatePageLayout();
foreach (Section section in doc.Sections)
{
if (section.ToString(SaveFormat.Text).Trim() == String.Empty)
section.Remove();
}
String PageText = "";
LayoutCollector lc = new LayoutCollector(doc);
int pages = lc.GetStartPageIndex(doc.LastSection.Body.LastParagraph);
for (int i = 1; i <= pages; i++)
{
ArrayList nodes = GetNodesByPage(i, doc);
foreach (Paragraph para in nodes)
{
if(para.GetText().Contains(ControlChar.PageBreak))
{
PageText = "Page Break";
break;
}
PageText += para.ToString(SaveFormat.Text).Trim();
}
//Empty Page
if (PageText == "")
{
foreach (Node node in nodes)
{
node.Remove();
}
}
nodes.Clear();
PageText = "";
}
doc.Save(MyDir + "Out.docx");
private 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;
}