Align a Table Horizontally in Word in Java

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

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

        //Align table to center

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

Align a Table Horizontally in Word in Java