翻譯|使用教程|編輯:顏馨|2023-05-16 10:09:01.360|閱讀 231 次
概述:本章介紹如何在C#中對PDF文檔進行OCR并從PDF中提取文本
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Aspose.OCR是一款字符識別組件,它使得開發(fā)人員可以添加OCR功能到他們的ASP.NET Web應(yīng)用程序、web服務(wù)和windows應(yīng)用程序中。它提供了一個簡單的類集用于控制字符識別。Aspose.OCR目的是為那些需要在他們自己的應(yīng)用程序中使用圖像(BMP和TIFF)的開發(fā)人員提供需求。它允許開發(fā)人員快速而簡單的從圖像中提取文本,并節(jié)省了從頭開發(fā)一個OCR解決方案的時間和精力。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
PDF 文件是最常見的業(yè)務(wù)文檔之一。在某些情況下,我們可能需要以編程方式閱讀掃描的PDF文檔。從掃描的PDF文件中提取文本的困難導(dǎo)致了工具的開發(fā),這些工具可以更輕松地從此類PDF文檔中閱讀和檢索文本。根據(jù)文檔的內(nèi)容,出于多種原因,從 PDF 文件中提取文本可能很有用。在本文中,我們將學(xué)習(xí)如何在C#中對PDF文檔進行OCR并從PDF中提取文本。
我們將使用 Aspose.OCR for .NET API 對 PDF 文檔執(zhí)行 OCR。它可以識別掃描的圖像,智能手機照片,屏幕截圖和圖像區(qū)域。API 以最流行的文檔和數(shù)據(jù)交換格式返回識別的文本結(jié)果。除了將圖像轉(zhuǎn)換為文本外,API 還可以根據(jù)掃描創(chuàng)建可搜索的 PDF。此外,它能夠自動更正已識別文本中的拼寫錯誤。
該 API 提供了 AsposeOcr 類,該類提供了執(zhí)行 OCR 操作的各種方法。它提供了RecognizePdf(字符串,DocumentRecognitionSettings)方法來識別所提供的PDF文檔中的文本。API 的 DocumentRecognitionSettings 類提供 PDF 識別過程的設(shè)置。類表示圖像識別的結(jié)果。
我們可以對PDF文檔執(zhí)行OCR,并按照以下步驟提取識別的文本:
以下示例代碼演示如何在 C# 中對 PDF 文檔進行 OCR 和提取識別的文本。
// This code example demonstrates how to OCR PDF documents and extract the recognized text. // Initialize the PCR engine AsposeOcr recognitionEngine = new AsposeOcr(); // Initialize recognition settings DocumentRecognitionSettings recognitionSettings = new DocumentRecognitionSettings(); // Specify language for OCR. Multi-language by default recognitionSettings.Language = Language.Eng; // Recognize text from PDF List<RecognitionResult> results = recognitionEngine.RecognizePdf("C:\\Files\\sample.pdf", recognitionSettings); // Show the recognized text foreach (RecognitionResult result in results) { Console.WriteLine(result.RecognitionText); }
我們可以對PDF文檔執(zhí)行OCR,并按照以下步驟保存識別的文本:
以下示例代碼演示如何對 PDF 文檔進行 OCR 并將識別的文本保存在 C# 中。
// This code example demonstrates how to OCR PDF documents and extract the recognized text. // Initialize the PCR engine AsposeOcr recognitionEngine = new AsposeOcr(); // Initialize recognition settings DocumentRecognitionSettings recognitionSettings = new DocumentRecognitionSettings(); // Specify language for OCR. Multi-language by default recognitionSettings.Language = Language.Eng; // Recognize text from PDF List<RecognitionResult> results = recognitionEngine.RecognizePdf("C:\\Files\\sample.pdf", recognitionSettings); // Save the recognized text AsposeOcr.SaveMultipageDocument("C:\\Files\\OCR_result.txt", SaveFormat.Text, results);
我們可以對掃描的PDF文檔執(zhí)行OCR,并按照前面提到的步驟將識別的文本保存在Word文檔中。但是,我們只需要在最后一步中指定 SaveFormat.Docx。
下面的示例代碼演示如何在 C# 中對 PDF 進行 OCR PDF 并將識別的文本另存為 Word 文檔。
// This code example demonstrates how to OCR PDF documents and save the recognized text as DOCX. // Initialize the PCR engine AsposeOcr recognitionEngine = new AsposeOcr(); // Initialize recognition settings DocumentRecognitionSettings recognitionSettings = new DocumentRecognitionSettings(); // Specify language for OCR. Multi-language by default recognitionSettings.Language = Language.Eng; // Recognize text from PDF List<RecognitionResult> results = recognitionEngine.RecognizePdf("C:\\Files\\sample.pdf", recognitionSettings); // Save the recognized text as DOCX AsposeOcr.SaveMultipageDocument("C:\\Files\\OCR_result.docx", SaveFormat.Docx, results);
我們可以對 PDF 文檔執(zhí)行 OCR,并按照前面提到的步驟將識別的文本保存在 JSON 文件中。但是,我們只需要在最后一步中指定 SaveFormat.Json。
以下示例代碼演示如何在 C# 中對 PDF 進行 OCR PDF 并將識別的文本另存為 JSON 文件。
// This code example demonstrates how to OCR PDF documents and save the recognized text as JSON. // Initialize the PCR engine AsposeOcr recognitionEngine = new AsposeOcr(); // Initialize recognition settings DocumentRecognitionSettings recognitionSettings = new DocumentRecognitionSettings(); // Specify language for OCR. Multi-language by default recognitionSettings.Language = Language.Eng; // Recognize text from PDF List<RecognitionResult> results = recognitionEngine.RecognizePdf("C:\\Files\\sample.pdf", recognitionSettings); // Save the recognized text as JSON AsposeOcr.SaveMultipageDocument("C:\\Files\\OCR_result.json", SaveFormat.Json, results);
以上便是如何對 PDF 文檔執(zhí)行 OCR 以及如何在 C# 中從 PDF 中提取文本的詳細步驟,希望能幫到您,若有其他問題歡迎加入我們的技術(shù)交流群,或關(guān)注我們。
歡迎下載|體驗更多Aspose產(chǎn)品
獲取更多信息請咨詢或 加入Aspose技術(shù)交流群(761297826)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn