Estrai tabelle dal documento PDF tramite C#

Come estrarre la tabella dal PDF usando la libreria C#

Come estrarre tabelle dal documento PDF utilizzando la libreria .NET

Per estrarre la tabella, useremo l’API Aspose.PDF for .NET che è un’API di manipolazione dei documenti ricca di funzionalità, potente e facile da usare per la piattaforma net. Apri il gestore pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Gestione pacchetti.

Package Manager Console

PM > Install-Package Aspose.PDF

Estrai tabelle da PDF tramite C#


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Creare l’oggetto TableAbsorber per trovare le tabelle.
  3. Visita la prima pagina con assorbitore.
  4. Ottieni la prima tabella sulla pagina.
  5. Rimuovi la tabella. Salvate il file.

Estrai tabelle dal PDF - C#


Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"c:\tmp\the_worlds_cities_in_2018_data_booklet 7.pdf");           
    foreach (var page in pdfDocument.Pages)
    {
        Aspose.Pdf.Text.TableAbsorber absorber = new Aspose.Pdf.Text.TableAbsorber();
        absorber.Visit(page);
        foreach (AbsorbedTable table in absorber.TableList)
        {
            foreach (AbsorbedRow row in table.RowList)
            {
                foreach (AbsorbedCell cell in row.CellList)
                {
                    TextFragment textfragment = new TextFragment();
                    TextFragmentCollection textFragmentCollection = cell.TextFragments;
                    foreach (TextFragment fragment in textFragmentCollection)
                    {
                        string txt = "";
                        foreach (TextSegment seg in fragment.Segments)
                        {
                            txt += seg.Text;
                        }
                        Console.WriteLine(txt);
                    }
                }
            }
        }
    }