Code using Ver 9.4.12:
- Code: Select all
string strTargetPage = @"D:\admin\SpirePDF\testable2.pdf";
Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(strTargetPage);
PdfTableExtractor extractor = new PdfTableExtractor(doc);
PdfTable[] tableLists = extractor.ExtractTable(0);
//Determine if the table list is null
if (tableLists != null && tableLists.Length > 0)
{
//Loop through the table in the list
foreach (PdfTable table in tableLists)
{
//Get row number and column number of a certain table
int iTotalRow = table.GetRowCount();
int iTotalCol = table.GetColumnCount();
//OutputExcel
Workbook workbook = new Workbook();
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
//Loop though the row and colunm
for (int iRow = 0; iRow < iTotalRow; iRow++)
{
for (int iCol = 0; iCol < iTotalCol; iCol++)
{
//Get text from the specific cell
string text = table.GetText(iRow, iCol);
sheet.Range[iRow + 1, iCol + 1].Value = text;
}
}
//Save your file
string strExcel = strTargetPage + "_" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".xlsx";
workbook.SaveToFile(strExcel);
}
}
Table:
Result: