PDF 工具

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

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

在本文中,我們將學習如何使用Xpdf命令行工具在 C++ 中查看 PDF 文件中的文本。 Xpdf 提供了一套命令行工具和 C++ 庫,用於處理 PDF 文件,包括文本提取。 透過將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. 選擇專案類型: 在「從範本新增」視窗中,選擇「主控台應用程式」,然後點擊「Go」。 然後選擇語言「C/C++」,然後點擊「下一步」。

  4. 輸入專案詳情:在「專案名稱」欄位,為您的專案命名(例如,「PDFViewer」)。 選擇您想要儲存專案檔案的位置,然後點擊「下一步」。

  5. 選擇編譯器:選擇您希望用於專案的編譯器。 Code::Blocks 預設應該會自動偵測到您系統上可用的編譯器。 如果不是,請從列表中選擇合適的編譯器,然後點擊「完成」。

在 C++ 中查看 PDF 文本的步骤

包含必要的標頭

首先,讓我們將所需的標頭檔添加到main.cpp檔案中:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
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 命令

string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
string command = "pdftotext " + pdfPath + " " + outputFilePath;
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非零,則表示發生錯誤,並且我們會打印錯誤訊息。

讀取提取的文本並顯示

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;
}
C++

在上述範例程式碼中,我們打開outputFile(由pdftotext生成的文字檔案),逐行讀取其內容,並將其存儲在textContent字串中。 最後,我們關閉檔案,並在控制台上列印提取的文本內容。

移除輸出檔案

如果您不需要可編輯的輸出文本檔或想要釋放硬碟空間,可以在程式結束之前使用以下命令刪除它:

remove(outputFilePath.c_str());
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:點擊「安裝」按鈕以安裝選擇的套件。

  7. 但是,您也可以使用以下命令通過 NuGet 套件管理器控制台安裝 IronPDF:
    :ProductInstall
    :ProductInstall
SHELL

使用 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
$vbLabelText   $csharpLabel

如需有關 IronPDF 的更詳細資訊,請造訪 IronPDF 文件

結論

在本文中,我們學習了如何使用 Xpdf 命令行工具在 C++ 中提取和查看 PDF 文件的內容。 此方法使我們能夠在 C++ 應用程式中無縫處理和分析提取的文本。

IronPDF 授權資訊 可免費用於開發目的,但生成的 PDF 帶有浮水印。 若要移除浮水印並將 IronPDF 用於商業用途,您可以購買許可證。

也提供免費試用授權以供商業用途測試。

Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
如何在NodeJS中將PDF轉換為圖像
下一個 >
如何在C++中創建PDF檔案