在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
PDF(便携式文档格式)文件广泛用于文档交换,能够通过编程读取其内容在各种应用中都很有价值。 以下库可用来用 C++ 阅读 PDF:Poppler、Mupdf、Haru 免费 PDF 库、Xpdf 和 Qpdf。
本文将探讨如何使用 Xpdf 命令行工具在 C++ 中读取 PDF 文件。 Xpdf 提供一系列用于处理 PDF 文件的实用程序,包括提取文本内容。 通过将 Xpdf 集成到 C++ 程序中,我们可以从 PDF 文件中提取文本并进行编程处理。
Xpdf 是一个开源软件套件,提供了一系列用于处理 PDF(便携文档格式)文件的工具和库。 Xpdf 套件包括多个命令行实用程序和 C++ 库,可实现各种 PDF 相关功能,如解析、渲染、文本提取等。 Xpdf的一些关键组件包括pdfimages、pdftops、pdfinfo和pdfimages。 在这里,我们将使用pdftotext
来读取PDF文档。
pdftotext
是一个命令行工具,它从 PDF 文件中提取文本内容并将其输出为纯文本。 当您需要从 PDF 中提取文本信息进行进一步处理或分析时,该工具尤其有用。 使用选项,您还可以指定从哪一页或哪几页提取文本。
要使 PDF 阅读器项目提取文本,我们需要具备以下先决条件:
系统中已安装 GCC 或 Clang 等 C++ 编译器。 您只需使用任何支持 C++ 编程的集成开发环境即可。
首先,让我们在顶部的 main.cpp 文件中添加必要的头文件:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <fstream>
让我们编写调用 Xpdf 命令行工具从 PDF 文档中提取文本内容的 C++ 代码。 我们将使用以下 input.pdf 文件:
代码示例如下:
// 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;
}
在上面的代码中,我们定义了pdfPath
变量以保存输入PDF文件的路径。请确保将其替换为实际输入PDF文档的适当路径。
我们还定义了outputFilePath
变量,以保存将由Xpdf生成的输出文本文件的路径。
该代码使用system
函数执行pdftotext
命令,将输入 PDF 文件路径和输出文本文件路径作为命令行参数传递。 status
变量捕获命令的退出状态。
如果pdftotext
成功执行(状态为0),我们接着使用ifstream
打开输出的文本文件。 然后,我们逐行读取文本内容,并将其存储在textContent
字符串中。
最后,我们从生成的输出文件中将提取的文本内容输出到控制台。 如果您不需要可编辑的输出文本文件或希望释放磁盘空间,只需在程序结束时使用以下命令删除它,然后再结束主函数:
remove(outputFilePath.c_str());
remove(outputFilePath.c_str());
编译 C++ 代码并运行可执行文件。 如果将pdftotext
添加到环境变量系统路径中,其命令将成功执行。 程序生成输出文本文件,并从 PDF 文档中提取文本内容。 然后将提取的文本显示在控制台上。
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
有关如何阅读PDF文档的更多详细信息,请访问IronPDF C# PDF阅读指南。
在本文中,我们学习了如何使用 Xpdf 命令行工具在 C++ 中读取 PDF 文档的内容。 通过将 Xpdf 集成到 C++ 程序中,我们可以在一秒钟内以编程方式从 PDF 文件中提取文本内容。 这种方法使我们能够在 C# 应用程序中处理和分析提取的文本。
探索 IronPDF 是一个强大的 C# 库,可以实现读取和操作 PDF 文件。 其丰富的功能、易用性和可靠的渲染引擎使其成为在 C#; 项目中使用 PDF 文档的开发人员的热门选择。
IronPDF在开发中是免费的,并提供免费试用以供商业使用。 除此之外,需要获得商业用途的许可。