How apply formatting for table cells


Complete code

using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            TableCellFormatting();
        }
        /// <summary>
        /// How apply formatting for table cells.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/tablecell-format.php
        /// </remarks>
        static void TableCellFormatting()
        {
            string filePath = @"TableCellFormatting.docx";

            // Let's a new create document.
            DocumentCore dc = new DocumentCore();

            // Add new table.
            Table table = new Table(dc);
            double width = LengthUnitConverter.Convert(100, LengthUnit.Millimeter, LengthUnit.Point);
            table.TableFormat.PreferredWidth = new TableWidth(width, TableWidthUnit.Point);
            table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Inside | MultipleBorderTypes.Outside, 
                BorderStyle.Single, Color.Black, 1);
            dc.Sections.Add(new Section(dc, table));

            TableRow row = new TableRow(dc);
            row.RowFormat.Height = new TableRowHeight(50, HeightRule.Exact);
            table.Rows.Add(row);

            // Create a cells with formatting: borders, background color, vertical alignment and text direction.
            TableCell cell = new TableCell(dc, new Paragraph(dc, "Cell 1"));
            cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Blue, 1);
            cell.CellFormat.BackgroundColor = Color.Yellow;
            row.Cells.Add(cell);

            row.Cells.Add(new TableCell(dc, new Paragraph(dc, "Cell 2"))
            {
                CellFormat = new TableCellFormat()
                {
                    VerticalAlignment = VerticalAlignment.Center
                }
            });

            row.Cells.Add(new TableCell(dc, new Paragraph(dc, "Cell 3"))
            {
                CellFormat = new TableCellFormat()
                {
                    TextDirection = TextDirection.BottomToTop
                }
            });

            // Save our document.
            dc.Save(filePath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables
Imports System.Linq

Namespace Example
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            TableCellFormatting()
        End Sub
        ''' Get your free 30-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' How apply formatting for table cells.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/tablecell-format.php
        ''' </remarks>
        Private Shared Sub TableCellFormatting()
            Dim filePath As String = "TableCellFormatting.docx"

            ' Let's a new create document.
            Dim dc As New DocumentCore()

            ' Add new table.
            Dim table As New Table(dc)
            Dim width As Double = LengthUnitConverter.Convert(100, LengthUnit.Millimeter, LengthUnit.Point)
            table.TableFormat.PreferredWidth = New TableWidth(width, TableWidthUnit.Point)
            table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Inside Or MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 1)
            dc.Sections.Add(New Section(dc, table))

            Dim row As New TableRow(dc)
            row.RowFormat.Height = New TableRowHeight(50, HeightRule.Exact)
            table.Rows.Add(row)

            ' Create a cells with formatting: borders, background color, vertical alignment and text direction.
            Dim cell As New TableCell(dc, New Paragraph(dc, "Cell 1"))
            cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Blue, 1)
            cell.CellFormat.BackgroundColor = Color.Yellow
            row.Cells.Add(cell)

            row.Cells.Add(New TableCell(dc, New Paragraph(dc, "Cell 2")) With {
                .CellFormat = New TableCellFormat() With {.VerticalAlignment = VerticalAlignment.Center}
            })

            row.Cells.Add(New TableCell(dc, New Paragraph(dc, "Cell 3")) With {
                .CellFormat = New TableCellFormat() With {.TextDirection = TextDirection.BottomToTop}
            })

            ' Save our document.
            dc.Save(filePath)

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.