PDF 工具

如何在 C++ 中读取 PDF 文件

PDF(便携式文档格式)文件广泛用于文档交换,能够通过编程读取其内容在各种应用中都很有价值。 以下库可用来用 C++ 阅读 PDF:Poppler、Mupdf、Haru 免费 PDF 库、Xpdf 和 Qpdf。

本文将探讨如何使用 Xpdf 命令行工具在 C++ 中读取 PDF 文件。 Xpdf 提供一系列用于处理 PDF 文件的实用程序,包括提取文本内容。 通过将 Xpdf 集成到 C++ 程序中,我们可以从 PDF 文件中提取文本并进行编程处理。

Xpdf - 命令行工具

Xpdf 是一个开源软件套件,提供了一系列用于处理 PDF(便携文档格式)文件的工具和库。 Xpdf 套件包括多个命令行实用程序和 C++ 库,可实现各种 PDF 相关功能,如解析、渲染、文本提取等。 Xpdf的一些关键组件包括pdfimagespdftopspdfinfopdfimages。 在这里,我们将使用pdftotext来读取PDF文档。

pdftotext 是一个命令行工具,它从 PDF 文件中提取文本内容并将其输出为纯文本。 当您需要从 PDF 中提取文本信息进行进一步处理或分析时,该工具尤其有用。 使用选项,您还可以指定从哪一页或哪几页提取文本。

先决条件

要使 PDF 阅读器项目提取文本,我们需要具备以下先决条件:

  1. 系统中已安装 GCC 或 Clang 等 C++ 编译器。 您只需使用任何支持 C++ 编程的集成开发环境即可。

  2. 系统中已安装 Xpdf 命令行工具。 Xpdf 是一组 PDF 实用程序,可以从 Xpdf 网站获取。从 Xpdf 网站 下载。 在环境变量路径中设置 Xpdf 的 bin 目录,以便在任何地方使用命令行工具访问该目录。

用 C++ 阅读 PDF 文件格式的步骤

第 1 步 包括必要的标题

首先,让我们在顶部的 main.cpp 文件中添加必要的头文件:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <fstream>
C++

步骤 2 编写 C# 代码

让我们编写调用 Xpdf 命令行工具从 PDF 文档中提取文本内容的 C++ 代码。 我们将使用以下 input.pdf 文件:

如何在 C++ 中读取 PDF 文件:图 1

代码示例如下:

// Include C library
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main() {
    string pdfPath = "input.pdf";
    string outputFilePath = "output.txt";

    string command = "pdftotext " + pdfPath + " " + outputFilePath;
    int status = system(command.c_str());

    if (status == 0) {
        cout << "Text extraction successful." << endl;
    } else {
        cout << "Text extraction failed." << endl;
        return 1;
    }

    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 document:" << endl;
        cout << textContent << endl;
    } else {
        cout << "Failed to open output file." << endl;
        return 1;
    }

    return 0;
}
// Include C library
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main() {
    string pdfPath = "input.pdf";
    string outputFilePath = "output.txt";

    string command = "pdftotext " + pdfPath + " " + outputFilePath;
    int status = system(command.c_str());

    if (status == 0) {
        cout << "Text extraction successful." << endl;
    } else {
        cout << "Text extraction failed." << endl;
        return 1;
    }

    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 document:" << endl;
        cout << textContent << endl;
    } else {
        cout << "Failed to open output file." << endl;
        return 1;
    }

    return 0;
}
C++

代码解释

在上面的代码中,我们定义了pdfPath变量以保存输入PDF文件的路径。请确保将其替换为实际输入PDF文档的适当路径。

我们还定义了outputFilePath变量,以保存将由Xpdf生成的输出文本文件的路径。

该代码使用system函数执行pdftotext命令,将输入 PDF 文件路径和输出文本文件路径作为命令行参数传递。 status 变量捕获命令的退出状态。

如果pdftotext成功执行(状态为0),我们接着使用ifstream打开输出的文本文件。 然后,我们逐行读取文本内容,并将其存储在textContent字符串中。

最后,我们从生成的输出文件中将提取的文本内容输出到控制台。 如果您不需要可编辑的输出文本文件或希望释放磁盘空间,只需在程序结束时使用以下命令删除它,然后再结束主函数:

remove(outputFilePath.c_str());
remove(outputFilePath.c_str());
C++

步骤 3 编译和运行程序

编译 C++ 代码并运行可执行文件。 如果将pdftotext添加到环境变量系统路径中,其命令将成功执行。 程序生成输出文本文件,并从 PDF 文档中提取文本内容。 然后将提取的文本显示在控制台上。

产出如下

如何在 C++ 中读取 PDF 文件:图 2

用 C&num 阅读 PDF 文件

IronPDF 库

IronPDF C# Library Overview 是一个流行的C# PDF库,提供强大的功能来处理PDF文档。 它使开发人员能够以编程方式创建、编辑、修改和读取 PDF 文件。

使用 IronPDF 库阅读 PDF 文档是一个简单明了的过程。 该库提供各种方法和属性,使开发人员能够从 PDF 页面中提取文本、图像、元数据和其他数据。 提取的信息可用于进一步处理、分析或在应用程序中显示。

以下代码示例将使用IronPDF读取PDF文件

// Rendering PDF documents to Images or Thumbnails
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);
    //...
}
// Rendering PDF documents to Images or Thumbnails
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);
    //...
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

有关如何阅读PDF文档的更多详细信息,请访问IronPDF C# PDF阅读指南

结论

在本文中,我们学习了如何使用 Xpdf 命令行工具在 C++ 中读取 PDF 文档的内容。 通过将 Xpdf 集成到 C++ 程序中,我们可以在一秒钟内以编程方式从 PDF 文件中提取文本内容。 这种方法使我们能够在 C# 应用程序中处理和分析提取的文本。

探索 IronPDF 是一个强大的 C# 库,可以实现读取和操作 PDF 文件。 其丰富的功能、易用性和可靠的渲染引擎使其成为在 C#; 项目中使用 PDF 文档的开发人员的热门选择。

IronPDF在开发中是免费的,并提供免费试用以供商业使用。 除此之外,需要获得商业用途的许可

Chipego
软件工程师
Chipego 拥有出色的倾听技巧,这帮助他理解客户问题并提供智能解决方案。他在 2023 年加入 Iron Software 团队,此前他获得了信息技术学士学位。IronPDF 和 IronOCR 是 Chipego 主要专注的两个产品,但他对所有产品的了解每天都在增长,因为他不断找到支持客户的新方法。他喜欢 Iron Software 的合作氛围,公司各地的团队成员贡献他们丰富的经验,以提供有效的创新解决方案。当 Chipego 离开办公桌时,你经常可以发现他在看书或踢足球。
< 前一页
如何在 C++ 中创建 PDF 文件
下一步 >
如何在C++中将HTML转换为PDF