Multi-column is a typesetting style commonly used in newspapers, books and magazines, which not only improves readability, but also improves page beauty. This article will show you how to use Free Spire.Doc for Java to add multiple columns to your Java application's Word document and set the width, spacing, and dividing lines for each column.
** Basic steps: ** ** 1. ** Free Spire.Doc for Java Download and unzip the package. ** 2. ** Import the Spire.Doc.jar package in the lib folder into your Java application as a dependency. (You can also install the JAR package directly from the Maven repository (see below for the code that makes up the pom.xml file)). ** 3. ** In your Java application, create a new Java Class (named MutiColumn here) and enter and execute the corresponding Java code.
** Configure the pom.xml file: **
<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 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 section
        Section section = document.addSection();
        //Add 3 columns to section and set the width and spacing of each column
        section.addColumn(100, 30);
        section.addColumn(100, 30);
        section.addColumn(100, 30);
        //Add column dividing line
        section.getPageSetup().setColumnsLineBetween(true);
        String paraText1 = "It is a privilege of youth to worry, and I want you to worry a lot. By worrying, people acquire the ability to think."
                +"However, there are some worries that the ability to think is not acquired. One of them. To confuse what has happened."
                +"No matter how much you worry, this does not give you an answer, it just leads to energy consumption.";
        String paraText2 = "I'm going to reflect on it, but it's not very productive to worry that I'm blaming myself before I know it."
                +"On the contrary, some people try to transfer responsibility to someone immediately."
                +"The quality is even worse because such people have the illusion that they are thinking in a rational way.";
        String paraText3 = "To acquire the ability to think, you must first develop an attitude of accepting things as they are."
                +"Next, it is necessary to calmly identify the cause without being influenced by emotions."
                +"To do so, we must develop a habit of looking at things from multiple perspectives.";
        //Add paragraph to section
        Paragraph paragraph = section.addParagraph();
        //Add text to paragraph
        paragraph.appendText(paraText1);
        //Add column break (text after column break starts in the next column)
        paragraph.appendBreak(BreakType.Column_Break);
        //Add paragraph to section
        paragraph = section.addParagraph();
        //Add text to paragraph
        paragraph.appendText(paraText2);
        //Add column break
        paragraph.appendBreak(BreakType.Column_Break);
        //Add paragraph to section
        paragraph = section.addParagraph();
        //Add text to paragraph
        paragraph.appendText(paraText3);
        //Create a paragraph style and set the font and size
        ParagraphStyle paragraphStyle = new ParagraphStyle(document);
        paragraphStyle.setName("style");
        paragraphStyle.getCharacterFormat().setFontName("Song body");
        paragraphStyle.getCharacterFormat().setFontSize(12);
        document.getStyles().add(paragraphStyle);
        //Apply paragraph styles to paragraphs
        for(int i = 0; i< section.getParagraphs().getCount(); i++){
            section.getParagraphs().get(i).applyStyle("style");
        }
        //Save result file
        document.saveToFile("MutiColumn.docx", FileFormat.Docx_2013);
    }
}
** Output document: **

Recommended Posts