Word documents have been widely used to create reports, quotations, invoices, etc. Generating Word documents automatically could save us lots of time and energy. This article will show you how to create a Word document on Java applications.
Add Spire.Doc.jar as a dependency
First of all,
download Free Spire.Doc for Java 2.7.3. Unzip it, and you’ll get the jar file from the ‘lib’ folder. Then, create a Java application in your IDE. Import the jar file in your project as a dependency.
In the following sections, I will introduce how to insert different elements such as text, paragraphs, tables, lists and images, to a Word document.
Create a Word document
Usually, a large part of the content in a Word document is text. Firstly, we need to create a new instance of Document. And then add a section by Document.addSection() method and add a paragraph to the section by Section.addParagraph() method. After that, add text strings in paragraph by Paragraph.appendText(string text) method. We could also set the styles and format by ParagraphStyle. Finally, invoke document.saveToFile() method to save the document.
-
- Document document = new Document();
-
-
- Section section = document.addSection();
-
-
- Paragraph para1 = section.addParagraph();
- para1.appendText("How to create Word document in Java");
-
- Paragraph para2 = section.addParagraph();
- para2.appendText("This article demonstrate how to create Word document with text, images, lists and tables.");
-
-
- 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");
-
-
- ParagraphStyle style2 = new ParagraphStyle(document);
- style2.setName("paraStyle");
- style2.getCharacterFormat().setFontName("Arial");
- style2.getCharacterFormat().setFontSize(11f);
- document.getStyles().add(style2);
- para2.applyStyle("paraStyle");
-
-
-
- para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
-
-
- para2.getFormat().setFirstLineIndent(12f);
-
-
- para1.getFormat().setAfterSpacing(15f);
- para2.getFormat().setAfterSpacing(10f);
-
-
- document.saveToFile("Output.docx", FileFormat.Docx);
Output
Create a table in Word
Table sets text into rows and columns, which makes the text easy to edit and check. Spire.Doc for Java offers a method section.addTable() to add tables to the Word document. We can use Table.resetCells(int rowsNum, int columnsNum) method to set row and column numbers.
-
- Document document = new Document();
-
-
- Section section = document.addSection();
-
-
- String[] header = {"Name", "Gender"};
- String[][] data =
- {
- new String[]{"Winny", "Female"},
- new String[]{"Lois", "Female"},
- new String[]{"Jois", "Female"},
- new String[]{"Moon", "Male"},
- new String[]{"Vinit", "Female"},
- };
-
-
- Table table = section.addTable();
- table.resetCells(data.length + 1, header.length);
- table.applyStyle(DefaultTableStyle.Colorful_List);
- table.getTableFormat().getBorders().setBorderType(BorderStyle.Single);
-
-
- TableRow row = table.getRows().get(0);
- row.isHeader(true);
- row.setHeight(20);
- row.setHeightType(TableRowHeightType.Exactly);
- row.getRowFormat().setBackColor(Color.gray);
- for (int i = 0; i < header.length; i++) {
- row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
- Paragraph p = row.getCells().get(i).addParagraph();
- p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
- TextRange range1 = p.appendText(header[i]);
- range1.getCharacterFormat().setFontName("Arial");
- range1.getCharacterFormat().setFontSize(12f);
- range1.getCharacterFormat().setBold(true);
- }
-
-
- for (int r = 0; r < data.length; r++) {
- TableRow dataRow = table.getRows().get(r + 1);
- dataRow.setHeight(25);
- dataRow.setHeightType(TableRowHeightType.Exactly);
- dataRow.getRowFormat().setBackColor(Color.white);
- for (int c = 0; c < data[r].length; c++) {
- dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
- TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
- range2.getCharacterFormat().setFontName("Arial");
- range2.getCharacterFormat().setFontSize(10f);
- }
- }
-
-
- for (int j = 1; j < table.getRows().getCount(); j++) {
- if (j % 2 == 0) {
- TableRow row2 = table.getRows().get(j);
- for (int f = 0; f < row2.getCells().getCount(); f++) {
- row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));
- }
- }
- }
-
-
- document.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
Output
Create lists in Word
There are two kinds of lists in Word: bulleted lists and numbered lists. Spire.Doc for Java offers Paragraph.getListFormat().applyBulletStyle() method and Paragraph.getListFormat().applyNumberedStyle() method to add the bulleted list and numbered list to the Word document.
-
- Document document = new Document();
-
- Section section = document.addSection();
-
- Paragraph paragraph1 = section.addParagraph();
- paragraph1.appendText("Bulleted List");
- paragraph1.applyStyle(BuiltinStyle.Heading_1);
- Paragraph paragraph2 = section.addParagraph();
- paragraph2.appendText("Chapter 1");
- Paragraph paragraph3 = section.addParagraph();
- paragraph3.appendText("Chapter 2");
- Paragraph paragraph4 = section.addParagraph();
- paragraph4.appendText("Chapter 3");
- Paragraph paragraph5 = section.addParagraph();
- paragraph5.appendText("Numbered List");
- paragraph5.applyStyle(BuiltinStyle.Heading_1);
- Paragraph paragraph6 = section.addParagraph();
- paragraph6.appendText("Chapter 1");
- Paragraph paragraph7 = section.addParagraph();
- paragraph7.appendText("Chapter 2");
- Paragraph paragraph8 = section.addParagraph();
- paragraph8.appendText("Chapter 3");
-
- for(int i = 1; i < 4; i++){
- Paragraph para = section.getParagraphs().get(i);
- para.getListFormat().applyBulletStyle();
- para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
- }
-
- for(int i = 5; i < 8; i++){
- Paragraph para = section.getParagraphs().get(i);
- para.getListFormat().applyNumberedStyle();
- para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
- }
-
- document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
Output
Add an image to Word
Images can beautify the document to attract more readers. Insertion of an image to Word documents can easily be done by using paragraph.appendPicture() method.
-
- Document document = new Document();
-
-
- Section section = document.addSection();
-
-
- Paragraph paragraph = section.addParagraph();
-
-
- DocPicture picture = paragraph.appendPicture("Image.png");
-
-
- picture.setWidth(300f);
- picture.setHeight(200f);
-
-
- document.saveToFile("InsertImage.docx", FileFormat.Docx_2013);
Output
Besides creating Word documents from scratch, Spire.Doc also supports to operate the existing Word documents, such as saving Word to PDF/image, adding digital signature to Word, etc. We will introduce more Word functions on Java platform in the future. Enjoy your reading!