C#에서 PDF 표를 읽는 방법

이 짧은 방법 자습서는 C#에서 PDF 테이블을 읽고 그 안의 모든 내용을 읽는 방법에 대해 안내합니다. PDF 파일의 모든 테이블을 구문 분석한 다음 특정 테이블의 개별 행과 셀에 액세스하는 방법에 대한 자세한 설명을 제공합니다. PDF에서 테이블을 읽기 위해 C# 코드는 소스 PDF 파일이 로드된 다음 내용을 읽기 위해 모든 테이블이 구문 분석되는 몇 줄로 구성됩니다.

C#에서 PDF 표를 읽는 단계

  1. PDF에서 테이블 데이터를 읽으려면 Aspose.PDF for .NET에 대한 참조를 추가하세요.
  2. Document 클래스 개체를 사용하여 소스 PDF 파일 로드
  3. TableAbsorber 클래스 개체를 인스턴스화하고 원하는 PDF 페이지에서 모든 테이블을 읽습니다.
  4. 대상 PDF 테이블의 모든 행을 반복합니다.
  5. 각 행의 모든 셀을 반복하고 모든 텍스트 조각을 가져옵니다.
  6. 셀의 각 텍스트 조각 표시 또는 처리

이 단계에서 체계적인 접근 방식을 따라 *C#*에서 PDF 테이블을 읽습니다. 여기서 처음에는 PDF 파일이 로드되고 모든 테이블은 TableAbsorber 클래스 개체를 사용하여 구문 분석됩니다. PDF 파일에서 모든 테이블을 방문하면 구문 분석된 컬렉션의 테이블에 대한 참조를 얻을 수 있습니다. PDF 파일의 모든 테이블, 행, 셀 및 텍스트 조각에 액세스하여 처리하거나 표시할 수 있습니다.

C#에서 PDF 테이블을 읽는 코드

using System;
using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace ReadPDFTableInCSharp
{
class Program
{
static void Main(string[] args)
{
// Instantiate the license to avoid trial limitations while reading table data from PDF
License asposePdfLicense = new License();
asposePdfLicense.SetLicense("Aspose.pdf.lic");
// Load source PDF document having a table in it
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"PdfWithTable.pdf");
// Declare and initialize TableAbsorber class object for reading table from the PDF
Aspose.Pdf.Text.TableAbsorber tableAbsorber = new Aspose.Pdf.Text.TableAbsorber();
// Parse all the tables from the desired page in the PDF
tableAbsorber.Visit(pdfDocument.Pages[1]);
// Get reference to the first table in the parsed collection
AbsorbedTable absorbedTable = tableAbsorber.TableList[0];
// Iterate through all the rows in the PDF table
foreach (AbsorbedRow pdfTableRow in absorbedTable.RowList)
{
// Iterate through all the cells in the pdf table row
foreach (AbsorbedCell pdfTableCell in pdfTableRow.CellList)
{
// Fetch all the text fragments in the cell
TextFragmentCollection textFragmentCollection = pdfTableCell.TextFragments;
// Iterate through all the text fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
// Display the text
Console.WriteLine(textFragment.Text);
}
}
}
System.Console.WriteLine("Done");
}
}
}

C# parse PDF table을 사용하는 이 샘플 코드에서는 테이블을 읽는 데 사용되는 TableAbsorber 클래스를 사용하여 가능합니다. 그러나 TextAbsorber, ParagraphAbsorber, FontAbsorber 및 TextFragmentAbsorber와 같은 다른 옵션을 사용하여 문서의 다른 요소에 액세스할 수도 있습니다. 전체 컬렉션을 반복하거나 배열 인덱스를 사용하여 개별 요소에 액세스할 수 있습니다.

이 항목에서 *C#*에서 PDF 테이블을 읽는 방법을 배웠습니다. 그러나 PDF 북마크를 읽으려면 C#을 사용하여 PDF에서 책갈피를 읽는 방법에 있는 문서를 참조하세요.

 한국인