Adding Page Number to PDF with Rust
Aspose.PDF for Rust via C++ provides built-in functionality to modify PDF documents programmatically. In this example, the application opens an existing PDF file, applies automatic page numbering to every page, and saves the modified document under a new name.
This approach is useful when generating finalized documents for distribution, printing, or archival purposes. The process requires only a few lines of code and does not alter the original file unless explicitly overwritten.
Page numbering is a common requirement for reports, invoices, contracts, manuals, and other multi-page documents. The add_page_num() method automatically inserts page numbers into all pages of the document, ensuring consistent pagination across the file.
After adding page numbers, the updated document is saved as a new PDF file.
- Open the existing PDF document.
- Add page numbers with add_page_num() method.
- Save the updated document.
use asposepdf::Document;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open a PDF-document with filename
let pdf = Document::open("sample.pdf")?;
// Add page number to a PDF-document
pdf.add_page_num()?;
// Save the previously opened PDF-document with new filename
pdf.save_as("sample_add_page_num.pdf")?;
Ok(())
}