当前位置: 首页 > news >正文

从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安装

参考: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

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 安装失败问题

下载: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源根目录下运行:

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

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

vcpkg install openssl:x64-windows
vcpkg install libxml2:x64-windows

libxml2的安装时间极长,超过半小时。
以上步骤,部分需要外网。

podofo\examples\helloworld\helloworld.cpp编译

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

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,内容如下:

#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

在这里插入图片描述
提取文本的示例代码

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

相关文章:

  • 应用分层简介
  • 压缩Docker虚拟磁盘空间CMD命令
  • ssm框架之mybatis框架动态SQL
  • Modern C++处理 Hooks 机制
  • 解决Java多张图合成JPG时出现红色前景及多列自适应适配
  • 《基于Spring Boot+Vue的智慧养老系统的设计与实现》开题报告
  • CAN及CANFD协议
  • 第四届光学与机器视觉国际学术会议(ICOMV 2025)
  • RabbitMQ 和 Redis 的选择
  • ssm框架之mybatis框架搭建
  • 牛客周赛 Round 85(DE)
  • 蓝桥杯备赛(基础语法4)
  • Shell 脚本中的 `read` 命令:灵活处理用户输入
  • 20. Excel 自动化:Excel 对象模型
  • 【NeurIPS-2022】CodeFormer: 将人脸复原转化为码本预测以减少LQ-HQ映射的不确定性
  • 基于ssm学科竞赛小程序的设计及实现(源码+lw+部署文档+讲解),源码可白嫖!
  • 使用Flux查询数据
  • (6)用于无GPS导航的Nooploop
  • [原创](Modern C++)现代C++的关键性概念: 灵活多变的绑定: std::bind
  • 化学工业领域 - 石油化工、高分子化工、生物化工极简理解
  • 病重老人取钱在银行门口去世,家属:已协商一致
  • 租车订单时隔7年从花呗免密扣费?“GoFun出行”引质疑
  • 习近平复信中国丹麦商会负责人
  • 当番茄霸总遇上晋江古言,短剧IP小变局
  • 秘鲁总统任命前司法部长阿拉纳为新总理
  • 为何选择上海?两家外企提到营商环境、人才资源……