This tutorial guides you on how to design tables in Word with Java. It has all the settings for the IDE, a list of steps, and a sample code demonstrating how to apply table styles in Word with Java. You will learn to select a particular table in a Word file and customize its design and style as per your requirements.
Steps to Set Table Styles in Word with Java
- Set the IDE to use Aspose.Words for Java to apply styles to tables
- Load the source Word file with tables into a Document object
- Access the target table from the loaded Word file using the table index
- Create a new table style and set its properties including its name
- Set the newly created style to the selected table
- Save the output Word file with custom table settings
These steps summarize the process for changing MS Word table design with Java. Load the source Word file with tables in it, access the target table using its index, and create a new table style with name and desired settings. Finally, set 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 has demonstrated the process to design a table in Word with Java by changing its style. When we create a style for a table, we can customize conditional style, first-row shading, texture, color, border, and font. You may access existing table styles from the loaded Word file and customize/use it with any table if required.
This article has taught us the process to set table design in MS Word with Java. To auto-fit table refer to the article on Autofit table in Word using Java.