Java implementation of Word document column effect

Column is a common typesetting style of newspapers, books and magazines. It can not only facilitate reading, but also increase the beauty of the page. This article introduces how to add multiple columns to Word documents in Java applications to achieve column splitting, and how to set the width, spacing and split line of each column.

Using tool: free flame.doc for Java (free version)

Jar file import method
Method 1:
download Free Spire.Doc for Java Package and extract it, then import the spirit.doc.jar package into your Java application from the lib folder. (see the figure below after importing successfully)

Method 2:
Import through Maven warehouse installation. Please refer to the link for detailed operation steps:

Java code example

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class CreateMutiColumnWordDocument {
    public static void main(String[] args){
        //Create a Word document
        Document document = new Document();
        //Add a section
        Section section = document.addSection();

        //Add three columns to the section and set the width and spacing of each column
        section.addColumn(100, 30);
        section.addColumn(100, 30);
        section.addColumn(100, 30);

        //Add column divider

        String paraText1 = "The moon, commonly known as the moon, also known as the Taiyin and xuantu in ancient times, is the only natural satellite of the earth and the fifth largest satellite in the solar system."
                +"The diameter of the moon is one quarter of the earth's, and its mass is one eightieth of the Earth's. compared with the surrounding planets, the moon is the satellite with the largest mass and the second highest density in the solar system, second only to Europa."
                +"The surface of the moon is covered with craters formed by the impact of meteorites. The distance between the moon and the earth is about 30 times the diameter of the earth.";

        String paraText2 = "Synchronization of rotation and revolution of the moon(Tidal locking ),So it always faces the earth on the same side."
                +"The gravitational influence of the moon causes tides in the earth's oceans and prolongs the time of each day."
                +"The ratio of the size of the moon and the sun is close to the ratio of distance, which makes its apparent size almost the same as that of the sun.";

        String paraText3 = "The moon is the first extraterrestrial planet that man has ever landed on."
                +"The Soviet moon program launched the first unmanned spacecraft to the moon in 1959;U.S.A NASA So far, the Apollo program is the only manned mission to the moon."
                +"2019 On January 3, 2004, China's chang'e-4 successfully landed on the back of the moon, the first soft landing on the back of the moon."
                +"1 On April 15, chang'e-4 completed the first biological experiment on the moon, and the first bud grew on the moon. On February 15, five more "Chinese place names" were added to the moon.";

        //Add paragraph to section
        Paragraph paragraph = section.addParagraph();
        //Add text to paragraph
        //Add a column break (the text after the break will start in the next column)

        //Add paragraph to section
        paragraph = section.addParagraph();
        //Add text to paragraph
        //Add column break

        //Add paragraph to section
        paragraph = section.addParagraph();
        //Add text to paragraph

        //Create paragraph styles and set font and size
        ParagraphStyle paragraphStyle = new ParagraphStyle(document);
        paragraphStyle.getCharacterFormat().setFontName("Song style");

        //Apply paragraph styles to paragraphs
        for(int i = 0; i< section.getParagraphs().getCount(); i++){

        //Save result document
        document.saveToFile("Column effect.docx", FileFormat.Docx_2013);


Column rendering:

Tags: Java Maven

Posted on Sat, 09 May 2020 11:00:20 -0400 by rm_phpbuilder