Close





 
Wednesday, 06 April 2011 09:13

PDF Bookmark C#, VB.NET

The sample demonstrates how to work with bookmark in PDF document.

Download Bookmark.pdf

Published in Interaction
Wednesday, 06 April 2011 05:30

PDF TableLayout C#, VB.NET

The sample demonstrates how to set PDF table layout.

Download TableLayout.pdf

Published in Table
Wednesday, 06 April 2011 05:25

PDF ImageTable C#, VB.NET

The sample demonstrates how to export data and pictures from database into a table in PDF document and set table format.

Download ImageTable.pdf

Published in Table
Wednesday, 06 April 2011 01:56

PDF SimpleTable C#, VB.NET

The sample demonstrates how to export data into a table in PDF document.

Download SimpleTable.pdf

Published in Table
Thursday, 20 January 2011 08:42

How to Create Table in Word Document

Sometimes, in order to give readers a better explanation, we may need to insert or create a table to show data information. Although Word table is not convenient as Excel, it is useful when we need to display data among one paragraph. As a powerful .NET/Silverlight Word component, Spire.Doc enables developers to create table in Word document with C#/VB.NET.

How to Use C#/VB.NET to Create a Word Table via Spire.Doc

In Spire.Doc, you need only four steps to insert or create a table in word document.

Step 1 Creating a table

You may use section.AddTable() method to create a table object in the section. Something should be paid attention, the table class in Spire.Doc is Spire.Doc.Table.
[C#]
            //Create word document.
            Document document = new Document();
            
            //Add a section in the document.
            Section section = document.AddSection();
 
            //Create a table.
            Spire.Doc.Table table = section.AddTable();
          
[Visual Basic]
        'Create word document.
        Dim document As New Document()

        'Add a section in the document.
        Dim section As Section = document.AddSection()


        'Create a table.
        Dim table As Spire.Doc.Table = section.AddTable()
          

Step 2 Setting size of the table

You should use table.ResetCells method to specify how many rows and columns the table has. In this method, you should give two number parameters, the first one is the number of the rows, the other one is the number of the columns.
[C#]
            //Set table size.
            table.ResetCells(data.Length + 1, header.Length);

            //***************** First Row *************************
            TableRow row = table.Rows[0];
            row.IsHeader = true;

            //Set the Height of the header.
            row.Height = 20; //unit: point, 1point = 0.3528 mm

            //Set the Height Type of the header.
            row.HeightType = TableRowHeightType.Exactly;
          
[Visual Basic]
        'Set the Rows and Columns.
        table.ResetCells(data.Length + 1, header.Length)

        '***************** First Row *************************
        Dim row As TableRow = table.Rows(0)
        row.IsHeader = True

        'Set the Height of the header.
        row.Height = 20 'unit: point, 1point = 0.3528 mm

        'Set the Height Type of the header.
        row.HeightType = TableRowHeightType.Exactly
          

Step 3 Filling table header (optinal)

Usually, the first row of the table is the header, and you may operate the header. You can set the height and back color, and then fill data into it.
[C#]
            //Fill the table with header.
            String[] header = { "Name", "Capital", "Continent", "Area", "Population" };

                //Set the Back Color of the data.
                dataRow.RowFormat.BackColor = Color.Empty;
                
                //"c" is the Column of the data.
                for (int c = 0; c < data[r].Length; c++)
                {
                    
                    //Set the Cell Format of the data.
                    dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    
                    //Fill the Cell Format with the data.
                    dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
          
[Visual Basic]
        'Fill the table with header.
        Dim header As String() = {"Name", "Capital", "Continent", "Area", "Population"}

            'Set the Back Color of the data.
            dataRow.RowFormat.BackColor = Color.Empty

            '"c" is the Column of the data.
            For c As Integer = 0 To data(r).Length - 1

                'Set the Cell Format of the data.
                dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle

                'Fill the Cell Format with the data.
                dataRow.Cells(c).AddParagraph().AppendText(data(r)(c))
            Next
          

Step 4 Filling table data

The same with table data, you can set the height, alignment and back color of it, and then fill data into it.
[C#]
            String[][] data =
    {
        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
        new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
        new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
        new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
        new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
        new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
        new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
        new String[]{"United States of America", "Washington", "North America", "9363130",
                        "249200000"},
        new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
        new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
    };
          
[Visual Basic]
        Dim data As String()() = { _
            New String() {"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, _
            New String() {"Bolivia", "La Paz", "South America", "1098575", "7300000"}, _
            New String() {"Brazil", "Brasilia", "South America", "8511196", "150400000"}, _
            New String() {"Canada", "Ottawa", "North America", "9976147", "26500000"}, _
            New String() {"Chile", "Santiago", "South America", "756943", "13200000"}, _
            New String() {"Colombia", "Bagota", "South America", "1138907", "33000000"}, _
            New String() {"Cuba", "Havana", "North America", "114524", "10600000"}, _
            New String() {"Ecuador", "Quito", "South America", "455502", "10600000"}, _
            New String() {"El Salvador", "San Salvador", "North America", "20865", "5300000"}, _
            New String() {"Guyana", "Georgetown", "South America", "214969", "800000"}, _
            New String() {"Jamaica", "Kingston", "North America", "11424", "2500000"}, _
            New String() {"Mexico", "Mexico City", "North America", "1967180", "88600000"}, _
            New String() {"Nicaragua", "Managua", "North America", "139000", "3900000"}, _
            New String() {"Paraguay", "Asuncion", "South America", "406576", "4660000"}, _
            New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _
            New String() {"United States of America", "Washington", "North America", "9363130", _
                            "249200000"}, _
            New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _
            New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _
            }
          
Full Code of C#/VB.NET Create Table in Word document:
[C#]
using Spire.Doc.Documents;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Fields;
using System;


namespace InsertingComments
{

    class Program
    {
        static void Main(string[] args)
        {
            //Create word document.
            Document document = new Document();
            
            //Add a section in the document.
            Section section = document.AddSection();
            
            //Fill the table with header.
            String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
            String[][] data =
    {
        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
        new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
        new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
        new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
        new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
        new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
        new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
        new String[]{"United States of America", "Washington", "North America", "9363130",
                        "249200000"},
        new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
        new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
    };

            //Create a table.
            Spire.Doc.Table table = section.AddTable();

            //Set table size.
            table.ResetCells(data.Length + 1, header.Length);

            //***************** First Row *************************
            TableRow row = table.Rows[0];
            row.IsHeader = true;

            //Set the Height of the header.
            row.Height = 20; //unit: point, 1point = 0.3528 mm

            //Set the Height Type of the header.
            row.HeightType = TableRowHeightType.Exactly;

            //Set the Back Color of the header.
            row.RowFormat.BackColor = Color.Gray;
            for (int i = 0; i < header.Length; i++)
            {

                //Set the Cell Format of the header.
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                Paragraph p = row.Cells[i].AddParagraph();
                
                //Set the Horizon of the header.
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                TextRange txtRange = p.AppendText(header[i]);
                
                //Set the Character Format of the header.
                txtRange.CharacterFormat.Bold = true;
            }

            //***************** Data Row *************************
            for (int r = 0; r < data.Length; r++)
            {
                TableRow dataRow = table.Rows[r + 1];
                
                //Set the Height of the data.
                dataRow.Height = 20;
                
                //Set the Height Type of the data.
                dataRow.HeightType = TableRowHeightType.Exactly;
                
                //Set the Back Color of the data.
                dataRow.RowFormat.BackColor = Color.Empty;
                
                //"c" is the Column of the data.
                for (int c = 0; c < data[r].Length; c++)
                {
                    
                    //Set the Cell Format of the data.
                    dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    
                    //Fill the Cell Format with the data.
                    dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
            }

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launch the file.
            System.Diagnostics.Process.Start("Sample.doc");
        }
    }
}
          
[Visual Basic]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports System
Imports Spire.Doc.Fields

Module Module1

    Sub Main()
        'Create word document.
        Dim document As New Document()

        'Add a section in the document.
        Dim section As Section = document.AddSection()

        'Fill the table with header.
        Dim header As String() = {"Name", "Capital", "Continent", "Area", "Population"}
        Dim data As String()() = { _
            New String() {"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, _
            New String() {"Bolivia", "La Paz", "South America", "1098575", "7300000"}, _
            New String() {"Brazil", "Brasilia", "South America", "8511196", "150400000"}, _
            New String() {"Canada", "Ottawa", "North America", "9976147", "26500000"}, _
            New String() {"Chile", "Santiago", "South America", "756943", "13200000"}, _
            New String() {"Colombia", "Bagota", "South America", "1138907", "33000000"}, _
            New String() {"Cuba", "Havana", "North America", "114524", "10600000"}, _
            New String() {"Ecuador", "Quito", "South America", "455502", "10600000"}, _
            New String() {"El Salvador", "San Salvador", "North America", "20865", "5300000"}, _
            New String() {"Guyana", "Georgetown", "South America", "214969", "800000"}, _
            New String() {"Jamaica", "Kingston", "North America", "11424", "2500000"}, _
            New String() {"Mexico", "Mexico City", "North America", "1967180", "88600000"}, _
            New String() {"Nicaragua", "Managua", "North America", "139000", "3900000"}, _
            New String() {"Paraguay", "Asuncion", "South America", "406576", "4660000"}, _
            New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _
            New String() {"United States of America", "Washington", "North America", "9363130", _
                            "249200000"}, _
            New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _
            New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _
            }

        'Create a table.
        Dim table As Spire.Doc.Table = section.AddTable()

        'Set the Rows and Columns.
        table.ResetCells(data.Length + 1, header.Length)

        '***************** First Row *************************
        Dim row As TableRow = table.Rows(0)
        row.IsHeader = True

        'Set the Height of the header.
        row.Height = 20 'unit: point, 1point = 0.3528 mm

        'Set the Height Type of the header.
        row.HeightType = TableRowHeightType.Exactly

        'Set the Back Color of the header.
        row.RowFormat.BackColor = Color.Gray
        For i As Integer = 0 To header.Length - 1

            'Set the Cell Format of the header.
            row.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle
            Dim p As Paragraph = row.Cells(i).AddParagraph()

            'Set the Horizon of the header.
            p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
            Dim txtRange As TextRange = p.AppendText(header(i))

            'Set the Character Format of the header.
            txtRange.CharacterFormat.Bold = True
        Next

        '***************** Data Row *************************
        For r As Integer = 0 To data.Length - 1
            Dim dataRow As TableRow = table.Rows(r + 1)

            'Set the Height of the data.
            dataRow.Height = 20

            'Set the Height Type of the data.
            dataRow.HeightType = TableRowHeightType.Exactly

            'Set the Back Color of the data.
            dataRow.RowFormat.BackColor = Color.Empty

            '"c" is the Column of the data.
            For c As Integer = 0 To data(r).Length - 1

                'Set the Cell Format of the data.
                dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle

                'Fill the Cell Format with the data.
                dataRow.Cells(c).AddParagraph().AppendText(data(r)(c))
            Next
        Next

        'Save doc file.
        document.SaveToFile("Sample.doc", FileFormat.Doc)

        'Launch the file.
        System.Diagnostics.Process.Start("Sample.doc")
    End Sub
End Module  
          

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. Click to learn more...
Published in Program Guide