We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Formatting Text in a Table

I am trying to create a table in a word document at a specific bookmark. For my test app. I was trying to create a 2x2 table that is centered and have the cells formatted thus:


Cell 1,1: Text is bold and centered with 20% Gray shade
Cell 1,2: Same as Cell 1,1
Cell 2,1: Text is not bold and left aligned with no shading
Cell 2,2: Same as Cell 2,1

The code I have so far is (I’ve bolded where I believe the error is occurring):

DocBuilder.MoveToBookmark(.Name)
Dim table As Tables.Table
table = DocBuilder.StartTable()
DocBuilder.RowFormat.AllowAutoFit = True

Dim cell As Tables.Cell

DocBuilder.RowFormat.Alignment = Tables.RowAlignment.Center
DocBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center
cell = DocBuilder.InsertCell()
cell.CellFormat.Shading.BackgroundPatternColor = TableHeaderShading

DocBuilder.Font.Bold = True
DocBuilder.Font.Name = “Arial Narrow”
DocBuilder.Write(“Cell 1,1”)


DocBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center
cell = DocBuilder.InsertCell
DocBuilder.Write(“Cell 1,2”)

DocBuilder.EndRow()

DocBuilder.Font.Bold = False

DocBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left
cell = DocBuilder.InsertCell()
cell.CellFormat.Shading.BackgroundPatternColor = Color.White
DocBuilder.Write(“Cell 2,1”)

cell = DocBuilder.InsertCell
DocBuilder.Write(“Cell 2,2”)

DocBuilder.EndRow()
DocBuilder.EndTable()

It inserts the table fine, however the cells are not formatted the way I wanted them. The cells are formatted like this:

Cell 1,1: Text is bold and centered with 20% Gray shade
Cell 1,2: Text is bold and left aligned with 20% Gray shade
Cell 2,1: Text is not bold and left aligned with no shading
Cell 2,2: Same as Cell 2,1

How can I get Cell 1,2 aligned center? As a side note, in the final app. there will be multiple columns and some will need to be centered and other left aligned.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I modified your code:

Dim doc As Document = New Document()

Dim DocBuilder As DocumentBuilder = New DocumentBuilder(doc)

Dim table As Tables.Table = DocBuilder.StartTable()

DocBuilder.RowFormat.AllowAutoFit = True

DocBuilder.RowFormat.Alignment = Tables.RowAlignment.Center

DocBuilder.InsertCell()

DocBuilder.CellFormat.Shading.BackgroundPatternColor = Color.LightGray

DocBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center

DocBuilder.Font.Bold = True

DocBuilder.Font.Name = "Arial Narrow"

DocBuilder.Write("Cell 1,1")

DocBuilder.InsertCell()

DocBuilder.Write("Cell 1,2")

DocBuilder.EndRow()

DocBuilder.InsertCell()

DocBuilder.CellFormat.Shading.BackgroundPatternColor = Color.Empty

DocBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left

DocBuilder.Font.Bold = False

DocBuilder.Write("Cell 2,1")

DocBuilder.InsertCell()

DocBuilder.Write("Cell 2,2")

DocBuilder.EndRow()

DocBuilder.EndTable()

doc.Save("C:\Temp\out.doc")

Hope this helps.

Best regards.