How to Autofit a Word Table in Java

This article demonstrates how to auto fit a Word table to content or to window, as well as how to fix the cloumn widths, by using Spire.Doc for Java.

Autofit to content

import com.spire.doc.*;

public class AutofitToContent {
    public static void main(String[] args) {

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

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the 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");
        table.get(2,0).addParagraph().appendText("T1062");
        table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Autofit column widths to contents
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

        //Save the document
        document.saveToFile("AutofitToContent.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java

Autofit to window

import com.spire.doc.*;

public class AutofitToWindow {

    public static void main(String[] args) {

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

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the 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");
        table.get(2,0).addParagraph().appendText("T1062");
        table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Autofit column widths to window
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        //Save the document
        document.saveToFile("AutofitToWindow.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java

Fix column width

import com.spire.doc.*;

public class FixColumnWidths {
    public static void main(String[] args) {

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

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the 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");
        table.get(2, 0).addParagraph().appendText("T1062");
        table.get(2, 1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Set the column widths
        for (int i = 0; i < table.getRows().getCount(); i++) {

            table.get(i,0).setCellWidth(80f,CellWidthType.Point);
            table.get(i,1).setCellWidth(160f,CellWidthType.Point);
        }

        //Fix the column widths so that the column width does not increases when the content exceeds the width
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);

        //Save the document
        document.saveToFile("FixColumnWidths.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java