Re: How to remove blank lines from word document

Hi
Thanks for your request. I think you should use DocumentVisitor in this case. Please see the following code:

public static void main(String[] args) throws Exception
{ 
    License license = new License();
    license.setLicense("Aspose.Words.lic");
    //Open document
    Document doc = new Document("C:\\Temp\\in.doc");
    EmptyParagraphRemover remover = new EmptyParagraphRemover();
    doc.accept(remover);
    //Save output document
    doc.save("C:\\Temp\\out.doc");
}

//---------------------------------------------------------------------------------------

import com.aspose.words.DocumentVisitor;
import com.aspose.words.Paragraph;
import com.aspose.words.VisitorAction;
public class EmptyParagraphRemover extends DocumentVisitor
{
    /// 
    /// Called when visiting of a Paragraph node is Started in the document.
    /// 
    public int visitParagraphStart(Paragraph paragraph)
    {
        if (!paragraph.hasChildNodes())
        {
            try
            {
                paragraph.remove();
            }
            catch (Exception ex) { System.out.println(ex); }
        }
        return VisitorAction.CONTINUE;
    }
}

Hope this helps.
Best regards.*

I get null pointers if I attempt to do this while I am still building my document.

I need a way of saying, “If the last line of the document is a blank line, remove it.” At that point, I need to continue writing.

Hi Steven,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,