Apache POI's HWPFDocument Example

Apache POI HWPFDocument Example: Read and Write Word Documents

Apache POI is a Java library for working with Microsoft Office files. Its HWPF module supports older binary Word documents in the .doc format. This example shows how to read text from a Word document and write text to a new one.

Note: HWPF is intended for the legacy .doc format. For modern .docx files, use Apache POI’s XWPF API instead.

Add the Apache POI Dependency

Add the HWPF component through the poi-scratchpad artifact. Keep its version aligned with the rest of your Apache POI dependencies.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>YOUR_POI_VERSION</version>
</dependency>

Read Text from a .doc File

The following example opens a legacy Word document and extracts its text:

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class ReadWordDocument {

    public static void main(String[] args) {
        String filePath = "input.doc";

        try (FileInputStream input = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(input);
             WordExtractor extractor = new WordExtractor(document)) {

            System.out.println(extractor.getText());

        } catch (IOException e) {
            System.err.println("Could not read the Word document: " + e.getMessage());
        }
    }
}

Use try-with-resources so streams and POI objects are closed even if an error occurs.

Create a .doc File

You can also create a basic legacy Word document with HWPFDocument:

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

public class CreateWordDocument {

    public static void main(String[] args) {
        String filePath = "output.doc";

        try (HWPFDocument document = new HWPFDocument()) {
            Range range = document.getRange();
            range.insertAfter("Hello from Apache POI HWPF!");

            try (FileOutputStream output = new FileOutputStream(filePath)) {
                document.write(output);
            }

            System.out.println("Document created: " + filePath);

        } catch (IOException e) {
            System.err.println("Could not create the Word document: " + e.getMessage());
        }
    }
}

Important Considerations

  • Use the correct API: HWPF handles old binary .doc files; XWPF handles Office Open XML .docx files.
  • Complex formatting: The examples show basic text operations. More advanced document structures and formatting require additional POI APIs.
  • Input validation: Validate uploaded files and handle malformed or unsupported documents safely.
  • Resource management: Close documents and streams after use. Try-with-resources is recommended.
  • Dependency versions: Use a POI version compatible with your Java runtime and keep related POI artifacts on the same version.

Conclusion

Apache POI’s HWPFDocument lets Java applications work with older .doc files. Use WordExtractor to retrieve document text, and the document’s range APIs for simple content updates or file creation. For .docx files, use XWPF instead.

Previous Post Next Post

Contact Form