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

相关推荐
_OP_CHEN1 小时前
Linux网络编程:(八)GCC/G++ 编译器完全指南:从编译原理到实战优化,手把手教你玩转 C/C++ 编译
linux·运维·c++·编译和链接·gcc/g++·编译优化·静态链接与动态链接
大锦终2 小时前
【动规】背包问题
c++·算法·动态规划
犯困的土子哥2 小时前
C++:哈希表
c++·哈希算法
Code Warrior2 小时前
【Linux】Socket 编程预备知识
linux·网络·c++
智者知已应修善业3 小时前
【c语言蓝桥杯计算卡片题】2023-2-12
c语言·c++·经验分享·笔记·算法·蓝桥杯
littlepeanut.top3 小时前
C++中将FlatBuffers序列化为JSON
开发语言·c++·json·flatbuffers
hansang_IR3 小时前
【题解】洛谷 P2330 [SCOI2005] 繁忙的都市 [生成树]
c++·算法·最小生成树
FMRbpm4 小时前
链表中出现的问题
数据结构·c++·算法·链表·新手入门
Elias不吃糖4 小时前
NebulaChat项目构建笔记
linux·c++·笔记·多线程
Alberta ゙5 小时前
C++初阶
开发语言·c++