We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove Blank Pages from Word document

I generate a word document using aspose word.
But my word doc has blank pages.

How can I remove all blank pages from my generated document!!

I have attached the test docx I generated.

Thank you!

Hi Nitin,

We are working on this query and will get back to you soon.

Best Regards,

Hi Nitin,

MS Word document is flow document and does not contain any information about its layout into lines and pages. Therefore, technically there is no “Page” concept in Word document however you can use the following code as a workaround to remove empty pages.

Document doc = new Document("Sample.docx");

foreach (Section section in doc.Sections)

{

if (section.ToString(SaveFormat.Text).Trim() == String.Empty)

section.Remove();

}

String PageText = "";

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

LayoutCollector lc = new LayoutCollector(doc);

int pages = lc.GetEndPageIndex(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("Out.docx");

----------------------------------------------------------------------------------------------------------------------

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))

{

if (lc.GetStartPageIndex(para) == page)

nodes.Add(para);

}

return nodes;

}

Please note that there should be at least one empty paragraph node on blank pages for this code to work as you can see in the attached document.

Best Regards,