Skip to main content

pdf_manip/
lib.rs

1#![warn(missing_docs)]
2//! PDF manipulation: pages, encryption, watermarks, text replacement, and more.
3//!
4//! All operations work on [`lopdf::Document`] objects, which can be loaded
5//! from a file (`Document::load`) or obtained from another `pdf-manip` call.
6//! Most functions return [`Result<_, ManipError>`].
7//!
8//! This crate's public API is panic-free. Errors are returned as `Result<T, ManipError>`.
9//!
10//! **Note on page numbering:** all page indices throughout this crate are
11//! **1-based** to match PDF conventions.
12//!
13//! # Quick Start
14//!
15//! ```no_run
16//! use lopdf::Document;
17//! use pdf_manip::pages;
18//! use pdf_manip::encrypt::{EncryptionAlgorithm, encrypt_and_save, EncryptConfig, Permissions};
19//!
20//! // Merge two PDFs.
21//! let mut merged = pages::merge(&["a.pdf", "b.pdf"]).unwrap();
22//! merged.save("merged.pdf").unwrap();
23//!
24//! // Extract pages 1–3 from a document.
25//! let doc = Document::load("report.pdf").unwrap();
26//! let mut subset = pages::extract_pages(&doc, &[1, 2, 3]).unwrap();
27//! subset.save("pages_1_to_3.pdf").unwrap();
28//!
29//! // Encrypt with AES-256.
30//! let mut doc = Document::load("sensitive.pdf").unwrap();
31//! let cfg = EncryptConfig {
32//!     owner_password: b"owner_pw".to_vec(),
33//!     user_password: b"user_pw".to_vec(),
34//!     algorithm: EncryptionAlgorithm::Aes256,
35//!     permissions: Permissions::allow_all(),
36//! };
37//! let out = std::fs::File::create("sensitive_enc.pdf").unwrap();
38//! encrypt_and_save(&mut doc, &cfg, out).unwrap();
39//! ```
40//!
41//! # Modules
42//!
43//! Functions are accessed through their sub-modules rather than re-exports:
44//!
45//! | Module | Operations |
46//! |---|---|
47//! | [`pages`] | `merge`, `split`, `extract_pages`, `delete_pages`, `insert_pages`, `rotate_pages` |
48//! | [`encrypt`] | `encrypt_document`, `remove_encryption`, AES-256 / RC4 |
49//! | [`watermark`] | Text and image watermarks |
50//! | [`optimize`] | Stream compression, object deduplication |
51//! | [`bookmarks`] | Read and write document outline / bookmarks |
52//! | [`text_replace`] | Search-and-replace in content streams |
53//! | [`header_footer`] | Add headers and footers to pages |
54
55pub mod bookmarks;
56pub mod cff_append;
57pub mod content_editor;
58#[cfg(feature = "image-insert")]
59pub mod downsample;
60pub(crate) mod encoding_utils;
61pub mod encrypt;
62pub mod error;
63pub(crate) mod flate_decode;
64#[cfg(feature = "font-subset")]
65pub mod font_subset;
66pub mod header_footer;
67#[cfg(feature = "image-insert")]
68pub mod image_insert;
69pub mod optimize;
70pub mod pages;
71#[cfg(feature = "pdfa-convert")]
72pub mod pdfa;
73#[cfg(feature = "pdfa-convert")]
74pub mod pdfa_cleanup;
75#[cfg(feature = "pdfa-convert")]
76pub mod pdfa_colorspace;
77#[cfg(feature = "pdfa-convert")]
78pub mod pdfa_fixups;
79#[cfg(feature = "pdfa-convert")]
80pub mod pdfa_fonts;
81#[cfg(feature = "pdfa-convert")]
82pub(crate) mod pdfa_repair;
83#[cfg(feature = "pdfa-convert")]
84pub mod pdfa_structure;
85#[cfg(feature = "pdfa-convert")]
86pub mod pdfa_xmp;
87pub mod pdfua;
88pub mod text_edit;
89pub mod text_replace;
90pub mod text_run;
91pub mod text_style;
92pub mod watermark;
93
94pub use content_editor::{ContentEditor, GraphicsSnapshot, GraphicsStateTracker};
95pub use error::{ManipError, Result};
96pub use text_run::{FontMap, TextRun};