If a Word document has 300 pages with hundreds of empty paragraphs, it is time-consuming to manually remove all empty paragraphs one by one. This article will show you how to programmatically remove empty paragraphs in a Word document using Java codes.
DEPENDENCY
First of all, you need to download the package of a free third-party library called Free Spire.Doc for Java from this link, find Spire.Doc.jar in the lib folder, and then add it to your Java program. If you use Maven, just type the following codes in the pom.xml file and click the button “Import Changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
USING THE CODE
Free Spire.Doc for Java supports removing the empty paragraphs in a Word document using ParagraphCollection.remove() method. The following are detailed steps.
l Create a Document instance.
l Load a Word sample document using Document.loadFromFile() method.
l Traverse every section in the document and
get sections using Document.getSections().get()
method.
l Traverse every object in the sections and
get the types of objects using DocumentObject.getDocumentObjectType()
method.
l Traverse every paragraph and get paragraphs
using Section.getParagraphs().get() method.
l Determine whether the object types are
paragraph and the content of paragraphs are blank or not, if the content is empty,
remove it using ParagraphCollection.remove()
method.
l Save the output document to another file
using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class removeBlankLines {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word sample document
doc.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.docx");
//Traverse every section in the document
for(int i = 0; i < doc.getSections().getCount();i++)
{
//Get sections
Section section = doc.getSections().get(i);
//Traverse objects in the sections
for (int j = 0;j < section.getBody().getChildObjects().getCount();j++)
{
//Get the types of objects
Object object = section.getBody().getChildObjects().get(j).getDocumentObjectType();
//Traverse evert paragraph
for(int z = 0 ; z < section.getParagraphs().getCount();z++)
{
//Get paragraphs
Paragraph paragraph = section.getParagraphs().get(z);
//Decide whether the object types are paragraphs or not
if(object.equals(DocumentObjectType.Paragraph))
{
//Determine whether the content of paragraphs are blank
if(paragraph.getChildObjects().getLastItem() == null)
{
//Delete blank paragraphs
section.getBody().getParagraphs().remove(paragraph);
z--;
}
}
}
}
}
//Save the output document
doc.saveToFile("output/DeleteBlankParas.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
No comments:
Post a Comment