PDF 工具

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

發佈 2023年8月2日
分享:

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. 建立一個新專案: 點擊頂部選單中的「File」,然後從下拉選單中選擇「New」。接著從子選單中點擊「Project」。

  3. 選擇專案類型: 在「New from template」視窗中,選擇「Console application」,然後點擊「Go」。接著選擇語言「C/C++」,然後點擊「Next」。

  4. 輸入專案詳細資料: 在「Project title」欄位中,為你的專案命名。 (例如,“PDFViewer”)選擇要儲存專案檔案的位置,然後點擊「下一步」。

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

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

包含必要的標頭文件

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

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

設定輸入和輸出路徑

string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   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());
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   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; 
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   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;
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

在上述範例程式碼中,我們打開 outputFile (由 pdftotext 生成的文本文件),逐行读取其内容,并将其存储在 textContent 字符串中。最后,我们关闭文件,并在控制台上打印提取的文本内容。

刪除輸出文件

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

remove(outputFilePath.c_str());
remove(outputFilePath.c_str());
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

編譯與運行程式

使用「Ctrl+F9」快捷鍵來構建程式碼。成功編譯後,運行可執行文件將從指定的PDF文件中提取文本內容並顯示在控制台上。輸出如下:

如何在C++中查看PDF文件:圖2

在 C# 中查看 PDF 文件

IronPDF 是一個強大的 .NET C# PDF 庫,允許用戶在 C# 應用程序中輕鬆查看 PDF 文件。IronPDF 利用 Chromium 網頁瀏覽器引擎,準確地渲染和顯示 PDF 內容,包括圖片、字體和複雜的格式。憑藉其用戶友好的界面和豐富的功能,開發人員可以將 IronPDF 無縫集成到他們的 C# 項目中,使用戶能夠高效且互動地查看 PDF 文檔。無論是顯示報告、發票或任何其他 PDF 內容,IronPDF 為創建功能豐富的 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"。

  5. 選擇 IronPDF 套件: 找到 "IronPDF" 套件,然後點擊它以將其選為你的項目。

  6. 安裝 IronPDF: 點擊 "Install" 按鈕以安裝選中的套件。

  7. 不過,你也可以使用 NuGet 套件管理控制台安裝 IronPDF,使用以下命令:
    :ProductInstall

使用IronPDF,我們可以 提取文字和圖片 並在控制台中顯示以供查看。以下代碼有助於完成此任務:

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
VB   C#

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

結論

在本文中,我們學會了如何使用 Xpdf 命令行工具在 C++ 中提取和查看 PDF 文件的內容。這種方法允許我們在 C++ 應用程序中無縫地處理和分析所提取的文本。

IronPDF 在開發過程中可以免費使用,但生成的 PDF 帶有浮水印。如需去除浮水印並將 IronPDF 用於商業用途,您可以購買 許可證.

.

A 免費試用授權 也可用於商業目的測試。

< 上一頁
如何在NodeJS中將PDF轉換為圖像
下一個 >
如何在C++中創建PDF檔案

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >