Independentsoft
Home
Purchase
Support
Company
Contact
Word .NET
>
Tutorial
> Table cell alignment
The following example shows you how to set vertical alignment in table cells.
C# example
using System; using Independentsoft.Office.Word; using Independentsoft.Office.Word.Tables; namespace Sample { class Program { static void Main(string[] args) { WordDocument doc = new WordDocument(); Table table = new Table(StandardBorderStyle.SingleLine); table.Width = 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.VerticalAlignment = 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.VerticalAlignment = 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.VerticalAlignment = VerticalAlignmentType.Bottom; Row row = new Row(); row.Add(cellTop); row.Add(cellCenter); row.Add(cellBottom); table.Add(row); doc.Body.Add(table); doc.Save("c:\\test\\output.docx", true); } } }
VB example
Imports System Imports Independentsoft.Office.Word Imports Independentsoft.Office.Word.Tables Module Module1 Sub Main(ByVal args() As String) Dim doc As WordDocument = New WordDocument() Dim table As Table = New Table(StandardBorderStyle.SingleLine) table.Width = New Width(TableWidthUnit.Percent, 100) ' Cell with top alignment Dim runTop As Run = New Run() runTop.AddText("Top aligned") Dim paraTop As Paragraph = New Paragraph() paraTop.Add(runTop) Dim cellTop As Cell = New Cell() cellTop.Add(paraTop) cellTop.VerticalAlignment = VerticalAlignmentType.Top ' Cell with center alignment Dim runCenter As Run = New Run() runCenter.AddText("Center aligned") Dim paraCenter As Paragraph = New Paragraph() paraCenter.Add(runCenter) Dim cellCenter As Cell = New Cell() cellCenter.Add(paraCenter) cellCenter.VerticalAlignment = VerticalAlignmentType.Center ' Cell with bottom alignment Dim runBottom As Run = New Run() runBottom.AddText("Bottom aligned") Dim paraBottom As Paragraph = New Paragraph() paraBottom.Add(runBottom) Dim cellBottom As Cell = New Cell() cellBottom.Add(paraBottom) cellBottom.VerticalAlignment = VerticalAlignmentType.Bottom Dim row As Row = New Row() row.Add(cellTop) row.Add(cellCenter) row.Add(cellBottom) table.Add(row) doc.Body.Add(table) doc.Save("c:\test\output.docx", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*