跳至頁尾內容
PDF 工具

如何在 C++ 中查看 PDF 文件

PDF 文件因其能夠在不同平台上保持格式一致,而被廣泛用於文件交換。 在各種應用中,以程式方式讀取 PDF 檔案的內容變得非常有價值。

在本文中,我們將學習如何使用Xpdf命令列工具在 C++ 中檢視 PDF 檔案中的文字。 Xpdf提供了一套用於處理 PDF 檔案的命令列實用程式和 C++ 庫,包括文字擷取。 透過將Xpdf整合到我們的 C++ PDF 檢視器程式中,我們可以有效率地查看 PDF 文件中的文字內容並以程式設計方式對其進行處理。

Xpdf - C++ 函式庫和命令列工具

Xpdf是一個開源軟體套件,提供了一系列用於處理 PDF 文件的工具和庫。 它包含各種命令列實用程式和 C++ 程式庫,可實現與 PDF 相關的功能,例如解析、渲染、列印和文字擷取。 Xpdf 的命令列工具也提供了直接從終端機查看 PDF 檔案的方法。

Xpdf 的關鍵元件之一是pdftotext ,它主要用於從 PDF 文件中提取文字內容。 但是,當與pdftopspdfimages等其他工具結合使用時, Xpdf允許使用者以不同的方式查看 PDF 內容。 pdftotext工具對於從 PDF 中提取文字資訊以進行進一步處理或分析非常有用,它提供了指定要從中提取文字的頁面的選項。

先決條件

在開始之前,請確保您已具備以下先決條件:

  1. 您的系統上已安裝 C++ 編譯器,例如 GCC 或 Clang。 我們將使用Code::Blocks IDE來實現這一目標。
  2. Xpdf 命令列工具已安裝並可透過命令列存取。下載 Xpdf並安裝適合您環境的版本。 之後,將 Xpdf 的 bin 目錄設定到系統環境變數路徑中,以便從檔案系統上的任何位置存取它。

建立 PDF 檢視器項目

1.開啟 Code::Blocks:在您的電腦上啟動 Code::Blocks IDE。 2.建立新項目:點擊頂部選單中的"檔案",然後從下拉式選單中選擇"新建"。 然後,從子選單中點選"項目"。 3.選擇項目類型:在"從範本新建"視窗中,選擇"控制台應用程式",然後按一下"開始"。 然後選擇語言"C/C++",並按一下"下一步"。 4.輸入項目詳情:在"項目標題"欄位中,為您的項目命名(例如,"PDF檢視器")。 選擇要儲存項目文件的位置,然後按一下"下一步"。 5.選擇編譯器:選擇您要用於專案的編譯器。 預設情況下,Code::Blocks 應該會自動偵測系統上可用的編譯器。 如果還沒有合適的編譯器,請從清單中選擇合適的編譯器,然後按一下"完成"。

C++中檢視PDF文字的步驟

包含必要的頭部資訊

首先,讓我們將所需的頭檔新增到main.cpp檔案中:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std; // Use standard namespace for convenience
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std; // Use standard namespace for convenience
C++

設定輸入輸出路徑

string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
C++

main函數中,我們宣告了兩個字串: pdfPathoutputFilePathpdfPath儲存輸入 PDF 檔案的路徑, outputFilePath儲存提取的文字將儲存為純文字檔案的路徑。

輸入檔如下:

如何在 C++ 中查看 PDF 檔案:圖 1

執行pdftotext命令

// Construct the command to execute pdftotext with input and output paths
string command = "pdftotext " + pdfPath + " " + outputFilePath;

// Execute the command using system function and capture the status
int status = system(command.c_str());
// Construct the command to execute pdftotext with input and output paths
string command = "pdftotext " + pdfPath + " " + outputFilePath;

// Execute the command using system function and capture the status
int status = system(command.c_str());
C++

在這裡,我們使用pdfPathoutputFilePath變數建立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; 
}
C++

我們檢查status變量,看看pdftotext指令是否成功執行。 如果status等於 0,則表示文字擷取成功,我們將列印成功訊息。 如果status不為零,則表示發生錯誤,我們將列印錯誤訊息。

讀取提取的文字並顯示

// Open the output file to read the extracted text
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) 
{ 
    string textContent;
    string line;
    while (getline(outputFile, line)) 
    {
        textContent += line + "\n"; // Concatenate each line to the text content
    }
    outputFile.close();
    cout << "Text content extracted from PDF:" << endl;
    cout << textContent << endl;
} 
else 
{
    cout << "Failed to open output file." << endl;
}
// Open the output file to read the extracted text
ifstream outputFile(outputFilePath);
if (outputFile.is_open()) 
{ 
    string textContent;
    string line;
    while (getline(outputFile, line)) 
    {
        textContent += line + "\n"; // Concatenate each line to the text content
    }
    outputFile.close();
    cout << "Text content extracted from PDF:" << endl;
    cout << textContent << endl;
} 
else 
{
    cout << "Failed to open output file." << endl;
}
C++

在上面的範例程式碼中,我們打開outputFilepdftotext產生的文字檔案),逐行讀取其內容,並將其儲存在textContent字串中。 最後,我們關閉文件並將提取的文字內容列印到控制台。

刪除輸出文件

如果您不需要可編輯的輸出文字文件,或者想要釋放磁碟空間,只需在程式結束時,在結束主函數之前使用以下命令將其刪除:

// Remove the output file to free up disk space and if output is not needed
remove(outputFilePath.c_str());
// Remove the output file to free up disk space and if output is not needed
remove(outputFilePath.c_str());
C++

編譯和運行程式

使用快速鍵"Ctrl+F9"產生程式碼。 編譯成功後,執行該執行檔將從指定的 PDF 文件中提取文字內容並將其顯示在控制台上。 輸出內容如下:

如何在 C++ 中查看 PDF 檔案:圖 2

在 C# 中查看 PDF 文件

IronPDF .NET C# 程式庫是一個功能強大的 .NET C# PDF 程式庫,它允許使用者在其 C# 應用程式中輕鬆查看 PDF 文件。 IronPDF 利用 Chromium 網頁瀏覽器引擎,能夠精確地渲染和顯示 PDF 內容,包括圖像、字體和複雜的格式。 IronPDF 擁有用戶友好的介面和豐富的功能,開發人員可以將其無縫整合到 C# 專案中,使用戶能夠有效且互動地查看 PDF 文件。 無論是顯示報告、發票或任何其他 PDF 內容,IronPDF 都提供了一個強大的解決方案,用於在 C# 中建立功能豐富的 PDF 檢視器。

若要在 Visual Studio 中安裝 IronPDF NuGet 套件,請依照下列步驟操作:

1.開啟 Visual Studio:啟動 Visual Studio 或您喜歡的任何其他 IDE。 2.建立或開啟您的專案:建立一個新的 C# 專案或開啟一個現有的項目,以便安裝 IronPDF 套件。 3.開啟 NuGet 套件管理員:在 Visual Studio 中,前往"工具">"NuGet 套件管理員">"管理解決方案的 NuGet 套件"。 或者,按一下解決方案資源管理器,然後選擇"管理解決方案的 NuGet 套件"。 4.搜尋 IronPDF:在"NuGet 套件管理員"視窗中,按一下"瀏覽"標籤,然後在搜尋列中搜尋"IronPDF"。 或者,請造訪NuGet IronPDF 套件並直接下載最新版本的"IronPDF"。 5.選擇 IronPDF 包:找到"IronPDF"包,點擊選擇它用於您的專案。 6.安裝 IronPDF:點選"安裝"按鈕安裝所選軟體包。

  1. 不過,您也可以使用 NuGet 套件管理器控制台,透過以下指令安裝 IronPDF:

    Install-Package IronPdf

使用 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);
    // Further processing here...
}
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);
    // Further processing here...
}
$vbLabelText   $csharpLabel

有關 IronPDF 的更多詳細信息,請訪問IronPDF 文件

結論

在本文中,我們學習如何使用 Xpdf 命令列工具在 C++ 中擷取和檢視 PDF 文件的內容。 這種方法使我們能夠在 C++ 應用程式中無縫地處理和分析提取的文字。

我們提供免費試用許可證,可用於商業用途測試。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。