Java: Create a Word Document

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>10.6.6</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.