คำแนะนำเกี่ยวกับบทช่วยสอนสั้นๆ เกี่ยวกับ วิธีอ่านตาราง PDF ใน C# และอ่านเนื้อหาทั้งหมดภายในนั้น มีคำอธิบายโดยละเอียดสำหรับการแยกวิเคราะห์ตารางทั้งหมดในไฟล์ PDF จากนั้นเข้าถึงแต่ละแถวและเซลล์ของตารางใดตารางหนึ่ง ในการ อ่านตารางจาก PDF C# โค้ดประกอบด้วยบรรทัดไม่กี่บรรทัดโดยโหลดไฟล์ PDF ต้นทาง จากนั้นจึงแยกวิเคราะห์ตารางทั้งหมดเพื่ออ่านเนื้อหา
ขั้นตอนในการอ่านตาราง PDF ใน C#
- เพิ่มการอ้างอิงถึง Aspose.PDF for .NET เพื่ออ่านข้อมูลตารางใน PDF
- โหลดไฟล์ PDF ต้นทางโดยใช้วัตถุคลาส Document
- สร้างอินสแตนซ์ของคลาสออบเจกต์ TableAbsorber และอ่านตารางทั้งหมดจากหน้า PDF ที่ต้องการ
- วนซ้ำทุกแถวในตาราง PDF เป้าหมาย
- วนซ้ำเซลล์ทั้งหมดในแต่ละแถวและดึงส่วนข้อความทั้งหมด
- แสดงหรือประมวลผลแต่ละส่วนของข้อความในเซลล์
ปฏิบัติตามขั้นตอนเหล่านี้อย่างเป็นระบบเพื่อ อ่านตาราง PDF ใน C# โดยเริ่มต้นโหลดไฟล์ PDF จากนั้นตารางทั้งหมดจะถูกแยกวิเคราะห์โดยใช้วัตถุคลาส TableAbsorber เมื่อเยี่ยมชมตารางทั้งหมดในไฟล์ PDF แล้ว คุณอาจได้รับข้อมูลอ้างอิงไปยังตารางใดๆ ในคอลเลกชันที่แยกวิเคราะห์ คุณสามารถเข้าถึงตาราง แถว เซลล์ และส่วนข้อความใดๆ ในไฟล์ PDF เพื่อประมวลผลหรือแสดง
รหัสเพื่ออ่านตาราง PDF ใน C
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# แยกวิเคราะห์ตาราง PDF ทำให้เป็นไปได้โดยใช้คลาส TableAbsorber ซึ่งใช้สำหรับการอ่านตาราง อย่างไรก็ตาม คุณสามารถใช้ตัวเลือกอื่นๆ เช่น TextAbsorber, ParagraphAbsorber, FontAbsorber และ TextFragmentAbsorber เพื่อเข้าถึงองค์ประกอบต่างๆ ของเอกสาร คุณสามารถวนซ้ำทั้งคอลเลกชันหรือเข้าถึงแต่ละองค์ประกอบโดยใช้ดัชนีอาร์เรย์
เราได้เรียนรู้วิธีการ อ่านตาราง PDF ใน C# ในหัวข้อนี้ อย่างไรก็ตาม หากคุณต้องการอ่านบุ๊กมาร์ก PDF โปรดดูบทความใน วิธีอ่านบุ๊กมาร์กใน PDF โดยใช้ C#