pdf has text as well as one table (with rows and columns) i want to isolate and get table in pdf as a datatable. how can do that with asp.net c# ?
Loading
pdf has text as well as one table (with rows and columns) i want to isolate and get table in pdf as a datatable. how can do that with asp.net c# ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jul 21, 2023, 1:30 AM EDT
Check this, it may help you.
using iTextSharp.text; using iTextSharp.text.pdf; using System.Data; using System.IO; public DataTable ExtractTableFromPDF(string pdfFilePath) { DataTable dataTable = new DataTable(); using (PdfReader reader = new PdfReader(pdfFilePath)) { for (int page = 1; page <= reader.NumberOfPages; page++) { ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); string currentPageText = PdfTextExtractor.GetTextFromPage(reader, page, strategy); string[] lines = currentPageText.Split('\n'); bool tableStarted = false; foreach (string line in lines) { if (line.Contains("HeaderRowText")) // Replace "HeaderRowText" with the unique identifier of the table's header row { tableStarted = true; string[] headers = line.Split('\t'); // Split by tab or another delimiter used in the table foreach (string header in headers) { dataTable.Columns.Add(header); } } else if (tableStarted && !string.IsNullOrWhiteSpace(line)) { string[] values = line.Split('\t'); // Split by tab or another delimiter used in the table dataTable.Rows.Add(values); } } } } return dataTable; }string pdfFilePath = "path/to/your/pdf/file.pdf"; DataTable extractedTable = ExtractTableFromPDF(pdfFilePath);Deepak RawatRank #646 · 3.7K reputationPosted Jul 21, 2023, 1:48 AM EDT
In your ASP.NET project, open the NuGet Package Manager and search for "iTextSharp." Install the iTextSharp (iText 7 Community) package.
using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using iText.Kernel.Geom; using iText.Kernel.Font; using iText.IO.Font.Constants; using iText.Kernel.Colors; public DataTable ExtractTableFromPDF(string pdfFilePath, int tableIndex) { DataTable dataTable = new DataTable(); using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(pdfFilePath))) { PdfPage page = pdfDocument.GetPage(1); // Assuming the table is on the first page ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); string pageContent = PdfTextExtractor.GetTextFromPage(page, strategy); string[] lines = pageContent.Split('\n'); // Assuming the table is represented as lines in the PDF string tableLine = lines[tableIndex]; // Split the table line into individual cells based on separators (e.g., tabs, spaces, etc.) string[] cells = tableLine.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries); // Assuming the first row contains column headers, populate the DataTable's columns foreach (string cell in cells) { dataTable.Columns.Add(cell.Trim()); } // Start from the second row to populate the DataTable's rows for (int i = tableIndex + 1; i < lines.Length; i++) { string rowLine = lines[i]; string[] rowCells = rowLine.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries); DataRow dataRow = dataTable.NewRow(); for (int j = 0; j < rowCells.Length; j++) { dataRow[j] = rowCells[j].Trim(); } dataTable.Rows.Add(dataRow); } } return dataTable; }