Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
L’extraction de tableaux à partir de PDF n’est pas une tâche triviale car les tableaux peuvent être créés de différentes manières.
Aspose.PDF for .NET dispose d’un outil pour faciliter la récupération des tableaux. Pour extraire les données d’un tableau, vous devez effectuer les étapes suivantes :
TableList
est une liste de AbsorbedTable. Pour obtenir les données, parcourez TableList
et gérez RowList et CellList.Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing.
L’exemple suivant montre l’extraction de tableaux de toutes les pages :
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractTable()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
foreach (var page in document.Pages)
{
Aspose.Pdf.TableAbsorber absorber = new Aspose.Pdf.TableAbsorber();
absorber.Visit(page);
foreach (var table in absorber.TableList)
{
Console.WriteLine("Table");
foreach (var row in table.RowList)
{
foreach (var cell in row.CellList)
{
foreach (var fragment in cell.TextFragments)
{
var sb = new StringBuilder();
foreach (var seg in fragment.Segments)
{
sb.Append(seg.Text);
}
Console.Write($"{sb.ToString()}|");
}
}
Console.WriteLine();
}
}
}
}
}
Chaque tableau absorbé a une propriété Rectangle qui décrit la position du tableau sur la page.
Si vous devez extraire des tableaux situés dans une région spécifique, vous devez travailler avec des coordonnées spécifiques.
Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing.
L’exemple suivant montre comment extraire un tableau marqué avec une annotation carrée :
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractMarkedTable()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
var page = document.Pages[1];
var squareAnnotation =
page.Annotations.FirstOrDefault(ann => ann.AnnotationType == Annotations.AnnotationType.Square)
as Aspose.Pdf.Annotations.SquareAnnotation;
var absorber = new Aspose.Pdf.Text.TableAbsorber();
absorber.Visit(page);
foreach (var table in absorber.TableList)
{
var isInRegion = (squareAnnotation.Rect.LLX < table.Rectangle.LLX) &&
(squareAnnotation.Rect.LLY < table.Rectangle.LLY) &&
(squareAnnotation.Rect.URX > table.Rectangle.URX) &&
(squareAnnotation.Rect.URY > table.Rectangle.URY);
if (isInRegion)
{
foreach (var row in table.RowList)
{
foreach (var cell in row.CellList)
{
foreach (var fragment in cell.TextFragments)
{
var sb = new StringBuilder();
foreach (var seg in fragment.Segments)
{
sb.Append(seg.Text);
}
var text = sb.ToString();
Console.Write($"{text}|");
}
}
Console.WriteLine();
}
}
}
}
}
L’exemple suivant montre comment extraire un tableau et le stocker sous forme de fichier CSV. Pour voir comment convertir un PDF en feuille de calcul Excel, veuillez consulter l’article Convertir PDF en Excel.
Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractTableSaveExcel()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Tables();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Instantiate ExcelSave Option object
Aspose.Pdf.ExcelSaveOptions excelSave = new Aspose.Pdf.ExcelSaveOptions { Format = ExcelSaveOptions.ExcelFormat.CSV };
// Save the output in XLS format
document.Save(dataDir + "ExtractTableSaveXLS_out.xlsx", excelSave);
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.