[Java] [Java] Realize multi-column effect of Word document

2 minute read

Multi-column is a typesetting style commonly used in newspapers, books and magazines, which not only improves readability but also enhances page aesthetics. This article will show you how to use Free Spire.Doc for Java to add multiple columns to a Word document in a Java application and set the width, spacing and divider for each column.

Basic steps: 1. Download and unzip the Free Spire.Doc for Java package. 2. Import the Spire.Doc.jar package from the lib folder as a dependency into your Java application. (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 (here named MutiColumn), enter the corresponding Java code and run it.

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 width and spacing for each column
        section.addColumn(100, 30);
        section.addColumn(100, 30);
        section.addColumn(100, 30);

        // Add column dividing line
        section.getPageSetup().setColumnsLineBetween(true);

        String paraText1 = "Trouble is a privilege of youth, I want you to worry a lot. By worrying, people acquire the power to think."
                + "But there are some worries that do not give you the ability to think. One of them is to mitigate what has happened."
                +"This does not give an answer no matter how much you worry, it just leads to energy drain.";

        String paraText2 = "I'm thinking that I'm regretful, but the worries of turning myself into blaming myself aren't too productive."
                + "On the contrary, there are people who try to transfer responsibility to someone immediately."
                + "It's even worse, because those people have the illusion that they are thinking about things.";

        String paraText3 = "In order to acquire the ability to think, first of all, we must develop the attitude of accepting things as they are."
                + "Next, you need to calmly determine the cause without feeling distracted."
                + "In order to do that, we have to have a habit of catching things from multiple perspectives.";

        //Add paragraph to section
        Paragraph paragraph = section.addParagraph();
        // add text to paragraph
        paragraph.appendText(paraText1);
        // Add a column break (text after the 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 paragraph style, set font and size
        ParagraphStyle paragraphStyle = new ParagraphStyle(document);
        paragraphStyle.setName("style");
        paragraphStyle.getCharacterFormat().setFontName("Song style");
        paragraphStyle.getCharacterFormat().setFontSize(12);
        document.getStyles().add(paragraphStyle);

        // apply paragraph style to paragraph
        for(int i = 0; i< section.getParagraphs().getCount(); i++){
            section.getParagraphs().get(i).applyStyle("style");
        }

        // save the result file
        document.saveToFile("MutiColumn.docx", FileFormat.Docx_2013);

    }
}

Output document: multicolumn.png