PDF 工具

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

发布 2023年八月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.系统中已安装 GCC 或 Clang 等 C++ 编译器。我们将使用 代码::块 为此,IDE

2.安装 Xpdf 命令行工具,并可通过命令行访问。 下载 并安装适合你环境的 Xpdf 版本。然后,在系统环境变量路径中设置 Xpdf 的 bin 目录,以便从文件系统的任何位置访问它。

创建 PDF 阅读器项目

  1. 打开 Code::Blocks: 在计算机上启动 Code::Blocks 集成开发环境。

  2. 创建新项目: 点击顶部菜单中的 "文件",然后从下拉菜单中选择 "新建"。然后从子菜单中选择 "项目"。

  3. 选择项目类型: 在 "从模板新建 "窗口中选择 "控制台应用程序",然后点击 "开始"。然后选择语言 "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>
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 函数中,我们声明了两个字符串:pdfPath "和 "outputFilePath"。pdfPath "存储输入 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#

在上述示例代码中,我们打开了输出文件 (生成的文本文件),逐行读取其内容,并将其存储到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&num 中查看 PDF 文件;

IronPDF IronPDF 是一款功能强大的 .NET C# PDF 库,可让用户在其 C# 应用程序中轻松查看 PDF 文件。利用 Chromium 网页浏览器引擎,IronPDF 能准确地渲染和显示 PDF 内容,包括图像、字体和复杂格式。凭借其友好的用户界面和丰富的功能,开发人员可以将 IronPDF 无缝集成到他们的 C# 项目中,使用户能够高效、交互式地查看 PDF 文档。无论是显示报告、发票还是其他任何 PDF 内容,IronPDF 都能为在 C&num 中创建功能丰富的 PDF 查看器提供强大的解决方案;

要在 Visual Studio 中安装 IronPDF NuGet 包,请按以下步骤操作:

  1. 打开 Visual Studio: 启动 Visual Studio 或你喜欢的任何其他集成开发环境。

  2. 创建或打开项目: 创建一个新的 C# 项目或打开一个现有项目,在其中安装 IronPDF 软件包。

  3. 打开 NuGet 包管理器: 在 Visual Studio 中,转到 "工具">"NuGet 包管理器">"管理解决方案的 NuGet 包"。或者,点击解决方案资源管理器,然后选择 "管理解决方案的 NuGet 包"。

  4. 搜索 IronPDF: 在 "NuGet 包管理器 "窗口中,点击 "浏览 "选项卡,然后在搜索栏中搜索 "IronPDF"。或者,访问 NuGet 网站 并直接下载最新版本的 "IronPDF"。

  5. 选择 IronPDF 软件包: 找到 "IronPDF "软件包并点击,为你的项目选择它。

  6. 安装 IronPDF: 点击 "安装 "按钮安装所选软件包。

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 IronPDF 可免费用于开发目的,但生成的 PDF 文件带有水印。要去除水印并将 IronPDF 用于商业用途,可以购买 许可证.

A 免费试用许可证 也可用于商业测试。

< 前一页
如何在NodeJS中将PDF转换为图像
下一步 >
如何在 C++ 中创建 PDF 文件

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >