News Category

How to set Vertical Alignment for table in Word via Spire.Doc

2015-06-11 08:37:29 Written by  support iceblue
Rate this item
(0 votes)

Set vertical alignment for table can show contents in different positions. There are three options including Top, Bottom, Middle. Default is Middle. This article talk about how to set vertical alignment for tables via Spire.Doc, the following is the detailed steps:

Step 1: Create a new Word document and add a new section.

Document document = new Document();
Section section = document.AddSection();

Step 2: Add a table with 3 columns and 3 rows. You can set showBoder property as true when you creating the table. Merge the first column as one cell.

Table table = section.AddTable(true);
table.ResetCells(3, 3);
table.ApplyVerticalMerge(0, 0, 2);

Step 3: Set the vertical alignment for each cell, default is top. Here we set the first row as Top, second row as Middle, third row as Bottom.

table.Rows[0].Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table.Rows[0].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Top;
table.Rows[0].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Top;
table.Rows[1].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table.Rows[1].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table.Rows[2].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;
table.Rows[2].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;

Step 4: Append data to table.

Paragraph paraPic = table.Rows[0].Cells[0].AddParagraph();
DocPicture pic = paraPic.AppendPicture(Image.FromFile("1.png"));

String[][] data = {
                       new string[] {"","Spire.Office","Spire.DataExport"},
                       new string[] {"","Spire.Doc","Spire.DocViewer"},
                       new string[] {"","Spire.XLS","Spire.PDF"}
                            };

      for (int r = 0; r < 3; r++)
      {
        TableRow dataRow = table.Rows[r];
        dataRow.Height = 50; 
        for (int c = 0; c < 3; c++)
           {
             if (c == 1)
               {
                  Paragraph par = dataRow.Cells[c].AddParagraph();
                  par.AppendText(data[r][c]);
                  dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2;
                }
             if (c == 2)
                {
                  Paragraph par = dataRow.Cells[c].AddParagraph();
                  par.AppendText(data[r][c]);
                  dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2;
                 } 
              }
         }

Step 5: Save and review.

document.SaveToFile(@"result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start(@"result.docx"); 

Result screenshot:

How to set Vertical Alignment for table in Word via Spire.Doc

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace SetVerticalAlignment
{
    class Program
    {

        static void Main(string[] args)
        {

            Document document = new Document();
            Section section = document.AddSection();

            Table table = section.AddTable(true);
            table.ResetCells(3, 3);

            table.ApplyVerticalMerge(0, 0, 2);

            table.Rows[0].Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            table.Rows[0].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Top;
            table.Rows[0].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Top;
            table.Rows[1].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            table.Rows[1].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            table.Rows[2].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;
            table.Rows[2].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;

            Paragraph paraPic = table.Rows[0].Cells[0].AddParagraph();
            DocPicture pic = paraPic.AppendPicture(Image.FromFile("1.png"));

            String[][] data = {
                       new string[] {"","Spire.Office","Spire.DataExport"},
                       new string[] {"","Spire.Doc","Spire.DocViewer"},
                       new string[] {"","Spire.XLS","Spire.PDF"}
                            };

            for (int r = 0; r < 3; r++)
            {
                TableRow dataRow = table.Rows[r];
                dataRow.Height = 50;
                for (int c = 0; c < 3; c++)
                {
                    if (c == 1)
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2;
                    }
                    if (c == 2)
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2;
                    }
                }
            }
            document.SaveToFile(@"result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start(@"result.docx");
        }
    }
}

Additional Info

  • tutorial_title: Set Vertical Alignment for table in Word
Last modified on Friday, 03 September 2021 03:53