Friday, 31 July 2020 07:17

Align a Table Horizontally in Word in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to align a table in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Table;
import com.spire.doc.documents.RowAlignment;

public class AlignTable {

    public static void main(String[] args) {

        //Create a Document object
        Document doc=new Document();

        //Add a table
        Table table =doc.addSection().addTable(true);

        //Set column width
        table.setColumnWidth(new float[]{100f,150f});

        //Set column number and row number
        table.resetCells(2,2);

        //Add text to cells
        table.get(0,0).addParagraph().appendText("Product Code");
        table.get(0,1).addParagraph().appendText("Description");
        table.get(1,0).addParagraph().appendText("T1052");
        table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl");

        //Align table to center
        table.getTableFormat().setHorizontalAlignment(RowAlignment.Center);

        //Save the document
        doc.saveToFile("AlignTable.docx", FileFormat.Docx_2013);
    }
}

Align a Table Horizontally in Word in Java

Additional Info

  • tutorial_title: