Skip to main content

Crate pdf_manip

Crate pdf_manip 

Source
Expand description

PDF manipulation: pages, encryption, watermarks, text replacement, and more.

All operations work on lopdf::Document objects, which can be loaded from a file (Document::load) or obtained from another pdf-manip call. Most functions return Result<_, ManipError>.

This crate’s public API is panic-free. Errors are returned as Result<T, ManipError>.

Note on page numbering: all page indices throughout this crate are 1-based to match PDF conventions.

§Quick Start

use lopdf::Document;
use pdf_manip::pages;
use pdf_manip::encrypt::{EncryptionAlgorithm, encrypt_and_save, EncryptConfig, Permissions};

// Merge two PDFs.
let mut merged = pages::merge(&["a.pdf", "b.pdf"]).unwrap();
merged.save("merged.pdf").unwrap();

// Extract pages 1–3 from a document.
let doc = Document::load("report.pdf").unwrap();
let mut subset = pages::extract_pages(&doc, &[1, 2, 3]).unwrap();
subset.save("pages_1_to_3.pdf").unwrap();

// Encrypt with AES-256.
let mut doc = Document::load("sensitive.pdf").unwrap();
let cfg = EncryptConfig {
    owner_password: b"owner_pw".to_vec(),
    user_password: b"user_pw".to_vec(),
    algorithm: EncryptionAlgorithm::Aes256,
    permissions: Permissions::allow_all(),
};
let out = std::fs::File::create("sensitive_enc.pdf").unwrap();
encrypt_and_save(&mut doc, &cfg, out).unwrap();

§Modules

Functions are accessed through their sub-modules rather than re-exports:

ModuleOperations
pagesmerge, split, extract_pages, delete_pages, insert_pages, rotate_pages
encryptencrypt_document, remove_encryption, AES-256 / RC4
watermarkText and image watermarks
optimizeStream compression, object deduplication
bookmarksRead and write document outline / bookmarks
text_replaceSearch-and-replace in content streams
header_footerAdd headers and footers to pages

Re-exports§

pub use content_editor::ContentEditor;
pub use content_editor::GraphicsSnapshot;
pub use content_editor::GraphicsStateTracker;
pub use error::ManipError;
pub use error::Result;
pub use text_run::FontMap;
pub use text_run::TextRun;

Modules§

bookmarks
PDF bookmarks (outlines) — read, create, modify, import/export.
cff_append
Append a single blank glyph to a CFF (Type1C) font program.
clock
Wall-clock time that is safe on wasm32, where SystemTime::now() panics. Wall-clock time that works on every target we ship to.
content_editor
Content stream round-trip: parse, modify, serialize.
downsample
Image downsampling for PDF optimization.
embed_font
Embed a TrueType font and make it usable from page content.
encrypt
PDF encryption, decryption, and password protection.
error
Error types for pdf-manip operations.
font_subset
Font subsetting for embedded PDF fonts.
header_footer
Headers, footers, and Bates numbering for PDF pages.
image_edit
Edit images that are already placed on a page: list them, move or resize one, swap its picture, remove it.
image_insert
Insert JPEG/PNG images into PDF pages as Image XObjects.
optimize
PDF compression and optimization.
pages
Page manipulation: merge, split, insert, delete, rearrange, rotate, crop.
pdfa
PDF/A conversion: the whole pipeline, in one place.
pdfa_cleanup
PDF/A cleanup: remove JavaScript, EmbeddedFiles, transparency, and other PDF/A-incompatible elements.
pdfa_colorspace
PDF/A color space normalization and OutputIntent management.
pdfa_fixups
Supplementary PDF/A compliance fixes.
pdfa_fonts
PDF/A font embedding and subsetting.
pdfa_structure
PDF/A structure and hierarchy fixes.
pdfa_subset
Conservative glyph-preserving font storage compaction.
pdfa_xmp
PDF/A XMP metadata repair and generation.
pdfua
PDF/UA remediation helpers.
text_edit
Layout-aware text editing: find → stage → commit (Phase 1B engine).
text_replace
Text replacement in PDF content streams (legacy convenience API).
text_run
Text run identification from content streams with CMap-aware decoding.
text_style
Text-run style mutations: bold and italic via embedded font variant swaps.
unicode_font
Embedding a caller-supplied Unicode font, so a replacement can contain characters the document’s own fonts have no glyph for. Embedding a caller-supplied Unicode font as a Type0 composite font, so a replacement can contain characters the original font has no glyph for.
watermark
Text and image watermarking for PDF pages.