Re: How to remove blank pages in word files before appending them?

Your output is quite different from mine, I probably should have specified that this are the cleanup options used for mailmerge:

private static void merge(Document document, InputStream xmlStream, boolean withRegions) throws Exception {
	com.aspose.words.MailMerge mm = document.getMailMerge();
	mm.setFieldMergingCallback(new ImageMerge());
	mm.setTrimWhitespaces(true);

	com.aspose.words.net.System.Data.DataSet dataSet = new com.aspose.words.net.System.Data.DataSet();
	dataSet.readXml(xmlStream);

	if (withRegions) {
		mm.setMergeDuplicateRegions(true);

		mm.setCleanupOptions(
			MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
			| MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
			| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
		);

		// eseguo merge
		mm.executeWithRegions(dataSet);
	}

	mm.setCleanupOptions(
		MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
		| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
		| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
	);

	// eseguo merge
	mm.execute(dataSet.getTables().get(0));

	// pulizia campi
	FieldsHelper.convertFieldsToStaticText(document, FieldType.FIELD_IF);
}

@mtassinari,
Please accept my apologies for your inconvenience. The code snippet shared in my previous post needs a little change. Please check LastParagraph().remove() in following modified code snippet.

doc.getRange().getBookmarks().get("_GoBack").remove();
while (doc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
{
    int counter = 0;
    NodeCollection<Node> childNodes = doc.getLastSection().getBody().getLastParagraph().getChildNodes();

    for (Node child : childNodes) {
        if (child.getNodeType() != NodeType.RUN) {
            ++counter;
        }
    }
    if (counter > 0) {
        break;
    }

    if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
            (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH))
        break;

    doc.getLastSection().getBody().getLastParagraph().remove();

    // If the current section becomes empty, we should remove it.
    if (!doc.getLastSection().getBody().hasChildNodes())
        doc.getLastSection().remove();

    // We should exit the loop if the document becomes empty.
    if (!doc.hasChildNodes())
        break;
}

Hi again,
I am still having issue with this, please see the attached example: another_trim_error.zip (220.9 KB)
As you see, the merged file has a blank page at the end, which I do not want.
Please also notice that I need to implement a generic solution, and not something that works for this specific template only.
Can you help me? The functionality I’d need is to “trim” all empty paragraphs and page breaks from the end of the document.

@mtassinari,
Thanks for your inquiry. Please use the following generic solution to remove empty paragraphs from the end of document. Hope this helps you.

if(doc.getRange().getBookmarks().get("_GoBack") !=  null)
    doc.getRange().getBookmarks().get("_GoBack").remove();

while (doc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
{
    if(doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0
            || doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.GROUP_SHAPE, true).getCount() > 0
            || doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FORM_FIELD, true).getCount() > 0
            || doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.FOOTNOTE, true).getCount() > 0
            || doc.getLastSection().getBody().getLastParagraph().getChildNodes(NodeType.COMMENT, true).getCount() > 0
            )
        break;

    //Check if last paragraph contains the page break
    if(doc.getLastSection().getBody().getLastParagraph().isEndOfDocument())
    {
	doc.getLastSection().getBody().getLastParagraph().getRange().replace(ControlChar.PAGE_BREAK, "", new FindReplaceOptions());
    }

    if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
            (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH))
        break;

    doc.getLastSection().getBody().getLastParagraph().remove();

    // If the current section becomes empty, we should remove it.
    if (!doc.getLastSection().getBody().hasChildNodes())
        doc.getLastSection().remove();

    // We should exit the loop if the document becomes empty.
    if (!doc.hasChildNodes())
        break;
}

A post was merged into an existing topic: Remove blank empty pages from Word document