Extracting tables from PDFs and converting them into Excel format offers numerous advantages, such as enabling data manipulation, analysis, and visualization in a more versatile and familiar environment. This task is particularly valuable for researchers, analysts, and professionals dealing with large amounts of tabular data. In this article, you will learn how to extract tables from PDF to Excel in C# and VB.NET using Spire.Office for .NET.

Install Spire.Office for .NET

To begin with, you need to add the Spire.Pdf.dll and the Spire.Xls.dll included in the Spire.Office for.NET package as references in your .NET project. Spire.PDF is responsible for extracting data from PDF tables, and Spire.XLS is responsible for creating an Excel document based on the data obtained from PDF.

The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Office

Extract Tables from PDF to Excel in C#, VB.NET

Spire.PDF for .NET offers the PdfTableExtractor.ExtractTable(int pageIndex) method to extract tables from a specific page of a searchable PDF document. The text of a specific cell can be accessed using PdfTable.GetText(int rowIndex, int columnIndex) method. This value can be then written to a worksheet through Worksheet.Range[int row, int column].Value property offered by Spire.XLS for .NET.  The following are the detailed steps.

  • Create an instance of PdfDocument class.
  • Load the sample PDF document using PdfDocument.LoadFromFile() method.
  • Extract tables from a specific page using PdfTableExtractor.ExtractTable() method.
  • Get text of a certain table cell using PdfTable.GetText() method.
  • Create a Workbook object.
  • Write the cell data obtained from PDF into a worksheet through Worksheet.Range.Value property.
  • Save the workbook to an Excel file using Workbook.SaveTofile() method.

The following code example extracts all tables from a PDF document and writes each of them into an individual worksheet within a workbook.

  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Utilities;
using Spire.Xls;

namespace ExtractTablesToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load the sample PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\table.pdf");

            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Clear default worksheets
            workbook.Worksheets.Clear();

            //Initialize an instance of PdfTableExtractor class
            PdfTableExtractor extractor = new PdfTableExtractor(doc);

            //Declare a PdfTable array 
            PdfTable[] tableList = null;

            int sheetNumber = 1;

            //Loop through the pages 
            for (int pageIndex = 0; pageIndex < doc.Pages.Count; pageIndex++)
            {
                //Extract tables from a specific page
                tableList = extractor.ExtractTable(pageIndex);

                //Determine if the table list is null
                if (tableList != null && tableList.Length > 0)
                {
                    //Loop through the table in the list
                    foreach (PdfTable table in tableList)
                    {
                        //Add a worksheet
                        Worksheet sheet = workbook.Worksheets.Add(String.Format("sheet{0}", sheetNumber));

                        //Get row number and column number of a certain table
                        int row = table.GetRowCount();
                        int column = table.GetColumnCount();

                        //Loop though the row and colunm 
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < column; j++)
                            {
                                //Get text from the specific cell
                                string text = table.GetText(i, j);

                                //Write text to a specified cell
                                sheet.Range[i + 1, j + 1].Value = text;
                            }
              
                        }
                        sheetNumber++;
                    }
                }
            }

            //Save to file
            workbook.SaveToFile("ToExcel.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Extract Tables from PDF to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Table
Wednesday, 20 October 2021 02:03

C#/VB.NET: Extract Tables from PDF

PDF is one of the most popular document formats for sharing and writing data. You may encounter the situation where you need to extract data from PDF documents, especially the data in tables. For example, there is useful information stored in the tables of your PDF invoices and you want to extract the data for further analysis or calculation. This article demonstrates how to extract data out of PDF tables and save it in a TXT file by using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

  • Package Manager
PM> Install-Package Spire.PDF 

Extract Data from PDF Tables

The following are the main steps to extract tables from a PDF document.

  • Create an instance of PdfDocument class.
  • Load the sample PDF document using PdfDocument.LoadFromFile() method.
  • Extract tables from a specific page using PdfTableExtractor.ExtractTable(int pageIndex) method.
  • Get text of a certain table cell using PdfTable.GetText(int rowIndex, int columnIndex) method.
  • Save the extracted data in a .txt file.
  • C#
  • VB.NET
using System.IO;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Utilities;

namespace ExtractPdfTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load the sample PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\table.pdf");

            //Create a StringBuilder object
            StringBuilder builder = new StringBuilder();

            //Initialize an instance of PdfTableExtractor class
            PdfTableExtractor extractor = new PdfTableExtractor(doc);

            //Declare a PdfTable array 
            PdfTable[] tableList = null;

            //Loop through the pages 
            for (int pageIndex = 0; pageIndex < doc.Pages.Count; pageIndex++)
            {
                //Extract tables from a specific page
                tableList = extractor.ExtractTable(pageIndex);

                //Determine if the table list is null
                if (tableList != null && tableList.Length > 0)
                {
                    //Loop through the table in the list
                    foreach (PdfTable table in tableList)
                    {
                        //Get row number and column number of a certain table
                        int row = table.GetRowCount();
                        int column = table.GetColumnCount();

                        //Loop though the row and colunm 
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < column; j++)
                            {
                                //Get text from the specific cell
                                string text = table.GetText(i, j);

                                //Add text to the string builder
                                builder.Append(text + " ");
                            }
                            builder.Append("\r\n");
                        }
                    }
                }
            }

            //Write to a .txt file
            File.WriteAllText("Table.txt", builder.ToString());
        }
    }
}
Imports System.IO
Imports System.Text
Imports Spire.Pdf
Imports Spire.Pdf.Utilities
 
Namespace ExtractPdfTable
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a PdfDocument object
            Dim doc As PdfDocument =  New PdfDocument() 
 
            'Load the sample PDF file
            doc.LoadFromFile("C:\Users\Administrator\Desktop\table.pdf")
 
            'Create a StringBuilder object
            Dim builder As StringBuilder =  New StringBuilder() 
 
            'Initialize an instance of PdfTableExtractor class
            Dim extractor As PdfTableExtractor =  New PdfTableExtractor(doc) 
 
            'Declare a PdfTable array 
            Dim tableList() As PdfTable =  Nothing 
 
            'Loop through the pages 
            Dim pageIndex As Integer
            For  pageIndex = 0 To  doc.Pages.Count- 1  Step  pageIndex + 1
                'Extract tables from a specific page
                tableList = extractor.ExtractTable(pageIndex)
 
                'Determine if the table list is null
                If tableList <> Nothing And tableList.Length > 0 Then
                    'Loop through the table in the list
                    Dim table As PdfTable
                    For Each table In tableList
                        'Get row number and column number of a certain table
                        Dim row As Integer =  table.GetRowCount() 
                        Dim column As Integer =  table.GetColumnCount() 
 
                        'Loop though the row and colunm 
                        Dim i As Integer
                        For  i = 0 To  row- 1  Step  i + 1
                            Dim j As Integer
                            For  j = 0 To  column- 1  Step  j + 1
                                'Get text from the specific cell
                                Dim text As String =  table.GetText(i,j) 
 
                                'Add text to the string builder
                                builder.Append(text + " ")
                            Next
                            builder.Append("\r\n")
                        Next
                    Next
                End If
            Next
 
            'Write to a .txt file
            File.WriteAllText("Table.txt", builder.ToString())
        End Sub
    End Class
End Namespace

C#/VB.NET: Extract Tables from PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Table

This article will demonstrate how to repeat the table’s header row in C#/VB.NET by using Spire.PDF for .NET.

C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;

namespace PDFGrid
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Add a page to pdf
            PdfPageBase page = doc.Pages.Add();

            //Create a PdfGrid object
            PdfGrid grid = new PdfGrid();

            //Set the cell padding of the grid
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

            //Set the Columns of the grid
            grid.Columns.Add(3);

            //Set the header rows and define the data
            PdfGridRow[] pdfGridRows = grid.Headers.Add(2);
            for (int i = 0; i < pdfGridRows.Length; i++)
            {
                pdfGridRows[i].Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Regular), true);
                pdfGridRows[i].Cells[0].Value = "Vendor Name";
                pdfGridRows[i].Cells[1].Value = "Address";
                pdfGridRows[i].Cells[2].Value = "City";
            }

            //Repeat the table header rows if the grid exceed one page
            grid.RepeatHeader = true;


            for (int i = 0; i < 60; i++)
            {
                PdfGridRow row = grid.Rows.Add();

                //Add the data to the table
                for (int j = 0; j < grid.Columns.Count; j++)
                {
                    row.Cells[j].Value = "(Row " + i + ", column " + j + ")";
                }
            }

            //draw grid on the pdf page
            PdfLayoutResult pdfLayoutResult = grid.Draw(page, new PointF(0, 20));
            float y = pdfLayoutResult.Bounds.Y + pdfLayoutResult.Bounds.Height;
            PdfPageBase currentPage = pdfLayoutResult.Page;


            //Save the doucment to file
            doc.SaveToFile("PDFGrid.pdf");

        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System.Drawing

Namespace PDFGrid
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Create a pdf document
            Dim doc As PdfDocument = New PdfDocument
            'Add a page to pdf
            Dim page As PdfPageBase = doc.Pages.Add
            'Create a PdfGrid object
            Dim grid As PdfGrid = New PdfGrid
            'Set the cell padding of the grid
            grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)
            'Set the Columns of the grid
            grid.Columns.Add(3)
            'Set the header rows and define the data
            Dim pdfGridRows() As PdfGridRow = grid.Headers.Add(2)
            Dim i As Integer = 0
            Do While (i < pdfGridRows.Length)
                pdfGridRows(i).Style.Font = New PdfTrueTypeFont(New Font("Arial", 11!, FontStyle.Regular), true)
                pdfGridRows(i).Cells(0).Value = "Vendor Name"
                pdfGridRows(i).Cells(1).Value = "Address"
                pdfGridRows(i).Cells(2).Value = "City"
                i = (i + 1)
            Loop
            
            'Repeat the table header rows if the grid exceed one page
            grid.RepeatHeader = true
            Dim i As Integer = 0
            Do While (i < 60)
                Dim row As PdfGridRow = grid.Rows.Add
                'Add the data to the table
                Dim j As Integer = 0
                Do While (j < grid.Columns.Count)
                    row.Cells(j).Value = ("(Row "  _
                                + (i + (", column "  _
                                + (j + ")"))))
                    j = (j + 1)
                Loop
                
                i = (i + 1)
            Loop
            
            'draw grid on the pdf page
            Dim pdfLayoutResult As PdfLayoutResult = grid.Draw(page, New PointF(0, 20))
            Dim y As Single = (pdfLayoutResult.Bounds.Y + pdfLayoutResult.Bounds.Height)
            Dim currentPage As PdfPageBase = pdfLayoutResult.Page
            'Save the doucment to file
            doc.SaveToFile("PDFGrid.pdf")
        End Sub
    End Class
End Namespace

Effective screenshot of repeating the table's header row:

Repeat the header rows in PDF table in C#, VB.NET

Published in Table

Spire.PDF supports to delete rows or columns from a PDF grid before drawing it onto a PDF page. This article demonstrates the detail steps of how to delete a row and a column from a PDF grid using Spire.PDF.

Detail steps:

Step 1: Create a PDF document and add a page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a PDF grid.

PdfGrid grid = new PdfGrid();
//Set cell padding
grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

Step 3: Add 3 rows and 4 columns to the grid.

PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();

grid.Columns.Add(4);

Step 4: Set columns' width.

foreach (PdfGridColumn column in grid.Columns)
{
    column.Width = 60f;
}

Step 5: Add values to grid cells.

for (int i = 0; i < grid.Columns.Count; i++)
{
    row1.Cells[i].Value = String.Format("column{0}", i + 1);
    row2.Cells[i].Value = "a";
    row3.Cells[i].Value = "b";
}

Step 6: Delete the second row and the second column from the grid.

grid.Rows.RemoveAt(1);

grid.Columns.RemoveAt(1);

Step 7: Draw the grid onto the page and save the file.

grid.Draw(page, new PointF(0, 20));

doc.SaveToFile("Output.pdf");

Output:

Delete Rows and Columns from a PDF Grid in C#

Full code:

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Grid;

namespace Delete_Row_and_Column_from_PDFGrid
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Create a PDF grid
            PdfGrid grid = new PdfGrid();

            //Set cell padding
            grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

            //Add 3 rows and 4 columns to the grid
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();
            PdfGridRow row3 = grid.Rows.Add();
            grid.Columns.Add(4);

            //Set columns’ width
            foreach (PdfGridColumn column in grid.Columns)
            {
                column.Width = 60f;
            }

            //Add values to grid cells
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                row1.Cells[i].Value = String.Format("column{0}", i + 1);
                row2.Cells[i].Value = "a";
                row3.Cells[i].Value = "b";
            }

            //Delete the second row
            grid.Rows.RemoveAt(1);

            //Delete the second column
            grid.Columns.RemoveAt(1);

            //Draw the grid to the page
            grid.Draw(page, new PointF(0, 20));

            //Save the file
            doc.SaveToFile("Output.pdf");
        }
    }
}
Published in Table

Spire.PDF supports to embed image and grid into a grid cell. We've introduced how to embed an image into a grid cell in the article - How to Insert an Image to PDF Grid Cell in C#, this article is going to show you how to embed a grid into a grid cell in PDF using Spire.PDF.

Detail steps:

Step 1: Create a PDF document and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Create a PDF grid.

//Create a grid
PdfGrid grid = new PdfGrid();

//Add two rows 
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();

//Set the Top and Bottom cell padding
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;

//Add two columns
grid.Columns.Add(2);

//Set columns' width 
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 120f;

Step 3: Create another PDF grid to embed.

//Create another grid
PdfGrid embedGrid = new PdfGrid();

//Add a row
PdfGridRow newRow = embedGrid.Rows.Add();

//Add two columns
embedGrid.Columns.Add(2);

//Set columns' width            
embedGrid.Columns[0].Width = 50f;
embedGrid.Columns[1].Width = 50f;

Step 4: Assign values to the cells of the embed grid and the grid, and set formatting.

//Create a PDFStringFormat instance
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

//Assign values to the cells of the embedGrid and set formatting
newRow.Cells[0].Value = "Spire.Doc";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = "Spire.PDF";
newRow.Cells[1].StringFormat = stringFormat;

//Assign values to the cells of the grid and set formatting
row1.Cells[0].Value = "Customer's Name";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
row1.Cells[1].Value = "Product(s)";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.Gray;
row2.Cells[0].Value = "Michael";
row2.Cells[0].StringFormat = stringFormat;
//Assign the embedGrid to a cell of the grid
row2.Cells[1].Value = embedGrid;
row2.Cells[1].StringFormat = stringFormat;

Step 5: Draw the grid to the new added page.

grid.Draw(page, new PointF(0f, 50f));

Step 6: Save the document.

pdf.SaveToFile("EmbedGridInCell.pdf");

Screenshot:

Embed a Grid into a Grid Cell in PDF in C#

Full code:

using Spire.Pdf.Grid;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace Embed_a_Grid_in_a_Grid_Cell_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document
            PdfDocument pdf = new PdfDocument();

            //Add a page
            PdfPageBase page = pdf.Pages.Add();

            //Create a pdf grid
            PdfGrid grid = new PdfGrid();

            //Add two rows
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();

            //Set Top and Bottom cell padding of the grid
            grid.Style.CellPadding.Top = 5f;
            grid.Style.CellPadding.Bottom = 5f;

            //Add two columns
            grid.Columns.Add(2);

            //Set the columns’ width
            grid.Columns[0].Width = 120f;
            grid.Columns[1].Width = 120f;
            

            //Create another grid to embed
            PdfGrid embedGrid = new PdfGrid();

            //Add a row
            PdfGridRow newRow = embedGrid.Rows.Add();

            //Add two columns
            embedGrid.Columns.Add(2);
            
            //Set the columns’ width
            embedGrid.Columns[0].Width = 50f;
            embedGrid.Columns[1].Width = 50f;

            //Create a PDFStringFormat instance
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            //Assign values to the cells of the embedGrid and set formatting
            newRow.Cells[0].Value = "Spire.Doc";
            newRow.Cells[0].StringFormat = stringFormat;
            newRow.Cells[1].Value = "Spire.PDF";
            newRow.Cells[1].StringFormat = stringFormat;

            //Assign values to the cells of the grid and set formatting
            row1.Cells[0].Value = "Customer's Name";
            row1.Cells[0].StringFormat = stringFormat;
            row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
            row1.Cells[1].Value = "Product(s)";
            row1.Cells[1].StringFormat = stringFormat;
            row1.Cells[1].Style.BackgroundBrush = PdfBrushes.Gray;
            row2.Cells[0].Value = "Michael";
            row2.Cells[0].StringFormat = stringFormat;
            //Assign the embedGrid to the cell of the grid
            row2.Cells[1].Value = embedGrid;
            row2.Cells[1].StringFormat = stringFormat;            
            
            //Draw the grid to the new added page
            grid.Draw(page, new PointF(0f, 50f));

            //Save the pdf document
            pdf.SaveToFile("EmbedGridInCell.pdf");
        }
    }
}
Published in Table
Thursday, 04 May 2017 09:39

Set Row Height in PDF Table in C#

When creating a PDF table using PdfTable, there is no direct API available in this class that allows to change the row height of the table. However, it is possible to change the row height through BeginRowLayout event.

Step 1: Create a new PDF document.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Initialize an instance of PdfTable class.

PdfTable table = new PdfTable();

Step 3: Create a DataTable.

DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("First Name");
dataTable.Columns.Add("Last Name");
dataTable.Columns.Add("Job Id");
dataTable.Rows.Add(new string[] { "102", "Lexa", "De Haan","AD_VP" });
dataTable.Rows.Add(new string[] { "103", "Alexander", "Hunoldsssss","IT_PROG" });
dataTable.Rows.Add(new string[] { "104", "Bruce", "Ernst", "IT_PROG" });
dataTable.Rows.Add(new string[] { "105", "John", "Chen", "FI_ACCOUNT" })

Step 4: Assign data table as data source to the table.

table.DataSource = dataTable;
table.Style.ShowHeader = true;

Step 5: Subscribe to event.

table.BeginRowLayout += Table_BeginRowLayout;

Step 6: Draw the table on the page and save the document.

table.Draw(page, new RectangleF(0,20,300,90)); 
doc.SaveToFile("Output.pdf");

Step 7: Set the row height in the BeginRowLayout event.

private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    args.MinimalHeight = 15f;
}

Output:

How to Set Row Height in PDF Table in C#

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
using System.Data;
using System.Drawing;


namespace SetRowHeight
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            PdfTable table = new PdfTable();
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("First Name");
            dataTable.Columns.Add("Last Name");
            dataTable.Columns.Add("Job Id");
            dataTable.Rows.Add(new string[] { "102", "Lexa", "De Haan", "AD_VP" });
            dataTable.Rows.Add(new string[] { "103", "Alexander", "Hunoldsssss", "IT_PROG" });
            dataTable.Rows.Add(new string[] { "104", "Bruce", "Ernst", "IT_PROG" });
            dataTable.Rows.Add(new string[] { "105", "John", "Chen", "FI_ACCOUNT" });
            table.DataSource = dataTable;
            table.Style.ShowHeader = true;

            foreach (PdfColumn col in table.Columns)
            {
                col.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            }
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray;

            table.BeginRowLayout += Table_BeginRowLayout;
            table.Draw(page, new RectangleF(0, 20, 300, 90));
            doc.SaveToFile("Output.pdf");


            System.Diagnostics.Process.Start("Output.pdf");

        }
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            args.MinimalHeight = 15f;
        }
    }
}
Published in Table

In order to format a table in PDF, Spire.PDF provides a PdfTableStyle class that represents parameters of PDF light table and a PdfCellStyle class that represents information about cell style. This article will introduce how to set the border color of a table including table border and cell border by using the two classes mentioned above.

Code Snippets:

Step 1: Define a multidimensional array of string.

string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
  new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
  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"}
};

Step 2: Initialize a new instance of PdfDocument class and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 3: Initialize a new instance of PdfTbale class, filling the table with the data predefined.

PdfTable table = new PdfTable();
table.DataSource = dataSource;

Step 4: Initialize an instance of PdfTableStyle class, and set the color of table border as Blue, then apply the style to PdfTable.Style.

PdfTableStyle style = new PdfTableStyle();
style.BorderPen = new PdfPen(Color.Blue, 1f);
table.Style = style;

Step 5: The syntax to set style of cell border is much different from setting table border style, since PdfTable class doesn't contain a property of CellStyle. Instead, CellStyle is a property included in BeginRowLayoutEventArgs class, which is an argument of StratRowLayout event. Therefore we customize a method as below to set the color of cell border.

public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    PdfCellStyle cellStyle = new PdfCellStyle();
    cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
    args.CellStyle = cellStyle;
}

In the Main method, "table_BeginRowLayout" must be added to BeginRowLayout event to ensure the custom method will be invoked when the event occurs.

table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

Step 6: Draw the table on PDF page and save to file.

table.Draw(page, new PointF(0,40));
pdf.SaveToFile(@"SetBorderColor.pdf"); 

Output:

 

How to Set the Border Color of Table in PDF in C#

 

Full Code:

using Spire.Pdf;
using Spire.Pdf.Tables;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace SetBorderColorOfTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //input data
            string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
              new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
              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"}
            };

            //initialize an instance of PdfDocument
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //initialize an instance of PdfTable
            PdfTable table = new PdfTable();            
            table.DataSource = dataSource;   
                     
            //set the color of table border
            PdfTableStyle style = new PdfTableStyle();
            style.BorderPen = new PdfPen(Color.Blue, 1f);
            table.Style = style;

            //add custom method to BeginRowLayout event
            table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

            //draw table on PDF and save file
            table.Draw(page, new PointF(0,40));
            pdf.SaveToFile(@"SetBorderColor.pdf");
            System.Diagnostics.Process.Start(@"SetBorderColor.pdf");
        }
        //customize a method to set color of cell border
        public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            PdfCellStyle cellStyle = new PdfCellStyle();
            cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
            args.CellStyle = cellStyle;
        }
    }
}
Published in Table
Wednesday, 27 May 2015 03:08

Merge cells in grid via Spire.PDF

Grid also offers more flexible resizing behavior than Table and lighter weight then a Table. It derives from the Panel element which is best used inside of forms. This article is mainly talk about how to merge cells in grid via Spire.PDF.

Prerequisite:

  • Download Spire.PDF for .NET (or Spire.Office for .NET) and install it on your system.
  • Add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".
  • Check the codes as below in C#:

Here are the detail steps:

Step 1: Create a new PDF document and add a new page.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Add a new grid with 5 columns and 2 rows, and set height and width.

PdfGrid grid = new PdfGrid();
grid.Columns.Add(5);
float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
for (int j = 0; j < grid.Columns.Count;j++)
{
  grid.Columns[j].Width = width * 0.20f;
}

 PdfGridRow row0 = grid.Rows.Add();
 PdfGridRow row1 = grid.Rows.Add();
 float height = 20.0f;
 for (int i = 0; i < grid.Rows.Count; i++)
 {
      grid.Rows[i].Height = height;
 }

Step 3: Draw the current grid.

grid.Draw(page, new PointF(0, 50));

Step 4: Set the font of the grid and fill some content in the cells. Use RowSpan and ColumnSpan to merge certain cells vertically and horizontally.

row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);
row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true);

row0.Cells[0].Value = "Corporation";
row0.Cells[0].RowSpan = 2;

row0.Cells[1].Value = "B&K Undersea Photo";
row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row0.Cells[1].ColumnSpan = 3;

row0.Cells[4].Value = "World";
row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen;

row1.Cells[1].Value = "Diving International Unlimited";
row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row1.Cells[1].ColumnSpan = 4;

Step 5: Draw the new grid and save the file, the review it.

grid.Draw(page, new PointF(0, 100));

doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Following is the result screenshot:

How to merge cells in grid via Spire.PDF

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;


namespace MergeCells
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            PdfGrid grid = new PdfGrid();
            grid.Columns.Add(5);
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
            for (int j = 0; j < grid.Columns.Count; j++)
            {
                grid.Columns[j].Width = width * 0.20f;
            }

            PdfGridRow row0 = grid.Rows.Add();
            PdfGridRow row1 = grid.Rows.Add();
            float height = 20.0f;
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Height = height;
            }

            grid.Draw(page, new PointF(0, 50));

            row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);
            row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true);

            row0.Cells[0].Value = "Corporation";
            row0.Cells[0].RowSpan = 2;

            row0.Cells[1].Value = "B&K Undersea Photo";
            row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[1].ColumnSpan = 3;

            row0.Cells[4].Value = "World";
            row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
            row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen;

            row1.Cells[1].Value = "Diving International Unlimited";
            row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row1.Cells[1].ColumnSpan = 4;

            grid.Draw(page, new PointF(0, 100));

            doc.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}
Published in Table
Thursday, 05 February 2015 01:51

Set the Color of Grid Border in PDF in C#

The PdfBorders class in Spire.PDF mainly contains three properties - DashStyle, Color and Width. By setting the value of these properties, you're able to change the appearance of grid border. In this article, I'll take color as an example to explain how to design gird border with Spire.PDF in C#.

As is shown in the following screenshot, Spire.PDF enables programmers to add color to PDF grid border as well as making the border as invisible.

Change the Color of Grid Border in PDF

Change the Color of Grid Border in PDF

Code Snippets:

Step 1: Create a new PDF document.

PdfDocument document = new PdfDocument();
PdfPageBase page=document.Pages.Add();

Step 2: Create a string array, create a 4 rows x 3 columns grid according to the length of string array. Set column width and row height.

String[] data
    = {
         "VendorName;Address;City",
         "Cacor Corporation;161 Southfield Rd;Southfield",
         "Underwater;50 N 3rd Street;Indianapolis",
         "J.W.  Luscher Mfg.;65 Addams Street;Berkely"
       };
 PdfGrid grid = new PdfGrid();
 for (int r = 0; r < data.Length; r++)
 {
     PdfGridRow row = grid.Rows.Add();
 }
 grid.Columns.Add(3);
 float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
 grid.Columns[0].Width = width*0.15f;
 grid.Columns[1].Width = width * 0.15f;
 grid.Columns[2].Width = width * 0.15f;
 float height=page.Canvas.ClientSize.Height-(grid.Rows.Count+1);
 grid.Rows[0].Height = 12.5f;
 grid.Rows[1].Height = 12.5f;
 grid.Rows[2].Height = 12.5f;
 grid.Rows[3].Height = 12.5f;

Step 3: Insert data into grid.

for (int r = 0; r < data.Length; r++)
 {
     String[] rowData = data[r].Split(';');
     for (int c = 0; c < rowData.Length; c++)
     {
         grid.Rows[r].Cells[c].Value = rowData[c];
     }
 }

Step 4: Initialize a new instance of PdfBorders and set color property as LightBlue or Transparent. Apply border style to PDF grid.

PdfBorders border = new PdfBorders();
border.All = new PdfPen(Color.LightBlue);

foreach (PdfGridRow pgr in grid.Rows)
{
    foreach (PdfGridCell pgc in pgr.Cells)
    {
        pgc.Style.Borders = border;
    }
}

Step 5: Draw the grid on PDF and save the file.

PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
document.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Entire Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Drawing;


namespace ChangeColorofGridBorder
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            String[] data
               = {
        "VendorName;Address;City",
        "Cacor Corporation;161 Southfield Rd;Southfield",
        "Underwater;50 N 3rd Street;Indianapolis",
        "J.W.  Luscher Mfg.;65 Addams Street;Berkely"
      };
            PdfGrid grid = new PdfGrid();
            for (int r = 0; r < data.Length; r++)
            {
                PdfGridRow row = grid.Rows.Add();
            }
            grid.Columns.Add(3);
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
            grid.Columns[0].Width = width * 0.15f;
            grid.Columns[1].Width = width * 0.15f;
            grid.Columns[2].Width = width * 0.15f;
            float height = page.Canvas.ClientSize.Height - (grid.Rows.Count + 1);
            grid.Rows[0].Height = 12.5f;
            grid.Rows[1].Height = 12.5f;
            grid.Rows[2].Height = 12.5f;
            grid.Rows[3].Height = 12.5f;

            //insert data to grid
            for (int r = 0; r < data.Length; r++)
            {
                String[] rowData = data[r].Split(';');
                for (int c = 0; c < rowData.Length; c++)
                {
                    grid.Rows[r].Cells[c].Value = rowData[c];
                }
            }

            grid.Rows[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold), true);

            //Set borders color to LightBule
            PdfBorders border = new PdfBorders();
            border.All = new PdfPen(Color.LightBlue);

            foreach (PdfGridRow pgr in grid.Rows)
            {
                foreach (PdfGridCell pgc in pgr.Cells)
                {
                    pgc.Style.Borders = border;
                }
            }

            PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
            document.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}
Published in Table
Wednesday, 23 January 2013 08:34

Set Table Layout in PDF with C#/VB.NET

Table Layout decides how the table displays in PDF page. People always set PDF table layout in order to let the table perfectly fit PDF page according to their own like. In this section, I will introduce a solution to set table layout in PDF via this .NET PDF component Spire.PDF for .NET with C#, VB.NET. First let us view the target PDF file as below picture:

Set PDF Table Layout

In Spire.PDF for .NET, there is a class called: Spire.Pdf.Tables.PdfTableLayoutFormat. By using this class, we can set table layout type and break type. Here Spire.PDF provided two layout types: Paginate and OnePage and two break type: FitElement and Fit Page. When you set the break type to be FitElement, PDF table will display according to the table length, while for FitPage choice, the table will automatically fit the PDF to every page ignoring table length.

Here let us see this method: PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format).There are three parameters passed. The first parameter determines the page size and margin. By setting the second parameter, we can set the horizontal and vertical distance between the table and PDF margin. While the last parameter is the table layout we just set. Obviously, we can set table layout by calling this method directly.

Now, you can download Spire.PDF for .NET and start table layout task by below key code:

[C#]
table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitElement;
            tableLayout.Layout = PdfLayoutType.Paginate;
            PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
            y = result.Bounds.Bottom + 5;
[VB.NET]
table.BeginRowLayout += New BeginRowLayoutEventHandler(table_BeginRowLayout)
Dim tableLayout As New PdfTableLayoutFormat()
tableLayout.Break = PdfLayoutBreakType.FitElement
tableLayout.Layout = PdfLayoutType.Paginate
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y), tableLayout)
y = result.Bounds.Bottom + 5

Spire.PDF for .NET is a PDF api that enables users to create, edit, read and handle PDF files in .NET applications.

Published in Table
Page 1 of 2