Independentsoft
Any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Table cell alignment
The following example shows you how to set vertical alignment in table cells.
import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.StandardBorderStyle; import com.independentsoft.office.word.VerticalAlignment; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.tables.Cell; import com.independentsoft.office.word.tables.CellProperties; import com.independentsoft.office.word.tables.Row; import com.independentsoft.office.word.tables.RowHeight; import com.independentsoft.office.word.tables.RowProperties; import com.independentsoft.office.word.tables.Table; import com.independentsoft.office.word.tables.TableWidthUnit; import com.independentsoft.office.word.tables.Width; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Table table = new Table(StandardBorderStyle.SINGLE_LINE); table.setWidth(new Width(TableWidthUnit.PERCENT, 100)); // Create cells with different vertical alignments // Cell with top alignment Run runTop = new Run(); runTop.addText("Top aligned"); Paragraph paraTop = new Paragraph(); paraTop.add(runTop); Cell cellTop = new Cell(); cellTop.add(paraTop); CellProperties propsTop = new CellProperties(); propsTop.setVerticalAlignment(VerticalAlignment.TOP); cellTop.setCellProperties(propsTop); // Cell with center alignment Run runCenter = new Run(); runCenter.addText("Center aligned"); Paragraph paraCenter = new Paragraph(); paraCenter.add(runCenter); Cell cellCenter = new Cell(); cellCenter.add(paraCenter); CellProperties propsCenter = new CellProperties(); propsCenter.setVerticalAlignment(VerticalAlignment.CENTER); cellCenter.setCellProperties(propsCenter); // Cell with bottom alignment Run runBottom = new Run(); runBottom.addText("Bottom aligned"); Paragraph paraBottom = new Paragraph(); paraBottom.add(runBottom); Cell cellBottom = new Cell(); cellBottom.add(paraBottom); CellProperties propsBottom = new CellProperties(); propsBottom.setVerticalAlignment(VerticalAlignment.BOTTOM); cellBottom.setCellProperties(propsBottom); // Create row with taller height to show alignment Row row = new Row(); row.add(cellTop); row.add(cellCenter); row.add(cellBottom); RowProperties rowProps = new RowProperties(); rowProps.setHeight(new RowHeight(1440)); // 1 inch height row.setRowProperties(rowProps); table.add(row); doc.getBody().add(table); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*