Aspose.PDF FOSS for Python Features

Aspose.PDF FOSS for Python Features

Aspose.PDF FOSS for Python Features

Aspose.PDF FOSS for Python is a pure-Python library for creating, opening, modifying, and saving PDF documents. This page surveys the major capability areas and the classes that act as their entry points. Each area is covered in depth on its own developer-guide page.


Document and Page Management

Document is the root object for every operation. Construct it with no arguments to start a new, empty document, or call load_from() to read an existing one from a path, bytes, or stream. document.pages exposes the mutable PageCollection, and Page.add_text() places positioned text on a page.

from aspose_pdf import Document

document = Document()
page = document.pages.add()
page.add_text("Hello from Aspose.PDF FOSS!", x=72, y=720, font_size=18)
document.save("hello.pdf")

Text Extraction and Search

PdfExtractor binds to a document and pulls text out page by page. bind_pdf() opens the source, extract_text() performs the extraction, and get_text() returns the accumulated result.

from aspose_pdf import PdfExtractor

extractor = PdfExtractor()
extractor.bind_pdf("hello.pdf")
extractor.extract_text()
print(extractor.get_text())
extractor.close()

For positioned, per-fragment search and replace, TextFragmentAbsorber exposes matched TextFragment objects with text_search_options controlling case sensitivity and regular-expression matching.


Page Rendering to Images

Document.save_page_as_image() renders a single page straight to a PNG or TIFF file at a configurable DPI. For lower-level access, Page.render() returns a RasterizedPage with raw pixel data through get_pixel() and pixels.

from aspose_pdf import Document

document = Document()
document.load_from("hello.pdf")
document.save_page_as_image(0, "page-1.png", dpi=150)

Forms and Interactive Fields

AcroForm fields are managed through Document.form, a Form facade. Form.add_text_field() (and the checkbox, radio, list, and combo box equivalents) creates a new field tied to a page and a widget rectangle, returning a Field. Form.fields lists every field currently on the document.

from aspose_pdf import Document

document = Document()
page = document.pages.add()
document.form.add_text_field("customer_name", page, (72, 700, 300, 720))

for field in document.form.fields:
    print(field.name, field.field_type)

document.save("form.pdf")

Security, Encryption, and Signatures

Document.encrypt() protects a document with a user password (needed to open it) and an owner password (needed to change permissions); decrypt() and change_passwords() manage protection on an already-open document. is_encrypted reports the current state.

from aspose_pdf import Document

document = Document()
document.load_from("hello.pdf")
document.encrypt(user_password="secret", owner_password="owner-secret")
document.save("encrypted.pdf")

Digital signature verification is handled by PdfSignature.validate(), which returns a ValidationResult describing trust and revocation status, and SigningUtils provides certificate and PKCS#7/CAdES signing helpers.


PDF/A and PDF/UA Compliance

Document.validate_pdfa() runs a heuristic PDF/A compliance check and returns a PdfAValidationResult; convert_to_pdfa() attempts to remediate the document toward that level. validate_pdfua() and auto_tag() provide the equivalent accessibility check and automatic structure-tree tagging for PDF/UA.

from aspose_pdf import Document

document = Document()
document.load_from("hello.pdf")

result = document.validate_pdfa("1b")
if not result.is_valid:
    document.convert_to_pdfa("1b")

document.auto_tag()
document.save("compliant.pdf")

Tips and Best Practices

  • Modify the object returned directly by document.pages, page.annotations, or document.form — edits made to a separate copy of a page or field are not reflected when you call save().
  • PageCollection is zero-indexed in this Python binding (document.pages[0] is the first page).
  • Catch InvalidPasswordException and PdfParseException before the broader AsposePdfException when calling load_from() on untrusted input, so you can react differently to a bad password than to a corrupted file.
  • Call document.optimize() or compress_streams() before save() on heavily edited documents to remove unused resources and shrink file size.
  • Pass a bounded PdfLoadLimits to load_from() when processing PDFs from an untrusted source, to cap memory and object counts during parsing.

Common Issues

IssueCauseFix
save() produces a file with unchanged editsEdits were made to a page, annotation, or field copy rather than the object obtained from document.pages, annotations, or formModify the object returned directly by those collections
InvalidPasswordException on load_from()The document is password-protected and no password, or the wrong one, was suppliedPass the correct password: document.load_from(path, password="...")
validate_pdfa() still reports a font error after convert_to_pdfa()No matching font file was found in the supplied font_lookup_directoryAdd the missing font to the lookup directory, or register it with FontRepository before converting
Rendered image looks emptyWrong page index passed to save_page_as_image() or render()pages is zero-indexed — confirm the index, and check whether PdfExtractor returns text for that page at all
Field added with Form.add_text_field() does not appear on the pageThe widget rectangle falls outside the page’s media_boxConfirm the rectangle coordinates are within Page.media_box

FAQ

Does Aspose.PDF FOSS for Python depend on a commercial PDF engine?

No. It is a from-scratch, pure-Python PDF engine released under the MIT license. Its only runtime dependencies are cryptography and asn1crypto, used for encryption and signature verification.

Can I build a PDF from scratch instead of opening an existing one?

Yes. Document() with no arguments creates an empty, in-memory document; call document.pages.add() to append pages before saving.

Which raster formats can I render a page to?

Page.render() and Document.save_page_as_image() produce PNG or TIFF raster output; the returned RasterizedPage also exposes raw pixel data through get_pixel() and the pixels property.

What exception should I catch for package-specific errors?

Catch AsposePdfException. Every package-specific error — including parsing (PdfParseException), password and encryption (PdfSecurityException, InvalidPasswordException), and validation (PdfValidationException) failures — derives from it.

Where do I go next?

Getting Started covers installation, licensing, and a first working example. This developer guide continues with document management topics such as forms, embedded file attachments, font discovery, and outline/bookmark authoring.


API Reference Summary

Class/MethodDescription
DocumentRoot object — create, load, save, and manage a PDF
Document.pagesThe document’s mutable PageCollection
Document.load_from() / Document.save()Read from or write to a path, bytes, or stream
Document.encrypt() / Document.decrypt()Password-protect or remove protection
Document.validate_pdfa() / Document.convert_to_pdfa()Heuristic PDF/A compliance check and conversion
Document.validate_pdfua() / Document.auto_tag()PDF/UA accessibility check and structure auto-tagging
Page.add_text()Add positioned text to a page
Page.render()Render a page to a RasterizedPage
PdfExtractorExtract page text and embedded attachments
PdfFileEditorConcatenate, split, insert, and delete pages across files
Form / FieldAcroForm container and individual form fields
PdfSignature / SigningUtilsDigital signature validation and creation
AsposePdfExceptionBase class for every package-specific exception

See Also