Java POI Word Demo

Introduction

Microsoft Word is one of the most widely used word processing software in the world. It allows users to create and edit documents, add formatting, images, tables, and more. In this article, we will explore how to work with Word documents programmatically using Java and the Apache POI library.

Apache POI is a popular Java library that allows developers to create, modify, and extract data from Microsoft Office documents, including Word, Excel, and PowerPoint. It provides a set of Java APIs to read and write Office files using the Office Open XML (OOXML) and Microsoft's OLE 2 Compound Document formats. In this article, we will focus on working with Word documents.

Prerequisites

Before we start, make sure you have the following prerequisites installed on your machine:

  • Java Development Kit (JDK)
  • Apache POI library

You can download the Apache POI library from the official website or include it as a dependency in your Maven or Gradle project.

Creating a Word Document

To create a new Word document using Java and Apache POI, we first need to create an instance of the XWPFDocument class, which represents a Word document. We can then add content to the document, such as paragraphs, tables, and images.

Here's an example of how to create a simple Word document with a title and a paragraph:

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordDocument {
    public static void main(String[] args) {
        // Create a new Word document
        XWPFDocument document = new XWPFDocument();

        // Create a title
        XWPFParagraph title = document.createParagraph();
        XWPFRun run = title.createRun();
        run.setText("My Word Document");
        run.setBold(true);
        run.setFontSize(16);

        // Create a paragraph
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run2 = paragraph.createRun();
        run2.setText("This is a simple Word document created using Java and Apache POI.");

        // Save the document
        try (FileOutputStream out = new FileOutputStream("my_document.docx")) {
            document.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this code snippet, we first create an instance of XWPFDocument, which represents the Word document. We then create a title paragraph using createParagraph() and add a run with bold text, increased font size, and the desired title text.

Next, we create a regular paragraph and add a run with the desired content. Finally, we save the document by writing it to a file using FileOutputStream.

Modifying an Existing Word Document

In addition to creating new Word documents, we can also modify existing documents using Apache POI. To do this, we need to load the document from a file, make the necessary modifications, and save it back to the file.

Here's an example of how to open an existing Word document, modify its content, and save it back to the file:

import org.apache.poi.xwpf.usermodel.*;

public class ModifyWordDocument {
    public static void main(String[] args) {
        try (FileInputStream in = new FileInputStream("my_document.docx")) {
            // Open the existing Word document
            XWPFDocument document = new XWPFDocument(in);

            // Get the first paragraph
            XWPFParagraph paragraph = document.getParagraphs().get(0);

            // Modify the content of the first paragraph
            for (XWPFRun run : paragraph.getRuns()) {
                run.setBold(true);
            }

            // Save the modified document
            try (FileOutputStream out = new FileOutputStream("my_document_modified.docx")) {
                document.write(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this code snippet, we first open the existing Word document using XWPFDocument and FileInputStream. We then retrieve the first paragraph from the document using getParagraphs().get(0). We can then modify the content of the paragraph by updating the properties of its runs.

In this example, we set the bold property of all runs in the first paragraph to true. Finally, we save the modified document to a new file using FileOutputStream.

Conclusion

In this article, we explored how to work with Word documents programmatically using Java and the Apache POI library. We learned how to create a new Word document, add content such as paragraphs and titles, and save the document to a file. We also learned how to modify an existing Word document by opening it, making the necessary modifications, and saving it back to the file.

Apache POI provides a powerful set of APIs for working with Microsoft Office documents, giving developers the ability to automate complex tasks and manipulate documents as needed.