Java: Count the Number of Words, Characters, Paragraphs, Pages and Lines in Word Documents

The word count feature in Microsoft Word is a very helpful tool that provides users with the ability to easily and accurately determine the number of words, characters, paragraphs, pages, and lines in their documents. By using this feature, authors can ensure that their writings meet specific length requirements, while readers can evaluate the length of the documents and make informed decisions about how to allocate their reading time. In this article, I will explain how to programmatically count the number of words, characters, paragraphs, pages, and lines in a Word document in Java.

Add Dependencies

In order to count the number of words, characters, paragraphs, pages, and lines in Word documents, this article uses Spire.Doc for Java, which is a professional Word library specifically designed for creating, reading, writing, converting, and printing Word documents in Java applications

Before coding, you need to add needed dependencies for including Spire.Doc for Java into your Java project. There are two ways to do that.

Method 1: If you are using maven, you can easily import the JAR file of Spire.Doc for Java into your application by adding the following code to your project’s pom.xml file.

<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</artifactId>
        <version>11.3.0</version>
    </dependency>
</dependencies>

Method 2: If you are not using maven, you can download the JAR file of Spire.Doc for Java from the official website, extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

Count the Number of Words, Characters, Paragraphs, Pages and Lines in a Word Document in Java

The following are the main steps to count the number of words, characters, paragraphs, pages, and lines in a Word document:

  • Create an instance of the Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Create an instance of the StringBuilder class.
  • Use Document.getBuiltinDocumentProperties().getPageCount()Document.getBuiltinDocumentProperties().getWordCount()Document.getBuiltinDocumentProperties().getCharCount()Document.getBuiltinDocumentProperties().getCharCountWithSpace()Document.getBuiltinDocumentProperties().getParagraphCount(), and Document.getBuiltinDocumentProperties().getLinesCount() methods to get the count of pages, words, characters, paragraphs and lines in the document.
  • Append the results to the string builder.
  • Save the data in the string builder to a .txt file using FileWriter.write() method.
import com.spire.doc.Document;
import java.io.FileWriter;
import java.io.IOException;

public class WordCount {
    public static void main(String[] args) throws IOException {
        //Create an instance of the Document class
        Document wordDocument = new Document();
        //Load a Word document
        wordDocument.loadFromFile("Sample.docx");

        //Create an instance of the StringBuilder class
        StringBuilder outputStringBuilder = new StringBuilder();

        //Count the number of pages, words, characters, paragraphs and lines in the document, and append the results to the string builder
        int pageCount = wordDocument.getBuiltinDocumentProperties().getPageCount();
        int wordCount = wordDocument.getBuiltinDocumentProperties().getWordCount();
        int charCount = wordDocument.getBuiltinDocumentProperties().getCharCount();
        int charCountWithSpace = wordDocument.getBuiltinDocumentProperties().getCharCountWithSpace();
        int paraCount = wordDocument.getBuiltinDocumentProperties().getParagraphCount();
        int lineCount = wordDocument.getBuiltinDocumentProperties().getLinesCount();

        outputStringBuilder.append("Pages: ").append(pageCount).append(System.lineSeparator());
        outputStringBuilder.append("Words: ").append(wordCount).append(System.lineSeparator());
        outputStringBuilder.append("Characters with No Spaces: ").append(charCount).append(System.lineSeparator());
        outputStringBuilder.append("Characters with Spaces: ").append(charCountWithSpace).append(System.lineSeparator());
        outputStringBuilder.append("Paragraphs: ").append(paraCount).append(System.lineSeparator());
        outputStringBuilder.append("Lines: ").append(lineCount).append(System.lineSeparator());

        //Save the data in the string builder to a text file 
        try (FileWriter fWriter = new FileWriter("WordCount.txt", true)) {
            fWriter.write(outputStringBuilder.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Design a site like this with WordPress.com
Get started