Java creates a Word document

Word has powerful word processing capabilities and is one of the most widely used tools in everyday work and life. In this article, I'll show you how to use Free Spire.Doc for Java to create Word documents in Java applications, insert images, and set font formats, alignment, indentation, and paragraph spacing.

** Import JAR package ** ** Method 1: ** Download Free Spire.Doc for Java, unzip it, and in the lib folder Import the Spire.Doc.jar package into your Java application as a dependency. ** Method 2: ** Install the JAR package via the Maven repository and configure the pom.xml file as follows:

<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>2.7.3</version>
    </dependency>
</dependencies>

** Java code **

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;

import java.awt.*;

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

        //Added section
        Section section = document.addSection();

        //Add 4 paragraphs to the section
        Paragraph para1 = section.addParagraph();
        para1.appendText("Being too thin increases the risk of depression ́");

        Paragraph para2 = section.addParagraph();
        para2.appendText("The slender style has always been a longing for women, but the number of men who are beginning to care about style is increasing in recent years."+
                "Some people choose to lose weight, such as dietary restrictions, liposuction, and injections."+
                "According to a study by Seoul National University School of Medicine in South Korea, if you are too thin, you will not feel joy and you will be at high risk of depression.");

        Paragraph para3 = section.addParagraph();
        para3.appendText("After analyzing data from 183 individual studies, researchers found that people who were too light had a better Happiness Index than others and were at increased risk of suffering from mental illness."+
                "Many people start by suppressing their appetite when they go on a diet, which can disrupt their physical function and make them more reluctant."+
                "On the other hand, people who are too thin have a low body fat percentage, and undernourishment damages brain cells, impairs memory, and is psychologically affected.");

        //Add an image to paragraph 4
        Paragraph para4 = section.addParagraph();
        DocPicture picture = para4.appendPicture("img.jpg ");
        //Set the width of the image
        picture.setWidth(300f);
        //Set the height of the image
        picture.setHeight(220f);

        //Use the first paragraph as the title and format the title
        ParagraphStyle style1 = new ParagraphStyle(document);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(Color.BLUE);
        style1.getCharacterFormat().setFontName("Mincho");
        style1.getCharacterFormat().setFontSize(12f);
        document.getStyles().add(style1);
        para1.applyStyle("titleStyle");

        //Format paragraphs 2 and 3
        ParagraphStyle style2 = new ParagraphStyle(document);
        style2.setName("paraStyle");
        style2.getCharacterFormat().setFontName("Mincho");
        style2.getCharacterFormat().setFontSize(11f);
        document.getStyles().add(style2);
        para2.applyStyle("paraStyle");
        para3.applyStyle("paraStyle");

        //Center paragraphs 1 and 4 horizontally
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Set paragraph 2 and paragraph 3 as the indent for the first line
        para2.getFormat().setFirstLineIndent(25f);
        para3.getFormat().setFirstLineIndent(25f);

        //Set a space after the first three paragraphs
        para1.getFormat().setAfterSpacing(15f);
        para2.getFormat().setAfterSpacing(10f);
        para3.getFormat().setAfterSpacing(10f);

        //Save the document
        document.saveToFile("Word Document.docx", FileFormat.Docx);
    }
}

Recommended Posts

Java creates a Word document
Java creates a table in a Word document
How to print a Java Word document
Java creates an Excel document
[Java] Add WordArt to Word document
[Java] Realize multi-column effect of Word document
Java creates a pie chart in Excel
Java compressed PDF document
[Java] Create a filter
java build a triangle
Merge Java Word documents
Java reserved word summary
A note about Java GC
Create a java method [Memo] [java11]
[Java] Create a temporary file
Find a subset in Java
What is a Java collection?
[Java] Make it a constant
[Java] Draw a simple pattern
Java sets Excel document properties
Make a rhombus using Java
Think of a Java update strategy
I created a PDF in Java.
How to make a Java container
Upload a file using Java HttpURLConnection
Beginner's word No4 (Java command, JVM)
A person writing C ++ tried writing Java
A really scary (Java anti-pattern) story
Java adds hyperlinks to Word documents
How slow is a Java Scanner?
Sort a List of Java objects
Run a batch file from Java
Access Teradata from a Java application
[Java] How to create a folder
A simple sample callback in Java
Add watermark to Java to PDF document
A brief description of JAVA dependencies
Java Calendar is not a singleton.
Java encryption, decryption of Word documents
How to make a Java array
I touched Tribuo published by Oracle. Document Tribuo --A Java prediction library (v4.0)