Java create word document

A Word file is supposed to be created without Word with the help of Java application protocol interfaces.

Concepts Involved: Apache POI and Maven

Apache POI Is an API provided by Apache foundation which is a collection of different java libraries. This facility gives the library to read, write, and manipulate different Microsoft files such as Excel sheets, PowerPoint, and Word files. There are two types basically older version including ‘.doc’, ‘.ppt’ while newer versions of files as ‘.docx’, ‘.pptx’. There are two ways to deal with Apache POI as mentioned below:

Binary Distribution  Source Distribution 
No compilation is required to generate file.jar files  Files to generated needs to be compiled in order to generate file.jar files 
If working with Linux go for the ‘.tar.gz’ version (version 3.5 backward) otherwise simply download the .zip. the file extension for installation to work on Apache POI.  Here format does not matter because file is not compiled so can be altered as per need 

Here zip file is taken for consideration and also if the operating system is Windows, zip files should be preferred. It is a simple java project so binary distribution API is used. In the process of creating the document, several paragraphs will be inserted as a sample to display output and will be providing styles to paragraphs such as font color, font name, and font size. 

Now in order to create a Word file without using Microsoft Word, there is a java interface called Spire and if there is a need to create a PDF document without using Adobe Acrobat then it can be done with the use of an interface known as ‘E-Ice blue’. Here ‘Spire.doc’ must have to be imported as per problem statement as all dealing is in word format.

Spire.Doc for Java is a professional Java Word API that enables Java applications to create, convert, manipulate, and print Word documents without using Microsoft Office. It will be imported as a reference for this program as a reference. 

Syntax: For importing libraries in java of Spire

import Spire.Doc.jar ;

Components Of Spire involved are as follows:

Component Responsibility
XML Word Processor Format (XWPF) Reading and writing in ‘docx’ extensions files of MS-Word
Horrible Word Processor Format (HWPF) Reading and writing in ‘doc’ extensions files of MS-Word 

Additionally, there is another Java API for PDF format as discussed above ‘E-Ice Blue’ that enables developers to read, write, convert, and print PDF documents in Java applications without using Adobe Acrobat. Similarly, a PowerPoint API allows developers to create, read, edit, convert, and print PowerPoint files within Java applications.

If user is creating shear Maven project, one can easily add the jar dependency by adding the following configurations to the pom.xml.

Now, just like any class Apache POI contains classes and methods to work on. Major components of Apache POI are discussed below to understand the internal working of a file, how it is been generated without Word with help of classes and methods. There are basically two versions of files been present in Word itself.

Older files extensions Newer file extension (Version 3.5)
doc docx 
xls xlsx
ppt pptx

Maven 

These files are accessed using Maven. Maven is a powerful project management tool that is based on POM (project object model). It is used for projects to build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.
In short terms we can tell maven is a tool that can be used for building and managing any Java-based project. Maven makes the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.

Maven does have some specific commands to deal with files. Most frequently used are:

poi + poi + scratchpadpoi    // In order to deal with older Word file versions 
poi + poi-ooxml              // In order to deal with new Word file versions

Implementation: Below program depicts the responsibility of Spire API in creating a Word file:

Java

import com.spire.doc.Document;

import com.spire.doc.FileFormat;

import com.spire.doc.Section;

import com.spire.doc.documents.BuiltinStyle;

import com.spire.doc.documents.Paragraph;

import com.spire.doc.documents.ParagraphStyle;

class GFG {

    public static void main(String[] args)

    {

        Document document = new Document();

        Section section = document.addSection();

        Paragraph heading = section.addParagraph();

        heading.appendText("Java");

        Paragraph subheading_1 = section.addParagraph();

        subheading_1.appendText("What's Java");

        Paragraph para_1 = section.addParagraph();

        para_1.appendText(

            "Java is a general purpose, high-level programming language developed by Sun Microsystems."

            + " The Java programming language was developed by a small team of engineers, "

            + "known as the Green Team, who initiated the language in 1991.");

        Paragraph para_2 = section.addParagraph();

        para_2.appendText(

            "Originally called OAK, the Java language was designed for handheld devices and set-top boxes. "

            + "Oak was unsuccessful and in 1995 Sun changed the name to Java and modified the language to take "

            + "advantage of the burgeoning World Wide Web. ");

        Paragraph subheading_2 = section.addParagraph();

        subheading_2.appendText("Java Today");

        Paragraph para_3 = section.addParagraph();

        para_3.appendText(

            "Today the Java platform is a commonly used foundation for developing and delivering content "

            + "on the web. According to Oracle, there are more than 9 million Java developers worldwide and more "

            + "than 3 billion mobile phones run Java.");

        heading.applyStyle(BuiltinStyle.Title);

        subheading_1.applyStyle(BuiltinStyle.Heading_3);

        subheading_2.applyStyle(BuiltinStyle.Heading_3);

        ParagraphStyle style = new ParagraphStyle(document);

        style.setName("paraStyle");

        style.getCharacterFormat().setFontName("Arial");

        style.getCharacterFormat().setFontSize(11f);

        document.getStyles().add(style);

        para_1.applyStyle("paraStyle");

        para_2.applyStyle("paraStyle");

        para_3.applyStyle("paraStyle");

        for (int i = 0;

             i < section.getParagraphs().getCount(); i++) {

            section.getParagraphs()

                .get(i)

                .getFormat()

                .setAfterAutoSpacing(true);

        }

        document.saveToFile(

            "output/CreateAWordDocument.docx",

            FileFormat.Docx);

    }

}

Output:

package ua.com.prologistic;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

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

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

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

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

import java.io.FileOutputStream;

public class WordWorker {

    public static void main(String[] args) {

        try {

            // создаем модель docx документа,

            // к которой будем прикручивать наполнение (колонтитулы, текст)

            XWPFDocument docxModel = new XWPFDocument();

            CTSectPr ctSectPr = docxModel.getDocument().getBody().addNewSectPr();

            // получаем экземпляр XWPFHeaderFooterPolicy для работы с колонтитулами

            XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(docxModel, ctSectPr);

            // создаем верхний колонтитул Word файла

            CTP ctpHeaderModel = createHeaderModel(

            «Верхний колонтитул — создано с помощью Apache POI на Java :)»

            );

            // устанавливаем сформированный верхний

            // колонтитул в модель документа Word

            XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeaderModel, docxModel);

            headerFooterPolicy.createHeader(

                    XWPFHeaderFooterPolicy.DEFAULT,

                    new XWPFParagraph[]{headerParagraph}

            );

            // создаем нижний колонтитул docx файла

            CTP ctpFooterModel = createFooterModel(«Просто нижний колонтитул»);

            // устанавливаем сформированый нижний

            // колонтитул в модель документа Word

            XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooterModel, docxModel);

            headerFooterPolicy.createFooter(

                    XWPFHeaderFooterPolicy.DEFAULT,

                    new XWPFParagraph[]{footerParagraph}

            );

            // создаем обычный параграф, который будет расположен слева,

            // будет синим курсивом со шрифтом 25 размера

            XWPFParagraph bodyParagraph = docxModel.createParagraph();

            bodyParagraph.setAlignment(ParagraphAlignment.RIGHT);

            XWPFRun paragraphConfig = bodyParagraph.createRun();

            paragraphConfig.setItalic(true);

            paragraphConfig.setFontSize(25);

            // HEX цвет без решетки #

            paragraphConfig.setColor(«06357a»);

            paragraphConfig.setText(

            «Prologistic.com.ua — новые статьи по Java и Android каждую неделю. Подписывайтесь!»

            );

            // сохраняем модель docx документа в файл

            FileOutputStream outputStream = new FileOutputStream(«F:/Apache POI Word Test.docx»);

            docxModel.write(outputStream);

            outputStream.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

        System.out.println(«Успешно записан в файл»);

    }

    private static CTP createFooterModel(String footerContent) {

        // создаем футер или нижний колонтитул

        CTP ctpFooterModel = CTP.Factory.newInstance();

        CTR ctrFooterModel = ctpFooterModel.addNewR();

        CTText cttFooter = ctrFooterModel.addNewT();

        cttFooter.setStringValue(footerContent);

        return ctpFooterModel;

    }

    private static CTP createHeaderModel(String headerContent) {

        // создаем хедер или верхний колонтитул

        CTP ctpHeaderModel = CTP.Factory.newInstance();

        CTR ctrHeaderModel = ctpHeaderModel.addNewR();

        CTText cttHeader = ctrHeaderModel.addNewT();

        cttHeader.setStringValue(headerContent);

        return ctpHeaderModel;

    }

}

The Word file format was invented by Microsoft and has gradually become irreplaceable. Most printed and digital publications, reports, brochures and resumes that we’ve seen were created with Word. In this article, you will learn how to programmatically create a Word document in Java using Spire.Doc for Java.

The Document class provided by Spire.Doc for Java represents a document model, which is used to create a Word document from scratch or load an existing Word file for further modification. A Word document must contain at least one section (represented by Section class) and each section is a container for basic Word elements like paragraphs, tables, headers, footers and so on. Below is a list of the key classes and methods covered in this tutorial.

Member Description
Document class Represents a Word document model.
Section class Represents a section in a Word document.
Paragraph class Represents a paragraph in a section.
ParagraphStyle class Defines the font formatting information that can be applied to a paragraph.
Section.addParagraph() method Adds a paragraph to a section.
Paragraph.appendText() method Appends text to a paragraph at the end.
Paragraph.applyStyle() method Applies a style to a paragraph.
Document.saveToFile() method Saves the document to a Word file with an extension of .doc or .docx. This method also supports saving the document to PDF, XPS, HTML, PLC, etc.

Install Spire.Doc for Java

First, you’re required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in 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.11</version>
    </dependency>
</dependencies>
    

Create a Simple Word Document in Java

The following are the steps to create a simple Word document containing several paragraphs by using Spire.Doc for Java.

  • Create a Document object.
  • Add a section using Document.addSection() method.
  • Set the page margins using Section.getPageSetup().setMargins() method.
  • Add several paragraphs to the section using Section.addParagraph() method.
  • Add text to the paragraphs using Paragraph.appendText() method.
  • Create ParagraphStyle objects, and apply them to separate paragraphs using Paragraph.applyStyle() method.
  • Save the document to a Word file using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

public class CreateWordDocument {

    public static void main(String[] args) {

        //Create a Document object
        Document doc = new Document();

        //Add a section
        Section section = doc.addSection();

        //Set the page margins
        section.getPageSetup().getMargins().setAll(40f);

        //Add a paragraph as title
        Paragraph titleParagraph = section.addParagraph();
        titleParagraph.appendText("Introduction of Spire.Doc for Java");

        //Add two paragraphs as body
        Paragraph bodyParagraph_1 = section.addParagraph();
        bodyParagraph_1.appendText("Spire.Doc for Java is a professional Word API that empowers Java applications to " +
                "create, convert, manipulate and print Word documents without dependency on Microsoft Word.");

        Paragraph bodyParagraph_2 = section.addParagraph();
        bodyParagraph_2.appendText("By using this multifunctional library, developers are able to process copious tasks " +
                "effortlessly, such as inserting image, hyperlink, digital signature, bookmark and watermark, setting " +
                "header and footer, creating table, setting background image, and adding footnote and endnote.");

        //Create and apply a style for title paragraph
        ParagraphStyle style1 = new ParagraphStyle(doc);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);;
        style1.getCharacterFormat().setTextColor(Color.BLUE);
        style1.getCharacterFormat().setFontName("Times New Roman");
        style1.getCharacterFormat().setFontSize(12f);
        doc.getStyles().add(style1);
        titleParagraph.applyStyle("titleStyle");

        //Create and apply a style for body paragraphs
        ParagraphStyle style2 = new ParagraphStyle(doc);
        style2.setName("paraStyle");
        style2.getCharacterFormat().setFontName("Times New Roman");
        style2.getCharacterFormat().setFontSize(12);
        doc.getStyles().add(style2);
        bodyParagraph_1.applyStyle("paraStyle");
        bodyParagraph_2.applyStyle("paraStyle");

        //Set the horizontal alignment of paragraphs
        titleParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        bodyParagraph_1.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify);
        bodyParagraph_2.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify);

        //Set the first line indent
        bodyParagraph_1.getFormat().setFirstLineIndent(30) ;
        bodyParagraph_2.getFormat().setFirstLineIndent(30);

        //Set the after spacing
        titleParagraph.getFormat().setAfterSpacing(10);
        bodyParagraph_1.getFormat().setAfterSpacing(10);

        //Save to file
        doc.saveToFile("WordDocument.docx", FileFormat.Docx_2013);
        doc.close();
    }
}

Java: Create a Word Document

Apply for a Temporary License

If you’d like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

How to generate MS word documents automatically? I started to search how to accomplish this task and found the Apache POI library. We can easily work with MS Office documents (word, excel, etc.) with the Apache POI library. For details please visit https://poi.apache.org.

I want to continue with my case. I needed to get values (parameters) from a text file or excel file and generate multiple word documents according to those values. For simplicity, I selected to use a text file for reading parameters and then generate word documents. Lets’ get started!

I divided my code into three parts.

1) ReadFile Class: Reads the text file and returns a list of values. These values will be our parameters for word generation.

2) WordGenerator Class: It contains getLines and createWord methods. First, we get the values with getLines method and createWord method to create word documents.

3) Main Class: This is our top-level class in which we instantiate WordGenerator Class and call its methods.

ReadFile Class

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ReadFile {
    public List<String> readLines(File filename) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
        List<String> lines = bufferedReader.lines()
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
        bufferedReader.close();
        return lines;
    }
}

WordGenerator Class

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class WordGenerator {
    public List<String> getLines(File fileName) throws Exception {
        ReadFile rf = new ReadFile();
        List<String> lines = rf.readLines(fileName);
        lines.forEach(System.out::println);
        return lines;
    }

    //Create Word
    public void createWord(List<String> lines) throws IOException {
        //Check the generated path. If it is not there, create it.
        if (!Paths.get("./generated").toFile().exists()) Files.createDirectories(Paths.get("./generated"));
        //Create Word docs.
        for (String line : lines) {
            //Blank Document
            XWPFDocument document = new XWPFDocument();
            //Write the Document in file system
            FileOutputStream out = new FileOutputStream("generated/" + "createdWord" + "_" + line + ".docx");
            //create Paragraph
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("VK Number (Parameter): " + line + " here you type your text...n");
            document.write(out);
            //Close document
            out.close();
            System.out.println("createdWord" + "_" + line + ".docx" + " written successfully");
        }
    }
}

Main Class

import java.io.File;
import java.util.List;
import java.util.Objects;

public class Main {
    public static void main(String[] args) throws Exception {
        //Instantiate WordGenerator Class
        WordGenerator wg = new WordGenerator();

        //Gather VK Lines from text file
        List<String> vkLines = wg.getLines(
            new File((Objects.requireNonNull(Main.class.getClassLoader().getResource("vk_no.txt")).toURI())));

        //Create word document according to VK lines
        wg.createWord(vkLines);
    }
}

Input Text File

11111
22222
33333
44444
55555

Console Output and Result:

Github Project

https://github.com/swtestacademy/word-file-generator

Thanks,
Onur Baskirt

onur baskirt

Onur Baskirt is a Software Quality Leader with international experience in world-class companies. Now, he is a Lead Technical Consultant at Emirates Airlines in Dubai.

Понравилась статья? Поделить с друзьями:
  • Java таблица как в excel
  • Java api for word
  • Java сохранение в excel
  • Java apache poi excel формула
  • Java apache poi excel примеры