C#/VB.NET – Extract All Tables from a PDF Document

Sometimes you may need to extract table data from PDF documents. For example, there is some useful information stored in the tables of a PDF invoice and you want to extract the data for further analysis. In this article, I am going to introduce how to extract data from all tables in a PDF document using Spire.PDF for .NET.

Installing Spire.PDF

First of all, you need to download the latest version of Spire.PDF from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet.

Now, let’s take a look at the code part.

Using the Code

Spire.PDF offers the PdfTableExtractor.ExtractTable() method to extract tables from a specific page. Below are the detailed steps to extract table from a whole PDF document.

Step 1. Create a Document object, and the load the source PDF file.

Step 2. Loop through the pages in the document, and get the table list from a specific page using ExtractTable() method.

Step 3. Loop through the cells in a certain table, and get the cell value via PdfTable.GetText() method.

Step 4. Write the extracted data in a TXT file.

[C#]

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;
            int tableNum = 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)
                    {
                        builder.Append("Table " + tableNum);
                        builder.Append("\r\n");

                        //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");
                     
                        }
                        builder.Append("\r\n");
                        tableNum += 1;
                    }
                }
            }

            //Write to a .txt file
            File.WriteAllText("Table.txt", builder.ToString());
        }
    }
}

[VB.NET]

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 
            Dim tableNum As Integer =  1 
 
            '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
                        builder.Append("Table " + tableNum)
                        builder.Append("\r\n")
 
                        '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
                        builder.Append("\r\n")
                        tableNum += 1
                    Next
                End If
            Next
 
            'Write to a .txt file
            File.WriteAllText("Table.txt", builder.ToString())
        End Sub
    End Class
End Namespace

Leave a comment