翻譯|使用教程|編輯:吉煒煒|2025-02-17 10:49:30.597|閱讀 100 次
概述:使用 TX Text Control 進行基于模板的文本提取提供了一種從 PDF 文檔中檢索結構化數據的有效方法。本文介紹如何使用模板從 PDF 文檔中提取文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
處理 PDF 文檔時,提取公司名稱、社會保險號或發票號等特定信息可能是一項挑戰。在理想情況下,這些數據將存在于結構良好的表單字段中,可以通過編程輕松訪問(包括使用 TX Text Control 功能)。但是,許多 PDF 文件都是扁平化的,這意味著表單字段被刪除,只留下原始文本。
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創建、編輯、打印、郵件合并、格式轉換、拆分合并、導入導出、批量生成等功能。廣泛應用于企業文檔管理,網站內容發布,電子病歷中病案模板創建、病歷書寫、修改歷史、連續打印、病案歸檔等功能的實現。TX Text Control 基于預定義模板區域提供了強大的文本提取解決方案,允許您甚至可以從扁平化的 PDF 中提取結構化信息。
基于模板的文本提取涉及定義一個矩形(邊界框),特定文本預計會出現在 PDF 文檔中。定義此區域后,TX Text Control 便可提取定義矩形內的文本行,確保數據檢索準確。
第一步,我們希望確定已知文檔中已知數據的文本位置。使用示例 PDF,找到在特定位置一致出現的已知文本(例如公司名稱或發票號碼)。
讓我們看一下非常典型的美國稅表W9。
該文檔仍然啟用了表單字段,如果我們有權訪問該文檔,則只需遍歷表單字段即可使用 TX Text Control 輕松提取數據。
但在這種情況下,我們無法訪問源文檔,只能訪問扁平化版本,其中所有表單字段都已被刪除,只有文本可見。
現在我們要定義矩形來搜索公司名稱。
為了演示使用 TX 文本控制庫實現這一點有多么簡單,我們將使用 .NET 控制臺應用程序。
確保您下載了.NET 8 SDK附帶的最新版本的 Visual Studio 2022 。
在 Visual Studio 2022 中,通過選擇“創建新項目”來創建新項目。
選擇控制臺應用程序作為項目模板然后單擊下一步確認。
為您的項目選擇一個名稱然后單擊下一步確認。
在下一個對話框中,選擇.NET 8 (長期支持)作為框架并通過創建進行確認。
在解決方案資源管理器中,選擇您創建的項目,然后從項目主菜單中選擇管理 NuGet 包...。
從包源下拉菜單中選擇文本控制離線包。
安裝以下軟件包的最新版本:
以下代碼使用 TX Text Control 查找我們的訓練數據“Text Control, LLC”的已知值,并返回稍后將用于所有其他文檔的位置。
using TXTextControl.DocumentServer.PDF.Contents; | |
try | |
{ | |
string pdfFilePath = "FormW9.pdf"; | |
// Check if the file exists before processing | |
if (!File.Exists(pdfFilePath)) | |
{ | |
Console.WriteLine($"Error: File '{pdfFilePath}' not found."); | |
return; | |
} | |
// Load PDF lines | |
var pdfLines = new Lines(pdfFilePath); | |
// Find the target text | |
var trainLines = pdfLines.Find("Text Control, LLC"); | |
// Check if any lines were found before accessing the index | |
if (trainLines.Count > 0) | |
{ | |
Console.WriteLine(trainLines[0].Rectangle.ToString()); | |
} | |
else | |
{ | |
Console.WriteLine("Text not found in the PDF."); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"An error occurred: {ex.Message}"); | |
} |
控制臺包含找到的文本的位置。
{X=1192,Y=2566,Width=1510,Height=180}
下一個代碼片段加載第二個文檔并在給定的矩形中搜索文本,該文本是我們從訓練數據中檢索到的。
using System.Drawing; | |
using TXTextControl.DocumentServer.PDF.Contents; | |
try | |
{ | |
string pdfFilePath = "FormW9_2.pdf"; | |
// Check if the file exists before processing | |
if (!File.Exists(pdfFilePath)) | |
{ | |
Console.WriteLine($"Error: File '{pdfFilePath}' not found."); | |
return; | |
} | |
// Load PDF lines | |
var pdfLines = new Lines(pdfFilePath); | |
// Define the search area | |
var searchRectangle = new Rectangle(1192, 2566, 1510, 180); | |
// Find text within the defined rectangle (include partial matches) | |
var contentLines = pdfLines.Find(searchRectangle, true); | |
// Filter only page 1 content lines | |
var page1ContentLines = contentLines.Where(cl => cl.Page == 1).ToList(); | |
// Check if any content was found | |
if (page1ContentLines.Count > 0) | |
{ | |
Console.WriteLine(page1ContentLines[0].Text); | |
} | |
else | |
{ | |
Console.WriteLine("No content found in the specified rectangle on page 1."); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"An error occurred: {ex.Message}"); | |
} |
控制臺包含從第二個文檔中提取的文本。
Document Processing Enterprises Ltd.
因為我們使用了Findtrue方法的第二個參數,所以搜索將返回整行,即使在這種情況下公司名稱更長。
即使公司名稱到了行末,它也會找到正確的值。
This is a very long company name - This is a very long company name - This is a very long company name
基于模板的文本提取是一項強大的功能,可用于從 PDF 文檔中提取結構化信息。通過在已知文本周圍定義一個矩形,TX Text Control 可以從類似文檔中提取文本,即使文本不在表單字段中。
慧都是Text Control的官方授權代理商,提供TX Text Control 系列產品免費試用,咨詢,正版銷售等于一體的專業化服務。
下載或體驗產品,請咨詢,或撥打產品熱線:023-68661681
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn