As a powerful and efficient words processing tool, Word plays an important role in our daily work. This article will introduce how to create a Word document in Java application by using Free Spire.Doc for Java, and then insert an image into it as well as set its paragraph styles.
Installation
Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.
<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>
Creating Word Document:
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 Document instance
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add 4 paragraphs to the section
Paragraph para1 = section.addParagraph();
para1.appendText("What Is Insomnia?");
Paragraph para2 = section.addParagraph();
para2.appendText("Insomnia is characterized by an inability to obtain a sufficient amount of sleep to feel rested. "+
"It can be due to either difficulty falling or staying asleep. It may also result in waking earlier than desired. "+
"The sleep is often reported to be of chronically poor quality, light, and unrefreshing. "+
"As a result of this, people with insomnia suffer from daytime symptoms like poor attention, irritability, and reduced energy.");
Paragraph para3 = section.addParagraph();
para3.appendText("Everyone has the potential to develop the kind of difficulty sleeping that characterizes insomnia. "+
"This is referred to as a predisposition or threshold. The threshold for developing insomnia will vary for each person.");
//Add an image to paragraph 4
Paragraph para4 = section.addParagraph();
DocPicture picture = para4.appendPicture("pic1.png");
//Set image width
picture.setWidth(300f);
//Set image height
picture.setHeight(250f);
//Set title style for paragraph 1
ParagraphStyle style1 = new ParagraphStyle(document);
style1.setName("titleStyle");
style1.getCharacterFormat().setBold(true);
style1.getCharacterFormat().setTextColor(Color.BLUE);
style1.getCharacterFormat().setFontName("Arial");
style1.getCharacterFormat().setFontSize(12f);
document.getStyles().add(style1);
para1.applyStyle("titleStyle");
//Set style for paragraph 2 and 3
ParagraphStyle style2 = new ParagraphStyle(document);
style2.setName("paraStyle");
style2.getCharacterFormat().setFontName("Arial");
style2.getCharacterFormat().setFontSize(11f);
document.getStyles().add(style2);
para2.applyStyle("paraStyle");
para3.applyStyle("paraStyle");
//Set horizontal alignment for paragraph 1 and paragraph 4
para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set first-line indent for paragraph 2 and 3
para2.getFormat().setFirstLineIndent(25f);
para3.getFormat().setFirstLineIndent(25f);
//Set spaces after paragraph 1, 2 and 3
para1.getFormat().setAfterSpacing(15f);
para2.getFormat().setAfterSpacing(10f);
para3.getFormat().setAfterSpacing(10f);
//Save the document
document.saveToFile("Word.docx", FileFormat.Docx);
}
}
Top comments (0)