Free C++ Library to Render PDF Pages to Image & Extract Text.
Open Source C++ PDF Processing Library for Rendering PDF Pages to Images, Extracting Text, Work with Password Protected PDFs, and Access Document Metadata via C++ API.
What is PDFium-Render?
Handling and rendering PDFs within a C++ application has historically been a complex challenge, often involving expensive commercial licenses or cumbersome open-source tools. Enter PDFium-Render, a modern, header-only C++ wrapper library that brings the power of Google's PDFium engine to your projects with an intuitive and clean interface. PDFium-render is a safe, idiomatic Rust binding for PDFium, the same PDF rendering engine that powers Google Chrome and Mozilla Firefox. Created and maintained by ajrcarey, this library provides developers with a powerful toolkit for rendering PDF pages to images, extracting text, manipulating PDF structures, and accessing document metadata—all while leveraging Rust's memory safety guarantees.
Unlike many PDF libraries that focus solely on creation or basic parsing, PDFium-Render excels at rendering PDF content with pixel-perfect accuracy, making it ideal for applications that need to display, convert, or process existing PDF documents. It supports various basic and advanced features for working with PDF documents, such as rendering PDF files and pages, text extraction and analysis, work with document metadata, access and manipulate page objects, working with password-protected PDFs, and may more. One of pdfium-render's greatest strengths is its cross-platform support. The library works seamlessly on Windows, macOS, and Linux, abstracting away platform-specific differences in PDFium binaries. Whether you're building a document viewer, automating PDF processing workflows, or extracting data from PDFs, pdfium-render provides the tools you need.
Getting Started with PDFium-Render
TThe recommend way to install PDFium-Render is using CMake. Please use the following command for a smooth installation.
Install PDFium-Render via GitHub
vcpkg install pdfium-render
Install PDFium-Render via GitHub
Git clone https://github.com/ajrcarey/pdfium-render.git You can also install it manually; download the latest release files directly from GitHub repository.
High-Fidelity PDF Rendering via C++
The open source PDFium-Render library's primary strength lies in its rendering capabilities. It can convert PDF pages into raster images with exceptional quality, supporting various output formats including PNG, JPEG, and BMP. The rendering engine handles complex PDF features like transparency, gradients, embedded fonts, and vector graphics with the same accuracy you'd expect from Chrome's PDF viewer. Here is simple example that shows how software developers can render a specific page into a PNG image format inside C++ applications.
How to Render a Specific PDF Page into PNG Format via C++?
use pdfium_render::prelude::*;
fn render_pdf_page() -> Result<(), PdfiumError> {
// Initialize PDFium library
let pdfium = Pdfium::new(
Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
.or_else(|_| Pdfium::bind_to_system_library())?
);
// Load a PDF document
let document = pdfium.load_pdf_from_file("sample.pdf", None)?;
// Get the first page
let page = document.pages().get(0)?;
// Render the page to a bitmap at 300 DPI
let bitmap = page.render_with_config(
&PdfRenderConfig::new()
.set_target_width(2480) // 300 DPI for 8.27 inches
.set_target_height(3508) // 300 DPI for 11.69 inches
)?;
// Save as PNG
bitmap.as_image().as_rgba8().unwrap()
.save("output.png")?;
Ok(())
}
Text Extraction and Analysis via C++ Library
Beyond rendering, the PDFium-Render library provides comprehensive text extraction capabilities. The text extraction API is intuitive and powerful. Software developers can extract text from individual pages, search for specific strings, and even access character-level positioning information with just a couple of lines of code —crucial for implementing features like text selection or highlighting. The following example shows, how software developers can extract text from specific PDF page regions inside C++ applications.
How to Extract Text from Specific PDF Page Regions via C++ API?
use pdfium_render::prelude::*;
fn extract_text_from_page() -> Result<(), PdfiumError> {
let pdfium = Pdfium::new(
Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
.or_else(|_| Pdfium::bind_to_system_library())?
);
let document = pdfium.load_pdf_from_file("document.pdf", None)?;
let page = document.pages().get(0)?;
// Extract all text from the page
let text = page.text()?;
let all_text = text.all();
println!("Page text: {}", all_text);
// Search for specific text
let search_results = text.search("important", true, false)?;
for result in search_results {
println!("Found at index: {}", result.start_index());
}
// Get text in a specific rectangular area
let rect = PdfRect::new(100.0, 100.0, 300.0, 200.0);
let bounded_text = text.bounded(rect)?;
println!("Text in region: {}", bounded_text);
Ok(())
}
Word with Password-Protected PDF via C++
Security is paramount when working with documents. PDFium-Render handles password-protected PDFs seamlessly, supporting both user and owner passwords. The library handles authentication transparently, returning appropriate errors if the password is incorrect or if the PDF has restrictions that prevent certain operations. Here is a very useful example that shows how to handle password protect PDF documents inside C++ applications.
How to Open and Load Password Protect PDF Documents via C++ API?
fn open_encrypted_pdf() -> Result<(), PdfiumError> {
let pdfium = Pdfium::new(
Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
.or_else(|_| Pdfium::bind_to_system_library())?
);
// Attempt to load with password
let document = pdfium.load_pdf_from_file(
"confidential.pdf",
Some("secret_password")
)?;
println!("Successfully opened encrypted PDF");
println!("Pages: {}", document.pages().len());
Ok(())
}
Working PDF Metadata and Properties via C++
Understanding a PDF's structure is essential for many applications. The open source PDFium-Render library provides complete access to document metadata, page counts, dimensions, and other properties. This functionality is invaluable for building document management systems, PDF validators, or any application that needs to categorize or process PDFs based on their properties. The following code example demonstrates, how programmers can work with PDF documents metadata and access various properties with ease.
How to Word with PDF Documents Metadata via C++ API?
fn inspect_document_metadata() -> Result<(), PdfiumError> {
let pdfium = Pdfium::new(
Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
.or_else(|_| Pdfium::bind_to_system_library())?
);
let document = pdfium.load_pdf_from_file("report.pdf", None)?;
// Access document metadata
let metadata = document.metadata();
println!("Title: {}", metadata.title().unwrap_or("Untitled"));
println!("Author: {}", metadata.author().unwrap_or("Unknown"));
println!("Creator: {}", metadata.creator().unwrap_or("Unknown"));
println!("Producer: {}", metadata.producer().unwrap_or("Unknown"));
// Get page count
let page_count = document.pages().len();
println!("Total pages: {}", page_count);
// Inspect individual page properties
for (index, page) in document.pages().iter().enumerate() {
let width = page.width();
let height = page.height();
println!("Page {}: {}x{} points", index + 1, width.value, height.value);
}
Ok(())
}