从pdf提取文本数据的c/cpp库(非OCR)

Aspose.PDF for C++

商业付费版,无源码。

功能强大,支持多种PDF操作。

对应的官方示例代码:Aspose.PDF-for-C

Spire.PDF for C++

商业付费版

对应的官方示例代码:Spire.PDF-for-C-

PDFTron SDK

商业付费版

PoDoFo

开源

当前版本:Version 0.10.4 on Sep 13, 2024

文档:https://podofo.github.io/podofo/documentation/

win下编译

按照 vcpkg 快速入门指南首先设置包管理器仓库。在 Windows 中,将环境变量 VCPKG_DEFAULT_TRIPLET 设置为 x64-windows 以默认安装 64 位依赖项,并定义一个 VCPKG_INSTALLATION_ROOT 变量,指定快速入门中创建的仓库位置可能也很有用。

vcpkg安装

c 复制代码
参考:https://learn.microsoft.com/zh-cn/vcpkg/get_started/get-started-vs?pivots=shell-powershell
git clone https://github.com/microsoft/vcpkg.git
https://github.com/microsoft/vcpkg/archive/refs/tags/2025.02.14.zip // 可以直接下载
cd vcpkg; .\bootstrap-vcpkg.bat

//PowerShell
$env:VCPKG_ROOT="C:\code\vcpkg-2025.02.14"
$env:PATH="$env:VCPKG_ROOT;$env:PATH"

在powershell中确认并设置VCPKG_INSTALLATION_ROOT

c 复制代码
PS C:\Users\s> $env:VCPKG_DEFAULT_TRIPLET
PS C:\Users\s> $env:VCPKG_DEFAULT_TRIPLET = "x64-windows"
PS C:\Users\s> $env:VCPKG_DEFAULT_TRIPLET
x64-windows
PS C:\Users\s>

vcpkg install freetype 安装失败问题

c 复制代码
下载:https://sourceforge.net/projects/freetype/files/freetype2/2.13.3/freetype-2.13.3.tar.gz/download
解压
cd freetype-2.13.3
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=C:\code\freetype-2.13.3\install
cmake --build . --config Release
cmake --install .

将C:\code\freetype-2.13.3\install下的文件拷贝到

C:\code\vcpkg-2025.02.14\installed\x64-windows下对应目录

拷贝到C:\code\vcpkg-2025.02.14\downloads也许也可以。

在podofo源根目录下运行:

c 复制代码
vcpkg install fontconfig freetype libxml2 openssl libjpeg-turbo libpng tiff zlib
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=C:\code\vcpkg-2025.02.14\scripts\buildsystems\vcpkg.cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug

安装失败的话,继续安装以下库

c 复制代码
vcpkg install openssl:x64-windows
vcpkg install libxml2:x64-windows

libxml2的安装时间极长,超过半小时。

以上步骤,部分需要外网。

podofo\examples\helloworld\helloworld.cpp编译

podofo\examples\helloworld\CMakeLists.txt 改为以下代码

c 复制代码
cmake_minimum_required(VERSION 3.10)
project(helloworld)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_compile_options(${PODOFO_CFLAGS})
add_executable(helloworld helloworld.cpp)
include_directories(${CMAKE_SOURCE_DIR}/../../src)
link_directories(${CMAKE_SOURCE_DIR}/../../build/target/Debug)
find_library(PODOFO_LIB podofo PATHS ${CMAKE_SOURCE_DIR}/../../build/target/Debug REQUIRED)
find_library(PODOFO_PRIVATE_LIB podofo_private PATHS ${CMAKE_SOURCE_DIR}/../../build/target/Debug REQUIRED)
find_library(PODOFO_3RDPARTY_LIB podofo_3rdparty PATHS ${CMAKE_SOURCE_DIR}/../../build/target/Debug REQUIRED)
target_link_libraries(helloworld ${PODOFO_LIBRARIES} ${PODOFO_LIB} ${PODOFO_PRIVATE_LIB} ${PODOFO_3RDPARTY_LIB})

缺少的podofo\src\podofo\podofo_config.h,内容如下:

c 复制代码
#ifndef PODOFO_CONFIG_H
#define PODOFO_CONFIG_H

// Template filled out by CMake
#define PODOFO_VERSION_MAJOR 0
#define PODOFO_VERSION_MINOR 10
#define PODOFO_VERSION_PATCH 4

// PoDoFo configuration options

// Libraries
// #define PODOFO_HAVE_JPEG_LIB
// #define PODOFO_HAVE_PNG_LIB
// #define PODOFO_HAVE_TIFF_LIB
// #define PODOFO_HAVE_FONTCONFIG
// #define PODOFO_HAVE_WIN32GDI

#endif // PODOFO_CONFIG_H

提取文本的示例代码

c 复制代码
#include <podofo/podofo.h>
#include <iostream>

using namespace PoDoFo;

void ExtractTextFromPdf(const std::string& filePath) {
    PdfMemDocument document;
    document.Load(filePath.c_str());

    for (int i = 0; i < document.GetPageCount(); ++i) {
        PdfPage* page = document.GetPage(i);
        PdfContentsTokenizer tokenizer(page);

        const char* token = nullptr;
        EPdfContentsType type;
        while (tokenizer.ReadNext(type, token)) {
            if (type == ePdfContentsType_String) {
                std::cout << token << std::endl;
            }
        }
    }
}

int main() {
    ExtractTextFromPdf("example.pdf");
    return 0;
}

PDFium

https://github.com/chromium/pdfium

mupdf

https://github.com/ArtifexSoftware/mupdf

1.9K

Poppler

github

Xpdf

https://www.xpdfreader.com/

tesseract OCR

https://github.com/tesseract-ocr/tesseract

65.4k

推荐

如果你需要纯 C++ 实现,推荐 PoDoFo 或 Poppler。

如果你需要高性能渲染和文本提取,推荐 PDFium。

如果你需要轻量级解决方案,推荐 MuPDF 或 Xpdf。

如果你需要处理扫描版 PDF,推荐结合 Tesseract OCR。

相关推荐
小乐xiaole2 分钟前
蓝桥杯 2025 C++组 省 B 题解
c++·蓝桥杯·深度优先
Q1860000000018 分钟前
PDF解析示例代码学习
学习·pdf
晓纪同学28 分钟前
C++ Primer (第五版)-第十三章 拷贝控制
java·开发语言·c++
arriettyandray1 小时前
Qt/C++学习系列之QTreeWidget的简单使用记录
c++·qt·学习
jayson.h2 小时前
pdf解密程序
java·前端·pdf
zhaoyqcsdn2 小时前
C++对象池设计:从高频`new/delete`到性能飞跃的工业级解决方案
c++·经验分享·笔记
CppPlayer-程序员阿杜2 小时前
poll为什么使用poll_list链表结构而不是数组 - 深入内核源码分析
网络·c++·链表·list·poll
@hdd2 小时前
C++| 深入剖析std::list底层实现:链表结构与内存管理机制
c++·链表·list
东风西巷3 小时前
手机上的PDF精简版:随时随地享受阅读
学习·智能手机·pdf·软件需求
weixin_428498493 小时前
使用POCO库进行ZIP压缩和解压
c++