News Category

Create a Multi-Column Word Document in Java

2019-08-07 09:25:13 Written by  support iceblue
Rate this item
(0 votes)

We can format Word document in a multi-column newsletter layout by adding columns. This article demonstrates how to add multiple columns to a Word document and specify the column width and the spacing between columns using Spire.Doc for Java.

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

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

        //add 3 columns to the section
        section.addColumn(100, 20);
        section.addColumn(100, 20);
        section.addColumn(100, 20);

        //add a paragraph to the section
        Paragraph paragraph = section.addParagraph();
        //add a paragraph to the section
        paragraph = section.addParagraph();
        String text = "Spire.Doc for Java is a professional Java Word API that enables Java applications "
        +"to create, convert, manipulate and print Word documents without using Microsoft Office.";
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);

        //add line between columns
        section.getPageSetup().setColumnsLineBetween(true);

        //save the resultant document
        document.saveToFile("Muti-Column Document.docx", FileFormat.Docx_2013);

    }
}

Output:

Create a Multi-Column Word Document in Java

Additional Info

  • tutorial_title:
Last modified on Tuesday, 31 August 2021 09:14