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

Free Support Forum - aspose.com

Remove blank lines at end of word document after main content

Dear aspose team using aspose words 14.10.0 in java im trying to delete extra lines entered by users only after main content (end of document) without affecting formatting. I have not been able to find required solution in already posted forums.

I am attaching input document and code what i have done so far. Below code removes empty lines from complete document. Desired functionality is to remove only spaces at end of document

private static void removeEmptySpaces(Document doc) throws Exception {
Node[] paras = doc.getChildNodes(NodeType.PARAGRAPH, true).toArray();
System.out.println(“total paras”+paras.length);
// for (int i = 0; i < (paras.length)-1; i++) {
for (int i = 0; i < (paras.length); i++) {
try {
Paragraph p = (Paragraph) paras[i];
if (p.getChildNodes().getCount() == 0 || p.toString(SaveFormat.TEXT).trim().equals("")) {
System.out.println(“para clause” + i);
System.out.println(“No spaces found in para”+i);
} catch (Exception ex) {
Logger.getLogger(EmptySpaceRemoval.class.getName()).log(Level.SEVERE, null, ex);

EmptySpaceRemoval.zip (29.7 KB)


Thanks for your inquiry. Please use the following code example to remove the empty lines (paragraphs) from the end of document. Hope this helps you.

Document document = new Document(MyDir + "input.docm");

while (document.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().equals(""))
    if (document.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
            document.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH)

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

    // We should exit the loop if the document becomes empty.
    if (!document.hasChildNodes())

document.save(MyDir + "18.12.docm");

Dear Tahir this is exactly what i required. Thank you, however i need your further assistance as i have encountered a ripple effect after addition of this method in existing application.

Normally i protect content before saving so user can only add further content in the same document only after previous content which should be un editable.

Now after adding contents i remove spaces, then protect document and save it using fol:

document.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS, “password”);
document.save(dataDirectory + “output.docm”)

but complete document becomes unprotected and any change can be made in previous content
do i have to change my protection methodology as i let open last section so content before becomes protected or im doing anything wrong?


Thanks for your inquiry. Could you please ZIP and attach your input, output and expected output Word documents here for our reference? We will then provide you more information about your query along with code example.

Dear Tahir i am attaching two files which are as fol
1st File
DocumentProtection.zip (current approach)
A file is loaded in memory in which user has written text. that file is protected so next time when user opens user wont be able to edit previous content.

some times user adds extra lines/ spaces after writing content un intentionally which also gets locked and results in unnecessary spaces between first content and new content

2nd file
Remove Space and Protect Document.zip

issue faced above has been resolved by your assistance but now when user opens document for writing second time. 1st content is no more locked and user will edit it.

Expected output word is outputprotect.docm but after removing extraspaces after content in any.

Remove Space And Protect Document.zip (46.4 KB)
DocumentProtection.zip (51.5 KB)

input and output files of both have been attached. Waiting for a favorable solution, thank you.


Thanks for sharing the detail. The EditableRange class represents a single editable range. EditableRange is a “facade” object that encapsulates two nodes EditableRangeStart and EditableRangeEnd in a document tree and allows to work with an editable range as a single object.

You need to add EditableRangeStart and EditableRangeEnd nodes into document that you want to edit and protect the document using Document.Protect method. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "inputprotect.docm");
DocumentBuilder builder = new DocumentBuilder(doc);

// Start an editable range
EditableRangeStart edRange1Start = builder.startEditableRange();
EditableRangeEnd edRange1End = builder.endEditableRange();

doc.save(MyDir + "18.12.docm");