Design Tables in Word with Java

This tutorial guides you on how to design tables in Word with Java. It includes all the necessary IDE settings, a list of steps, and a sample code demonstrating how to apply table styles in Word with Java. You will learn how to select a specific table in a Word file and customise its design and style according to your requirements.

Steps to Set Table Styles in Word with Java

  1. Set up the IDE to use Aspose.Words for Java to apply styles to tables.
  2. Load the source Word file containing tables into a Document object.
  3. Access the target table from the loaded Word file using the table index.
  4. Create a new table style and set its properties, including its name.
  5. Apply the newly created style to the selected table.
  6. Save the output Word file with custom table settings.

These steps summarise the process for changing MS Word table design with Java. Load the source Word file containing tables, access the target table using its index, and create a new table style with a name and desired settings. Finally, apply this style to the selected table and save the output Word file.

Code to Apply Table Design for Word with Java

import com.aspose.words.*;
import java.awt.*;
public class Main {
public static void main(String[] args) throws Exception {//Table Styles
License pdfLicense = new License();
pdfLicense.setLicense("license.lic");
Document document = new Document("Table.docx");// DOCX with tables
Table table = (Table) document.getChild(NodeType.TABLE, 0, true);
TableStyle customStyle = (TableStyle) document.getStyles().add(StyleType.TABLE, "CustomTableStyle");
customStyle.getConditionalStyles().getFirstRow().getShading().setBackgroundPatternColor(Color.GREEN);
customStyle.getConditionalStyles().getFirstRow().getShading().setTexture(TextureIndex.TEXTURE_NONE);
customStyle.getFont().setColor(Color.RED);
customStyle.getBorders().setLineStyle(LineStyle.DOUBLE);
customStyle.getBorders().setLineWidth(2.0);
customStyle.getFont().setShadow(true);
customStyle.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
table.setStyle(customStyle);
document.save("FormattedTable.docx");//With new table style
}
}

This code demonstrates the process to design a table in Word with Java by changing its style. When we create a style for a table, we can customise conditional styles, first-row shading, texture, colour, border, and font. You may access existing table styles from the loaded Word file and customise or use them with any table if required.

This article has taught us the process to set table design in MS Word with Java. To auto-fit tables, refer to the article on Autofit table in Word using Java.

 Українська