在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
PDF 文件是一种广泛使用的文档交换格式,因为它能够在不同平台上保留格式。 在各种应用程序中,通过编程读取 PDF 文件的内容变得非常重要。
在本文中,我们将学习如何使用Xpdf
命令行工具在C++中查看PDF文件中的文本。 Xpdf
提供了一套用于处理PDF文件的命令行工具和C++库,包括文本提取功能。 通过将Xpdf
集成到我们的C++ PDF查看程序中,我们可以高效地查看PDF文件中的文本内容,并以编程方式处理它。
Xpdf
- C++ 库和命令行工具Xpdf 是一个开源软件套件,提供用于处理 PDF 文件的一系列工具和库。 它包括各种命令行实用程序和 C++ 库,可实现与 PDF 相关的功能,如解析、渲染、打印和文本提取。 Xpdf 的命令行工具还提供了直接从终端查看 PDF 文件的方法。
Xpdf的关键组件之一是pdftotext
,主要用于从PDF文件中提取文本内容。 然而,当与其他工具如pdftops
和pdfimages
结合使用时,Xpdf
允许用户以不同的方式查看PDF内容。 pdftotext
工具在从 PDF 中提取文本信息以供进一步处理或分析方面非常有价值,并且它提供了选项来指定要从哪些页面提取文本。
在我们开始之前,请确保您具备以下先决条件:
系统中已安装 GCC 或 Clang 等 C++ 编译器。 我们将为此使用Code::Blocks IDE。
打开 Code::Blocks:在您的计算机上启动 Code::Blocks 集成开发环境。
创建一个新项目:从顶部菜单中点击“文件”,然后从下拉菜单中选择“新建”。 然后,点击子菜单中的 "项目"。
选择项目类型:在“从模板新建”窗口中,选择“控制台应用程序”,然后点击“Go”。 然后选择语言 "C/C++",点击 "下一步"。
输入项目详细信息:在“项目标题”字段中,为您的项目命名(例如,“PDFViewer”)。 选择保存项目文件的位置,然后单击 "下一步"。
首先,让我们将所需的头文件添加到我们的main.cpp文件中:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
string pdfPath = "input.pdf";
string outputFilePath = "output.txt";
在main
函数中,我们声明了两个字符串:pdfPath
和outputFilePath
。 pdfPath
存储输入 PDF 文件的路径,而 outputFilePath
存储提取文本作为纯文本文件保存的路径。
输入文件如下:
pdftotext
命令string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
string command = "pdftotext " + pdfPath + " " + outputFilePath;
int status = system(command.c_str());
在这里,我们使用pdfPath
和outputFilePath
变量构建了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;
}
我们检查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;
}
在上述示例代码中,我们打开outputFile
(由pdftotext
生成的文本文件),逐行读取其内容,并将其存储在textContent
字符串中。 最后,我们关闭文件并在控制台上打印提取的文本内容。
如果您不需要可编辑的输出文本文件或希望释放磁盘空间,只需在程序结束时使用以下命令删除它,然后再结束主函数:
remove(outputFilePath.c_str());
remove(outputFilePath.c_str());
使用 "Ctrl+F9 "快捷键构建代码。 编译成功后,运行可执行文件将从指定的 PDF 文档中提取文本内容并显示在控制台上。 输出结果如下
IronPDF .NET C# 库 是一个强大的 .NET C# PDF 库,使用户能够在他们的 C# 应用程序中轻松查看 PDF 文件。 IronPDF 利用 Chromium 网页浏览器引擎,可准确渲染和显示 PDF 内容,包括图像、字体和复杂格式。 凭借其友好的用户界面和丰富的功能,开发人员可以将 IronPDF 无缝集成到他们的 C# 项目中,使用户能够高效、交互式地查看 PDF 文档。 无论是用于显示报告、发票还是任何其他 PDF 内容,IronPDF 都能提供一个强大的解决方案,用于在 C# 中创建功能丰富的 PDF 查看器。
要在 Visual Studio 中安装 IronPdf NuGet 包,请按照以下步骤操作:
打开 Visual Studio: 启动 Visual Studio 或任何其他您偏好的集成开发环境 (IDE)。
创建或打开您的项目:创建一个新的C#项目,或者打开一个现有项目以安装IronPDF包。
打开 NuGet 包管理器:在 Visual Studio 中,依次选择“工具”>“NuGet 包管理器”>“为解决方案管理 NuGet 包”。 或者,点击解决方案资源管理器,然后选择 "管理解决方案的 NuGet 包"。
搜索 IronPDF: 在“NuGet 包管理器”窗口中,点击“浏览”标签,然后在搜索栏中搜索“IronPDF”。 或者,访问 NuGet IronPDF Package 直接下载最新版本的“IronPDF”。
选择IronPDF包:找到“IronPDF”包并点击它以将其选择用于您的项目。
安装 IronPDF:点击“安装”按钮以安装所选软件包。
:ProductInstall
:ProductInstall
使用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
欲了解有关IronPDF的详细信息,请访问IronPDF文档。
在本文中,我们学习了如何使用 Xpdf 命令行工具在 C++ 中提取和查看 PDF 文档的内容。 这种方法使我们能够在 C# 应用程序中无缝处理和分析提取的文本。
IronPDF 许可信息 可免费用于开发目的,但生成的 PDF 带有水印。 要去除水印并将 IronPDF 用于商业目的,您可以购买许可证。
此外,还提供免费试用许可证用于商业用途测试。