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。