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.