ByteScout PDF Extractor SDK - VB.NET - Extract CSV from PDF and Fill Database in SQL Server - ByteScout

ByteScout PDF Extractor SDK – VB.NET – Extract CSV from PDF and Fill Database in SQL Server

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Extractor SDK – VB.NET – Extract CSV from PDF and Fill Database in SQL Server

How to extract CSV from PDF and fill database in SQL server in VB.NET using ByteScout PDF Extractor SDK

This code in VB.NET shows how to extract CSV from PDF and fill database in SQL server with this how to tutorial

With this source code sample you may quickly learn how to extract CSV from PDF and fill database in SQL server in VB.NET. What is ByteScout PDF Extractor SDK? It is the SDK that helps developers to extract data from unstructured documents, pdf, images, scanned and electronic forms. Includes AI functions like automatic table detection, automatic table extraction and restructuring, text recognition and text restoration from pdf and scanned documents. Includes PDF to CSV, PDF to XML, PDF to JSON, PDF to searchable PDF functions as well as methods for low level data extraction. It can help you to extract CSV from PDF and fill database in SQL server in your VB.NET application.

Fast application programming interfaces of ByteScout PDF Extractor SDK for VB.NET plus the instruction and the code below will help you quickly learn how to extract CSV from PDF and fill database in SQL server. Just copy and paste the code into your VB.NET application’s code and follow the instruction. Enjoy writing a code with ready-to-use sample VB.NET codes.

ByteScout PDF Extractor SDK free trial version is available on our website. VB.NET and other programming languages are supported.

Try ByteScout PDF Extractor SDK today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

Program.vb
      
Imports System.Data.SqlClient Imports Bytescout.PDFExtractor Module Program Sub Main() Try ' Step-1: Get Datatable Dim oDataTable = GetDataTableFromDocument("sample.pdf") ' PLEASE NOTE Please Replace With your connection String, You need to have "PersonData" table into your database. ' You can find that table from Scripts.sql file Dim connectionString As String = "Data Source=DESKTOP-92VMCQG\SQLEXPRESS;Initial Catalog=SampleDatabase;Persist Security Info=True;User ID=sa;Password=Hiren@009" ' Step-2: Insert into database InsertIntoSqlServerDatabase(oDataTable, connectionString) ' Step-3: Fetch from database and display results DisplayDatabaseResults(connectionString) Catch ex As Exception Console.WriteLine(ex.Message) End Try Console.WriteLine("Press enter key to exit...") Console.ReadLine() End Sub ''' <summary> ''' Inserts into Sql Server database ''' </summary> Private Sub InsertIntoSqlServerDatabase(ByVal oDataTable As DataTable, ByVal connectionString As String) Using con As SqlConnection = New SqlConnection(connectionString) ' Open connection con.Open() ' Sql query to insert data Dim cmdInsert As String = "Insert into PersonData (id, first_name, last_name, email, gender, ip_address) values (@id, @first_name, @last_name, @email, @gender, @ip_address)" For Each itmRow As DataRow In oDataTable.Rows ' Prepare sql command Dim cmd As SqlCommand = New SqlCommand(cmdInsert, con) cmd.CommandType = CommandType.Text cmd.Parameters.Add(New SqlParameter("@id", Convert.ToString(itmRow("id")))) cmd.Parameters.Add(New SqlParameter("@first_name", Convert.ToString(itmRow("first_name")))) cmd.Parameters.Add(New SqlParameter("@last_name", Convert.ToString(itmRow("last_name")))) cmd.Parameters.Add(New SqlParameter("@email", Convert.ToString(itmRow("email")))) cmd.Parameters.Add(New SqlParameter("@gender", Convert.ToString(itmRow("gender")))) cmd.Parameters.Add(New SqlParameter("@ip_address", Convert.ToString(itmRow("ip_address")))) ' Execute sql command cmd.ExecuteNonQuery() Next ' Close connection con.Close() End Using End Sub ' Displays inserted database results Private Sub DisplayDatabaseResults(ByVal connectionString As String) ' Person data holder Dim personDataTable As DataTable = New DataTable() Using con As SqlConnection = New SqlConnection(connectionString) ' Sql query to fetch data Dim cmdInsert As String = "SELECT id, first_name, last_name, email, gender, ip_address FROM PersonData;" ' Prepare sql command Dim cmd As SqlCommand = New SqlCommand(cmdInsert, con) cmd.CommandType = CommandType.Text ' Prepare DataAdapter Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(cmd) ' Fill person dataTable dataAdapter.Fill(personDataTable) End Using ' Display all person data if any If personDataTable IsNot Nothing AndAlso personDataTable.Rows.Count > 0 Then ' Print all columns For Each column As DataColumn In personDataTable.Columns Console.Write("{0} | ", column.ColumnName) Next Console.WriteLine() ' Print all data For Each dataRow As DataRow In personDataTable.Rows For Each column As DataColumn In personDataTable.Columns Console.Write("{0} | ", dataRow(column.ColumnName)) Next Console.WriteLine() Next Else Console.WriteLine("No data retrieved..") End If End Sub ''' <summary> ''' Get DataTable from Document ''' </summary> Private Function GetDataTableFromDocument(ByVal fileName As String) As DataTable Dim oDataTable As DataTable = Nothing ' Initialise table detector Using tableDetector As TableDetector = New TableDetector("demo", "demo") Using CSVExtractor As CSVExtractor = New CSVExtractor("demo", "demo") ' Set table detection mode to "bordered tables" - best for tables with closed solid borders. tableDetector.ColumnDetectionMode = ColumnDetectionMode.BorderedTables ' We should define what kind of tables we should detect. ' So we set min required number of columns to 2 ... tableDetector.DetectionMinNumberOfColumns = 2 ' ... and we set min required number of rows to 2 tableDetector.DetectionMinNumberOfRows = 2 ' Load PDF document tableDetector.LoadDocumentFromFile(fileName) CSVExtractor.LoadDocumentFromFile(fileName) ' Get page count Dim pageCount As Integer = tableDetector.GetPageCount() If tableDetector.FindTable(0) Then ' Set extraction area for CSV extractor to rectangle received from the table detector CSVExtractor.SetExtractionArea(tableDetector.FoundTableLocation) ' Generate CSV data Dim allCsvData = CSVExtractor.GetCSV() ' Generate Datatable oDataTable = GetDataTableFromCSV(allCsvData) End If End Using End Using Return oDataTable End Function ''' <summary> ''' Get Datatable from CSV ''' </summary> Private Function GetDataTableFromCSV(ByVal allCsvData As String) As DataTable Dim oRetDataTable = New DataTable() oRetDataTable.Columns.Add("id") oRetDataTable.Columns.Add("first_name") oRetDataTable.Columns.Add("last_name") oRetDataTable.Columns.Add("email") oRetDataTable.Columns.Add("gender") oRetDataTable.Columns.Add("ip_address") Dim rows = allCsvData.Split(vbLf) For iRow As Integer = 1 To rows.Length - 1 ' Get all column data Dim columns = rows(iRow).Split(","c) If columns.Length >= 5 Then ' Prepare new row Dim oRow = oRetDataTable.NewRow() oRow("id") = columns(0) oRow("first_name") = columns(1) oRow("last_name") = columns(2) oRow("email") = columns(3) oRow("gender") = columns(4) oRow("ip_address") = columns(5) ' Add row back to datatable oRetDataTable.Rows.Add(oRow) End If Next ' Return DataTable Return oRetDataTable End Function End Module

Try ByteScout PDF Extractor SDK today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

VIDEO

ON-PREMISE VERSION INFORMATION

Get 60 Day Free Trial or Visit ByteScout PDF Extractor SDK Home Page

Explore ByteScout PDF Extractor SDK Documentation

Get ByteScout PDF Extractor SDK Free Training

WEB API

Get Your Free API Key

Explore Web API Documentation

Tutorials:

prev
next