Crate oxidize_pdf

Source
Expand description

§oxidize-pdf

A comprehensive, pure Rust PDF library for generation, parsing, and manipulation with zero external PDF dependencies.

§Features

  • PDF Generation: Create multi-page documents with text, graphics, and images
  • PDF Parsing: Complete parser supporting rendering and content extraction
  • PDF Operations: Split, merge, rotate, and extract pages
  • Text Extraction: Extract text with position and formatting information
  • Resource Access: Work with fonts, images, and other PDF resources
  • Pure Rust: No C dependencies or external libraries
  • 100% Native: Complete PDF implementation from scratch

§Quick Start

§Creating PDFs

use oxidize_pdf_core::{Document, Page, Font, Color, Result};

// Create a new document
let mut doc = Document::new();
doc.set_title("My PDF");

// Create a page
let mut page = Page::a4();

// Add text
page.text()
    .set_font(Font::Helvetica, 24.0)
    .at(50.0, 700.0)
    .write("Hello, PDF!")?;

// Add graphics
page.graphics()
    .set_fill_color(Color::rgb(0.0, 0.5, 1.0))
    .circle(300.0, 400.0, 50.0)
    .fill();

// Save the document
doc.add_page(page);
doc.save("output.pdf")?;

§Parsing PDFs

use oxidize_pdf_core::parser::{PdfDocument, PdfReader};

// Open and parse a PDF
let reader = PdfReader::open("document.pdf")?;
let document = PdfDocument::new(reader);

// Get document information
println!("Pages: {}", document.page_count()?);
println!("Version: {}", document.version()?);

// Process pages
for i in 0..document.page_count()? {
    let page = document.get_page(i)?;
    println!("Page {} size: {}x{} points", i+1, page.width(), page.height());
}

// Extract text
let text_pages = document.extract_text()?;
for (i, page_text) in text_pages.iter().enumerate() {
    println!("Page {} text: {}", i+1, page_text.text);
}

§Modules

§Generation Modules

  • document - PDF document creation and management
  • page - Page creation and layout
  • graphics - Vector graphics and images
  • text - Text rendering and flow
  • writer - Low-level PDF writing

§Parsing Modules

§Manipulation Modules

  • operations - PDF manipulation (split, merge, rotate)
  • [text::extraction] - Text extraction with positioning

§Examples

§Content Stream Processing

use oxidize_pdf_core::parser::{PdfDocument, PdfReader};
use oxidize_pdf_core::parser::content::{ContentParser, ContentOperation};

let reader = PdfReader::open("document.pdf")?;
let document = PdfDocument::new(reader);
let page = document.get_page(0)?;

// Get and parse content streams
let streams = page.content_streams_with_document(&document)?;
for stream in streams {
    let operations = ContentParser::parse(&stream)?;
     
    for op in operations {
        match op {
            ContentOperation::ShowText(text) => {
                println!("Text: {:?}", String::from_utf8_lossy(&text));
            }
            ContentOperation::SetFont(name, size) => {
                println!("Font: {} at {} pt", name, size);
            }
            ContentOperation::MoveTo(x, y) => {
                println!("Move to ({}, {})", x, y);
            }
            _ => {} // Handle other operations
        }
    }
}

§Resource Access

use oxidize_pdf_core::parser::{PdfDocument, PdfReader};

let reader = PdfReader::open("document.pdf")?;
let document = PdfDocument::new(reader);
let page = document.get_page(0)?;

// Access page resources
if let Some(resources) = page.get_resources() {
    // Check fonts
    if let Some(fonts) = resources.get("Font").and_then(|f| f.as_dict()) {
        for (name, _) in &fonts.0 {
            println!("Font resource: {}", name.as_str());
        }
    }
     
    // Check images/XObjects
    if let Some(xobjects) = resources.get("XObject").and_then(|x| x.as_dict()) {
        for (name, _) in &xobjects.0 {
            println!("XObject resource: {}", name.as_str());
        }
    }
}

Re-exports§

pub use document::Document;
pub use document::DocumentMetadata;
pub use error::OxidizePdfError;
pub use error::PdfError;
pub use error::Result;
pub use graphics::Color;
pub use graphics::GraphicsContext;
pub use graphics::Image;
pub use graphics::ImageColorSpace;
pub use graphics::ImageFormat;
pub use page::Margins;
pub use page::Page;
pub use text::measure_text;
pub use text::split_into_words;
pub use text::Font;
pub use text::FontFamily;
pub use text::TextAlign;
pub use text::TextContext;
pub use text::TextFlowContext;
pub use parser::ContentOperation;
pub use parser::ContentParser;
pub use parser::DocumentMetadata as ParsedDocumentMetadata;
pub use parser::ParsedPage;
pub use parser::PdfArray;
pub use parser::PdfDictionary;
pub use parser::PdfDocument;
pub use parser::PdfName;
pub use parser::PdfObject;
pub use parser::PdfReader;
pub use parser::PdfStream;
pub use parser::PdfString;
pub use operations::merge_pdfs;
pub use operations::rotate_pdf_pages;
pub use operations::split_pdf;

Modules§

document
error
graphics
objects
operations
PDF operations module
page
parser
PDF Parser Module - Complete PDF parsing and rendering support
pdf_version
Supported PDF versions
text
writer

Constants§

VERSION
Current version of oxidize-pdf