Extract Text from Word Using Java

Import JAR Dependency

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://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>
import com.spire.doc.Document;

import java.io.FileWriter;
import java.io.IOException;

public class ExtractText {

public static void main(String[] args) throws IOException {

//Load Word document
Document document = new Document();
document.loadFromFile("Island.docx");

//Get text from document as string
String text=document.getText();

//Write string to a .txt file
writeStringToTxt(text," Extracted.txt");
}

public static void writeStringToTxt(String content, String txtFileName) throws IOException{

FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

--

--

--

Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to write a successful CDR report?

CDR Report Writing

Bon Voyage Kubernetes

Feren OS January 2020 Snapshot is here

ISSUES WITH PREPRINT SERVER: ASKING QUESTIONS TO AUTHOR?

The Easiest Way To Format Dates In JavaScript

NoSQL-Apache Casandra Architecture

AXL token? Yes!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

Java I/O Character Stream Implementation

JVM Architecture for beginners

How to Create a Selenium Project Using Maven on Intellij

File Path separator in Java