Independentsoft
Professional software libraries for developers
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.VerticalAlignmentType; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.tables.Cell; import com.independentsoft.office.word.tables.Row; 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)); // 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); cellTop.setVerticalAlignment(VerticalAlignmentType.TOP); // 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); cellCenter.setVerticalAlignment(VerticalAlignmentType.CENTER); // 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); cellBottom.setVerticalAlignment(VerticalAlignmentType.BOTTOM); Row row = new Row(); row.add(cellTop); row.add(cellCenter); row.add(cellBottom); 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*