在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PDF 檔案因其能夠在不同平台間保留格式而成為文件交換的廣泛使用格式。 在各種應用程式中,以程式方式讀取 PDF 文件的內容變得非常有價值。
在本文中,我們將學習如何使用Xpdf
命令行工具在 C++ 中查看 PDF 文件中的文本。 Xpdf
提供了一套命令行工具和 C++ 庫,用於處理 PDF 文件,包括文本提取。 透過將Xpdf
整合到我們的 C++ PDF 查看器程式中,我們可以高效地查看 PDF 檔案中的文字內容並以程式化的方式進行處理。
Xpdf
- C++ 庫和命令行工具Xpdf 是一個開源軟體套件,提供用於處理 PDF 文件的各種工具和庫。 它包括各種命令行工具和 C++ 庫,這些工具和庫實現了與 PDF 相關的功能,如解析、渲染、打印和文本提取。 Xpdf 的命令行工具也提供了直接從終端查看 PDF 文件的方法。
Xpdf 的一個關鍵組件是 pdftotext
,它主要以從 PDF 文件中提取文本內容而聞名。 然而,當與其他工具如pdftops
和pdfimages
結合使用時,Xpdf
允許用戶以不同的方式查看 PDF 內容。 pdftotext
工具在從 PDF 提取文字信息以供進一步處理或分析方面非常有用,並且提供選項來指定從哪些頁面提取文字。
在開始之前,請確保您已具備以下先決條件:
您的系統上安裝了 C++ 編譯器,例如 GCC 或 Clang。 我們將使用Code::Blocks IDE來達成此目的。
開啟 Code::Blocks: 啟動您電腦上的 Code::Blocks IDE。
建立新專案:從頂部選單中點擊「檔案」,然後從下拉選單中選擇「新建」。 然後,從子菜單中點擊「專案」。
選擇專案類型: 在「從範本新增」視窗中,選擇「主控台應用程式」,然後點擊「Go」。 然後選擇語言「C/C++」,然後點擊「下一步」。
輸入專案詳情:在「專案名稱」欄位,為您的專案命名(例如,「PDFViewer」)。 選擇您想要儲存專案檔案的位置,然後點擊「下一步」。
首先,讓我們將所需的標頭檔添加到main.cpp檔案中:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
在main
函數中,我們宣告兩個字串:pdfPath
和outputFilePath
。 pdfPath
儲存輸入PDF檔案的路徑,outputFilePath
儲存提取文字將被保存為純文字檔案的路徑。
輸入文件如下:
pdftotext
命令string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
在這裡,我們使用pdfPath
和outputFilePath
變數構建了pdftotext
命令,以開啟 PDF 文件來查看其內容。 然後呼叫system
函數來執行命令,其返回值存儲在status
變量中。
檢查文字提取狀態
if (status == 0)
{
cout << "Text extraction successful." << endl;
} else
{
cout << "Text extraction failed." << endl;
}
if (status == 0)
{
cout << "Text extraction successful." << endl;
} else
{
cout << "Text extraction failed." << endl;
}
我們檢查 status
變數以查看 pdftotext
命令是否成功執行。 如果status
等於0,這表示文本提取成功,我們會打印一條成功信息。 如果status
非零,則表示發生錯誤,並且我們會打印錯誤訊息。
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) {
string textContent;
string line;
while (getline(outputFile, line)) {
textContent += line + "\n";
}
outputFile.close();
cout << "Text content extracted from PDF:" << endl;
cout << textContent << endl;
} else {
cout << "Failed to open output file." << endl;
}
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) {
string textContent;
string line;
while (getline(outputFile, line)) {
textContent += line + "\n";
}
outputFile.close();
cout << "Text content extracted from PDF:" << endl;
cout << textContent << endl;
} else {
cout << "Failed to open output file." << endl;
}
在上述範例程式碼中,我們打開outputFile
(由pdftotext
生成的文字檔案),逐行讀取其內容,並將其存儲在textContent
字串中。 最後,我們關閉檔案,並在控制台上列印提取的文本內容。
如果您不需要可編輯的輸出文本檔或想要釋放硬碟空間,可以在程式結束之前使用以下命令刪除它:
remove(outputFilePath.c_str());
remove(outputFilePath.c_str());
使用「Ctrl+F9」快捷鍵編譯程式碼。 編譯成功後,執行可執行檔將從指定的 PDF 文件中提取文本內容並在控制台顯示。 輸出如下:
IronPDF .NET C# 庫 是一個強大的 .NET C# PDF 庫,允許使用者在其 C# 應用程式中輕鬆查看 PDF 文件。 IronPDF 利用 Chromium 網路瀏覽器引擎,精確地渲染和顯示 PDF 內容,包括圖片、字體和複雜格式。 憑藉其用戶友好的介面和廣泛的功能,開發人員可以將IronPDF無縫整合到他們的C#專案中,使用戶能夠高效且互動地查看PDF文件。 無論是用於顯示報告、發票或任何其他 PDF 內容,IronPDF 提供了一個強大的解決方案,用於在 C# 中創建功能豐富的 PDF 查看器。
要在 Visual Studio 中安裝 IronPDF NuGet 套件,請按照以下步驟進行:
開啟 Visual Studio:啟動 Visual Studio 或任何您偏好的其他 IDE。
創建或打開您的專案:創建一個新的 C# 專案或打開一個現有的專案,並在其中安裝 IronPDF 套件。
開啟 NuGet 套件管理器:在 Visual Studio 中,依次選擇「工具」>「NuGet 套件管理器」>「管理方案的 NuGet 套件」。 或者,點擊解決方案總管,然後選擇「管理解決方案的 NuGet 套件」。
搜尋 IronPDF: 在「NuGet 套件管理員」視窗中,點擊「瀏覽」標籤,然後在搜尋欄中搜尋「IronPDF」。 或者,造訪NuGet IronPDF 套件,直接下載最新版本的"IronPDF"。
選擇 IronPDF 套件:找到「IronPDF」套件,然後點擊以選擇它作為您的專案。
安裝 IronPDF:點擊「安裝」按鈕以安裝選擇的套件。
:ProductInstall
:ProductInstall
使用 IronPDF,我們可以執行操作,例如從 PDF 文件中提取文本和圖像並在控制台中顯示以供查看。 以下程式碼有助於完成此任務:
using IronPdf;
using IronSoftware.Drawing;
using System.Collections.Generic;
// Extracting Image and Text content from Pdf Documents
// open a 128-bit encrypted PDF
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Get all text to put in a search index
string text = pdf.ExtractAllText();
// Get all Images
var allImages = pdf.ExtractAllImages();
// Or even find the precise text and images for each page in the document
for (var index = 0 ; index < pdf.PageCount ; index++)
{
int pageNumber = index + 1;
text = pdf.ExtractTextFromPage(index);
List<AnyBitmap> images = pdf.ExtractBitmapsFromPage(index);
//...
}
using IronPdf;
using IronSoftware.Drawing;
using System.Collections.Generic;
// Extracting Image and Text content from Pdf Documents
// open a 128-bit encrypted PDF
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Get all text to put in a search index
string text = pdf.ExtractAllText();
// Get all Images
var allImages = pdf.ExtractAllImages();
// Or even find the precise text and images for each page in the document
for (var index = 0 ; index < pdf.PageCount ; index++)
{
int pageNumber = index + 1;
text = pdf.ExtractTextFromPage(index);
List<AnyBitmap> images = pdf.ExtractBitmapsFromPage(index);
//...
}
Imports IronPdf
Imports IronSoftware.Drawing
Imports System.Collections.Generic
' Extracting Image and Text content from Pdf Documents
' open a 128-bit encrypted PDF
Private pdf = PdfDocument.FromFile("encrypted.pdf", "password")
' Get all text to put in a search index
Private text As String = pdf.ExtractAllText()
' Get all Images
Private allImages = pdf.ExtractAllImages()
' Or even find the precise text and images for each page in the document
For index = 0 To pdf.PageCount - 1
Dim pageNumber As Integer = index + 1
text = pdf.ExtractTextFromPage(index)
Dim images As List(Of AnyBitmap) = pdf.ExtractBitmapsFromPage(index)
'...
Next index
如需有關 IronPDF 的更詳細資訊,請造訪 IronPDF 文件。
在本文中,我們學習了如何使用 Xpdf 命令行工具在 C++ 中提取和查看 PDF 文件的內容。 此方法使我們能夠在 C++ 應用程式中無縫處理和分析提取的文本。
IronPDF 授權資訊 可免費用於開發目的,但生成的 PDF 帶有浮水印。 若要移除浮水印並將 IronPDF 用於商業用途,您可以購買許可證。
也提供免費試用授權以供商業用途測試。