从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。

相关推荐
oioihoii2 分钟前
C++共享内存小白入门指南
java·c++·算法
布茹 ei ai4 分钟前
QtWeatherApp - 简单天气预报软件(C++ Qt6)(附源码)
开发语言·c++·qt·开源·开源项目·天气预报
Bruce_kaizy4 分钟前
c++图论————图的基本与遍历
c++·算法·图论
Zmm147258369_6 分钟前
好用的PC耐力板机构
c++
Code Slacker34 分钟前
LeetCode Hot100 —— 普通数组(面试纯背版)(五)
数据结构·c++·算法·leetcode·面试
秦苒&1 小时前
【C语言】详解数据类型和变量(一):数据类型介绍、 signed和unsigned、数据类型的取值范围、变量、强制类型转换
c语言·开发语言·c++·c#
智者知已应修善业1 小时前
【删除有序数组中的重复项 II之O(N)算法】2024-1-31
c语言·c++·经验分享·笔记·算法
爱装代码的小瓶子1 小时前
【c++进阶】C++11新特性:一切皆可{}初始化
开发语言·c++·visual studio
xiaoye-duck1 小时前
吃透C++类和对象(中):构造函数与析构函数深度解析
c++
AA陈超1 小时前
Lyra Starter Game 中 GameFeature 类(如 ShooterCore)的加载流程
c++·笔记·学习·ue5·虚幻引擎