翻譯|使用教程|編輯:李顯亮|2020-04-22 10:40:52.910|閱讀 501 次
概述:為了在C ++應用程序中自動進行PDF解析,本文演示了如何使用C ++ 從PDF文檔中提取文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在數字信息領域,從文檔(PDF,文字處理,網頁等)中提取文本具有多種用例。例如,它可以用于解析文檔,執行文本分析,信息檢索,將文檔內容存儲到數據庫中等等。如果縮小范圍,PDF是保存和共享數字信息的最廣泛使用的文檔格式之一。這種受歡迎程度使PDF文檔成為信息的巨大來源。因此,從PDF文檔中解析或提取文本可能會涉及多種文本分析方案。
為了在C ++應用程序中自動進行PDF解析,本文演示了如何使用C ++ 從PDF文檔中提取文本。它涵蓋以下文本提取方案:
Aspose.PDF for C ++使您可以通過幾個簡單的步驟來解析PDF文檔。以下是從PDF文檔提取文本的方法。
以下代碼示例顯示了如何使用C ++從PDF中提取文本。
auto extractor = MakeObject(); // Bind source PDF document extractor->BindPdf(u"candy.pdf"); // Extract text from PDF to PdfExtractor extractor->ExtractText(); auto memStream = MakeObject(); // Save text into memory stream extractor->GetText(memStream); auto unicode = System::Text::Encoding::get_Unicode(); String allText = unicode->GetString(memStream->ToArray()); // Print extracted text Console::WriteLine(u"Extracted text:"); Console::WriteLine(allText);
在某些情況下,僅需要從幾頁PDF中提取文本。在這種情況下,可以通過設置開始和結束頁碼來指定PDF中的頁面范圍。以下是從PDF文檔中特定頁面提取文本的步驟。
以下代碼示例顯示了如何從C ++中的PDF特定頁面提取文本。
auto extractor = MakeObject(); // Bind source PDF document extractor->BindPdf(u"candy.pdf"); // Set page range extractor->set_StartPage(2); extractor->set_EndPage(2); // Extract text from PDF to PdfExtractor extractor->ExtractText(); auto memStream = MakeObject(); // Save text into memory stream extractor->GetText(memStream); auto unicode = System::Text::Encoding::get_Unicode(); String allText = unicode->GetString(memStream->ToArray()); // Print extracted text Console::WriteLine(u"Extracted text:"); Console::WriteLine(allText);
您可以從文檔的每一頁分別提取文本,而不是從PDF文檔提取所有文本。以下是從PDF中逐頁提取文本的步驟。
下面的代碼示例演示如何在C ++中從PDF逐頁提取文本。
auto extractor = MakeObject(); // Bind source PDF document extractor->BindPdf(u"candy.pdf"); // Extract text from PDF to PdfExtractor extractor->ExtractText(); auto unicode = System::Text::Encoding::get_Unicode(); int pageNumber = 1; while (extractor->HasNextPageText()) { auto memStream = MakeObject(); extractor->GetNextPageText(memStream); String text; // Specify Unicode encoding type in StreamReader constructor auto streamReader = MakeObject(memStream, unicode); streamReader->get_BaseStream()->Seek(0, SeekOrigin::Begin); text = streamReader->ReadToEnd(); streamReader->Dispose(); // Print extracted text std::cout << "Page: " << pageNumber << "\n"; Console::Write(text); std::cout << "\n------------------------\n"; pageNumber++; }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn