Remove Empty page with LINQ Engine

Hello,
I am using the linq reporting engine. I wish I could insert content to pages dynamically (conditional if), and remove blank pages. How can I do?
input.docx (18.5 KB)
output.docx (17.8 KB)

@Blegork

You can insert content to pages dynamically using LINQ Reporting but it does not not remove the empty pages from the generated document. However, you can remove empty pages from Word document using Aspose.Words after generating document from LINQ Reporting engine.

Could you please share some more detail about your requirement along with document that contains empty pages? We will then provide you code example accordingly.

@tahir.manzoor
I am attaching a sample file containing a blank page. I am using Java

Example.docx (11.8 KB)

@Blegork

Following code example shows how to remove the empty pages from the Word document. Hope this helps you.

// Load DOCX file you want to Remove Blank Pages from
Document doc = new Document(MyDir + "Example (4).docx");
 
// An Array List will hold Blank or Empty Page numbers
ArrayList empty_Page_Numbers = new ArrayList();
empty_Page_Numbers.add(-1);
 
// Extract each Page as a separate Word document
int total_Pages = doc.getPageCount();
for (int i = 0; i < total_Pages; i++)
{
    Document one_Page_Doc = doc.extractPages(i, 1);
 
    // Get text representation of this Page and total Count of Shapes
    int shape_Count = 0;
    String text_Of_Page = "";
    for (Section section : one_Page_Doc.getSections())
    {
        // Lets not consider the content of Headers and Footers
        text_Of_Page = text_Of_Page + section.getBody().toString(SaveFormat.TEXT);
        shape_Count += section.getBody().getChildNodes(NodeType.SHAPE, true).getCount();
    }
 
    // if text_of_Page is Empty and does not contain any Shape nodes then consider this Page is Blank or Empty
    if (isNullOrEmpty(text_Of_Page.trim()) && shape_Count == 0)
        empty_Page_Numbers.add(i);
}
empty_Page_Numbers.add(total_Pages);
 
// Concatenate small one-Page Word documents with Non-Empty Pages again
Document final_Document = (Document)doc.deepClone(false);
final_Document.removeAllChildren();
 
for (int i = 1; i < empty_Page_Numbers.size(); i++)
{
    int index = (int)empty_Page_Numbers.get(i - 1) + 1;
    int count = (int)empty_Page_Numbers.get(i) - index;
 
    if (count > 0)
        final_Document.appendDocument(doc.extractPages(index, count), ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
 
final_Document.save(MyDir + "21.10.java.docx");
public static boolean isNullOrEmpty(String s) {
    return s == null || s.length() == 0;
}

@tahir.manzoor

Thanks for the code. I have noticed that if I have a document with number of pages, when I delete the blank pages, the page number remains wrong. For example I have a document with 3 pages, I delete the 2nd page, the page number remains 1 and 3. How can I solve this problem? manzoor

@Blegork

You can use following code snippet before saving the document to get the desired output.

for(Section section : final_Document.getSections())
{
	section.getPageSetup().setRestartPageNumbering(false);
}

final_Document.save(MyDir + "21.10.java.docx");