Adjusting the column widths in a Word table is crucial for making the document look neat and easy to read. Particularly in tables with a lot of text, appropriate column widths can facilitate smoother reading. Word offers two approaches: percentage-based and fixed widths. Setting column widths by percentage can adapt to various screen sizes, keeping content neatly formatted and more pleasant to read. Using fixed widths stabilizes the table structure, precisely aligning each section, which is especially suitable for tables requiring strict alignment of numbers or complex designs. This article will introduce how to set Word table column widths based on percentage or fixed values using Spire.Doc for Java in Java projects.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Set Column Width Based on Percentage in Java
To set column widths in a Word table using percentage values, you first need to define the table's width type as percentage. This can be achieved with Table.SetPreferredWidth(new PreferredWidth(WidthType.Percentage, (short)100)). Then, iterate through each column and set their widths to either the same or different percentage values. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.loadFromFile() method.
- Retrieve the first section of the document using Document.getSections().get(0).
- Get the first table within the section using Section.getTables().get(0).
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to percentage values using the TableRow.getCells().get(index).setCellWidth(value, CellWidthType.Percentage) method, where value is the percentage width you wish to apply.
- Save the changes to the Word document using the Document.saveToFile() method.
- Java
import com.spire.doc.*; public class PercentageColumnWidth { public static void main(String[] args) { // Create a new Document object Document doc = new Document(); // Load the document doc.loadFromFile("Sample.docx"); // Get the first Section of the document Section section = doc.getSections().get(0); // Cast the first Table in the Section to a Table type Table table = section.getTables().get(0); // Create a PreferredWidth object, set the width type to Percentage, and set the width value to 100% PreferredWidth percentageWidth = new PreferredWidth(WidthType.Percentage, (short) 100); // Set the Table's preferred width to the PreferredWidth object created above table.setPreferredWidth(percentageWidth); // Define a variable of type TableRow TableRow tableRow; // Iterate over all rows in the Table for (int i = 0; i < table.getRows().getCount(); i++) { // Get the current row tableRow = table.getRows().get(i); // Set the width of the first cell to 34%, type as Percentage tableRow.getCells().get(0).setCellWidth(34, CellWidthType.Percentage); // Set the width of the second cell to 33%, type as Percentage tableRow.getCells().get(1).setCellWidth(33, CellWidthType.Percentage); // Set the width of the third cell to 33%, type as Percentage tableRow.getCells().get(2).setCellWidth(33, CellWidthType.Percentage); } // Save the modified document, specifying the file format as Docx2016 doc.saveToFile("SetColumnWidthsWithPercentageValues.docx", FileFormat.Docx_2016); // Close the document doc.close(); } }
Set Column Width Based on Fixed Value in Java
When setting column widths in a Word table using fixed values, you first need to set the table to a fixed layout. This is done with Table.getTableFormat().setLayoutType(LayoutType.Fixed), then iterate through each column and set the width to the same or different fixed values as required. Here are the detailed steps:
- Create a Document object.
- Load a document using the Document.loadFromFile() method.
- Retrieve the first section of the document using Document.getSections().get(0).
- Get the first table within the section using Section.getTables().get(0).
- Use a for loop to iterate through all rows in the table.
- Set the column width for cells in different columns to fixed values using the TableRow.getCells().get(index).setCellWidth(value, CellWidthType.Point) method.
- Save the changes to the Word document using the Document.saveToFile() method.
- Java
import com.spire.doc.*; public class FixedColumnWidth { public static void main(String[] args) { // Create a new Document object Document doc = new Document(); // Load the document doc.loadFromFile("Sample.docx"); // Get the first Section of the document Section section = doc.getSections().get(0); // Cast the first Table in the Section to a Table type Table table = section.getTables().get(0); // Set the table layout type to Fixed table.getTableFormat().setLayoutType(LayoutType.Fixed); // Set the table resizing method to not Auto table.getTableFormat().isAutoResized(false); // Get the left margin float leftMargin = section.getPageSetup().getMargins().getLeft(); // Get the right margin float rightMargin = section.getPageSetup().getMargins().getRight(); // Calculate the page width minus left and right margins double pageWidth = section.getPageSetup().getPageSize().getWidth() - leftMargin - rightMargin; // Define a variable of type TableRow TableRow tableRow; // Iterate through all rows in the Table for (int i = 0; i < table.getRows().getCount(); i++) { // Get the current row tableRow = table.getRows().get(i); // Set the first column cell width to 34% of the page width tableRow.getCells().get(0).setCellWidth((float) (pageWidth * 0.34), CellWidthType.Point); // Set the second column cell width to 33% of the page width tableRow.getCells().get(1).setCellWidth((float) (pageWidth * 0.33), CellWidthType.Point); // Set the third column cell width to 33% of the page width tableRow.getCells().get(2).setCellWidth((float) (pageWidth * 0.33), CellWidthType.Point); } // Save the modified document, specifying the file format as Docx2016 doc.saveToFile("SetColumnWidthsWithFixedValues.docx", FileFormat.Docx_2016); // Close the document doc.close(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.