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

Free Support Forum - aspose.com

How to remove blank lines from word document

Hi,

Can you please suggest me how to remove blank lines from the word doc.

Currently I am replacing the unwanted lines with "" (empty string) but I don't want these lines at all.

Thanks,

Bhoomica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Here is code that removes empty paragraphs from the end of the document:

//Open document

Document doc = new Document(@"Test111\in.doc");

//get last section

Section lastSect = doc.LastSection;

//Remove empty paragraphs from the end of the document

while (string.IsNullOrEmpty(lastSect.Body.LastParagraph.ToTxt().Trim()))

{

lastSect.Body.LastParagraph.Remove();

}

//Save output document

doc.Save(@"Test111\out.doc");

Hope this helps.

Best regards.

Hi,

Thanks for the reply.

But I need to replace the blank lines from the complete document.

Its not only the last paragraph or last section.

Also can you please help in terms of JAVA code.

Thanks,

Bhoomica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Hi,

Thanks for the help but it could not help much.

I am attaching the template & output files.

You can see 2-3 blank lines in between where I have replaced the unwanted line of template with ""

The Paragraph thing is not working for this.

I want to keep 1 line difference as in template but I want to remove those extra blank lines which get inserted when i do replacement.

Please help me out on this.

Thanks,

Bhoomica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I see few line break characters (ā€˜\vā€™) in your document. You can try just removing these characters. Please try using the following visitor.

import com.aspose.words.*;

public class EmptyParagraphRemover extends DocumentVisitor {

///

/// Called when a Run node is encountered in the document.

///

public int visitRun(Run run)

{

if(run.getText().contains(ControlChar.LINE_BREAK))

{

run.setText(run.getText().replace(ControlChar.LINE_BREAK_CHAR, ' '));

}

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

///

/// 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.

Thanks a lot.

Its really helpful.

Hi,

I tried to replace the blank lines in my resultant doc but no condition is getting got satisfied.

I am attaching the zip file which has the test code to check which character is there in doc that matches the constants in ControlChar class.

Also I have added the template & the output file which I am getting.

Can you please help?

Thanks & Regards

Bhoomica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following visitor.

public class EmptyParagraphRemover extends DocumentVisitor

{

///

/// Called when a Run node is encountered in the document.

///

public int visitRun(Run run)

{

if (run.getText().contains(ControlChar.LINE_BREAK))

{

run.setText(run.getText().replace(ControlChar.LINE_BREAK_CHAR, ' '));

}

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

///

/// 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.

Its not helpful.

If you look into the zip file, you will find that it is not removing the blank lines where condition in template is replaced with ""

I tried checking using different char in ControlChar class but still of no use as no condition gets satisfied.

Can you help me find out what actually the character is when I replace the with "".

Thanks

Bhoomica

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This occurs because empty paragraphs can contain bookmarks. You can try using the following code:

///

/// Called when visiting of a Paragraph node is Started in the document.

///

public int visitParagraphStart(Paragraph paragraph)

{

try

{

if(paragraph.toTxt().trim().equals("") || !paragraph.hasChildNodes())

paragraph.remove();

}

catch(Exception ex) {System.out.println(ex);}

return VisitorAction.CONTINUE;

}

Best regards.

Hi,

If I use the help code provided above, then the blank lines go but the header also doesn't get displayed.

If I don't use this code

________________________________________________________________________

if(paragraph.toTxt().trim().equals(""))<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

paragraph.remove();

}

_________________________________________________________________________

then the empty lines remain in the document which is not required.

Please help me out as I am totally stuck in this.

I want the header to get displayed & blank lines to get removed. (These blank lines have bookmarks as suggested above in your reply so the code where BREAK_LINE etc. is checked doesn't work)

I am attaching the code.

Looking forward for your help !

Thanks

Bhoomica

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Maybe you should just remove bookmarks from the document before removing empty paragraphs. Please see the following code:

//Code from DocFulfillment.java

if (document.getRange().getBookmarks().getCount() > 0)

{

//Remove bookamrks from the docuemnt

document.getRange().getBookmarks().clear();

EmptyParagraphRemover remover = new EmptyParagraphRemover();

document.accept(remover);

}

=============================================================

// Called when visiting of a Paragraph node is Started in the document.

public int visitParagraphStart(Paragraph paragraph)

{

try

{

if (!paragraph.hasChildNodes())

{

paragraph.remove();

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

return VisitorAction.CONTINUE;

}

Hope this helps.

Best regards.

Hello,


How do I replace multiple blank lines with a single blank line ?

Thanks

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, you can just check whether the next node after the current paragraph is also empty paragraph and only in this case remove empty paragraph. Here is modified code:

private static class EmptyParagraphRemover extends DocumentVisitor

{

///

/// Called when a Run node is encountered in the document.

///

public int visitRun(Run run)

{

if (run.getText().contains(ControlChar.LINE_BREAK))

{

run.setText(run.getText().replace(ControlChar.LINE_BREAK_CHAR, ' '));

}

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a Paragraph node is Started in the document.

///

public int visitParagraphStart(Paragraph paragraph)

{

if (!paragraph.hasChildNodes())

{

try

{

CompositeNode nextNode = (CompositeNode)paragraph.getNextSibling();

if (!paragraph.hasChildNodes() && nextNode.getNodeType()==NodeType.PARAGRAPH && !nextNode.hasChildNodes())

paragraph.remove();

}

catch (Exception ex) { System.out.println(ex); }

}

return VisitorAction.CONTINUE;

}

}

Hope this helps.

Best regards,