Empty page not getting removed

Hi Team,

I have the following code that removes empty pages in document. But in one particular document , empty page (page number 3 ) is not getting removed. Providing the code for reference and attaching the documents. Please help

public static void main(String... args) throws Exception {
        com.aspose.words.License license = new com.aspose.words.License();
        license.setLicense("/home/saurabharora/aspose-licence");
        Document document = new Document("/home/saurabharora/Downloads/Application Form_5-0000049215_2 (2)_yatin.docx");

        removeEmptySectionAndPages(document);
        document.updateFields();
        document.updatePageLayout();

        document.save("/home/saurabharora/Downloads/Application Form_5-0000049215_2_output.docx");
    }

private static boolean isSectionEmpty(NodeCollection childNodes) throws Exception {
        Iterator iterator = childNodes.iterator();
        while (iterator.hasNext()) {
            Body bodyNode = (Body) iterator.next();
            if (!StringUtils.isEmpty(bodyNode.toString(SaveFormat.TEXT).trim())) {
                return false;
            }
        }

        return true;
    }

    private static Map<Integer, List<Node>> getPageIndexToNodeMap(Document document, LayoutCollector lc) throws Exception {
        Map<Integer, List<Node>> pageToNodes = new HashMap();

        for (Object node : document.getChildNodes(NodeType.ANY, true)) {
            int startPageIndex = lc.getStartPageIndex((Node) node);
            if (pageToNodes.get(startPageIndex) == null) {
                pageToNodes.put(startPageIndex, new ArrayList());
            }
            pageToNodes.get(startPageIndex).add((Node) node);
        }
        return pageToNodes;
    }

    private static void removeLastPagePageBreak(Document document) throws Exception {
        NodeCollection nodeCollection = document.getChildNodes(NodeType.ANY, true);
        Integer nodeCount = nodeCollection.getCount();
        for (int index = nodeCount - 1; index >= 0; index--) {
            Node node = nodeCollection.get(index);
            if (node.getNodeType() == NodeType.PARAGRAPH) {
                Paragraph para = (Paragraph) node;
                if (!StringUtils.isEmpty(para.toString(SaveFormat.TEXT).trim())) {
                    break;
                }

            } else if (node.getNodeType() == NodeType.SHAPE) {
                break;
            } else if (node.getNodeType() == NodeType.RUN) {
                Run para = (Run) node;
                if (!StringUtils.isEmpty(para.toString(SaveFormat.TEXT).trim())) {
                    break;
                }
            }

            if ((node.getNodeType() != NodeType.RUN)) {
                continue;
            }

            Run run = (Run) node;
            if (run.getText().contains(ControlChar.PAGE_BREAK)) {
                run.setText(run.getText().replace(ControlChar.PAGE_BREAK, ""));
                break;
            }
        }
    }

    public static void removeEmptySectionAndPages(Document document) throws Exception {
        document.updatePageLayout();
        for (Section section : document.getSections()) {
            NodeCollection childNodes = section.getChildNodes(NodeType.BODY, true);
            if (childNodes.getCount() == 0 || isSectionEmpty(childNodes)) {
                section.remove();
            }
        }
        LayoutCollector lc = new LayoutCollector(document);
        boolean lastPageEmpty = false;
        int pages = lc.getStartPageIndex(document.getLastSection().getBody().getLastParagraph());
        Map<Integer, List<Node>> pageIndexToNodeMap = getPageIndexToNodeMap(document, lc);
        for (int i = 0; i <= pages; i++) {
            List<Node> nodes = pageIndexToNodeMap.get(i);
            if (nodes == null) {
                break;
            }
            boolean empty = true;

            for (Node node : nodes) {
                if (node.getNodeType() == NodeType.PARAGRAPH) {
                    Paragraph para = (Paragraph) node;
                    if (!StringUtils.isEmpty(para.toString(SaveFormat.TEXT).trim())) {
                        if (para.toString(SaveFormat.TEXT).trim().equalsIgnoreCase("\\")) {
                            System.out.println("heRE");
                            continue;
                        }
                        empty = false;
                        break;
                    }
                } else if (node.getNodeType() == NodeType.SHAPE) {
                    empty = false;
                    break;
                } else if (node.getNodeType() == NodeType.RUN) {
                    Run para = (Run) node;
                    if (!StringUtils.isEmpty(para.toString(SaveFormat.TEXT).trim())) {
                        if (para.toString(SaveFormat.TEXT).trim().equalsIgnoreCase("\\")) {
                            System.out.println("heRE");
                            continue;
                        }
                        empty = false;
                        break;
                    }
                }
            }

            if (empty) {
                for (Node node : nodes) {
                    if ((node.getNodeType() != NodeType.SECTION && node.getNodeType() != NodeType.BODY) || ((node.getNodeType() == NodeType.SECTION || node.getNodeType() == NodeType.BODY) && StringUtils.isEmpty(node.toString(SaveFormat.TEXT).trim())) || "\\".equals(node.toString(SaveFormat.TEXT).trim())) {
                        node.remove();

                        if (i == pages) {
                            lastPageEmpty = true;
                        }
                    }else{
                        System.out.println("Not removed");
                    }
                }
            }
        }

        if (lastPageEmpty) {
            removeLastPagePageBreak(document);
        }
    }

page_removal_issue.zip (184.9 KB)

@saurabh.arora Please try using a built-in Document.removeBlankPages method:

Document doc = new Document("C:\\Temp\\in.docx");
doc.removeBlankPages();
doc.save("C:\\Temp\\out.docx");

Here is the produced output:
out.docx (97.3 KB)

Hi Team ,

Thanks for the reply.

But the solution does not work. Page 3 which is blank is till there in the generated document. Please check and help.

@saurabh.arora There is no empty page between the second and the third pages in the output document produced on my side:

Document.removeBlankPages method returns number of pages that were removed:

Document doc = new Document("C:\\Temp\\in.docx");
ArrayList<Integer> removedPages = doc.removeBlankPages();
for (int i : removedPages)
    System.out.println("Page " + i + " has been removed.");
doc.save("C:\\Temp\\out.docx");

The code returns:

Page 3 has been removed.