Document
Overview
Document is a class in Aspose.PDF FOSS for Python.
Pythonic wrapper for PDF document lifecycle and core operations.
This class provides 35 methods for working with Document objects in Python programs.
Available methods include: __init__, add_attachment, auto_tag, change_passwords, check, close, compress_streams, convert_to_pdfa, convert_to_pdfua, decrypt, dispose, encrypt, and 23 additional methods.
All public members are accessible to any Python application after installing the Aspose.PDF FOSS for Python package.
Properties: attachments, embedded_files, file_name, form, id, info, and 10 more.
Properties
| Name | Type | Access | Description |
|---|---|---|---|
load_limits | PdfLoadLimits | Read | Return limits used for loading, lazy processing, and authored assets. |
pages | PageCollection | Read | Get the collection of pages. |
form | Form | Read | Get the interactive form of the document. |
tagged_content | TaggedContent | Read | Get an editable view of the document’s tagged structure tree. |
attachments | `` | Read | Get the collection of attachments in the document. |
embedded_files | list[FileSpecification] | Read | The document’s embedded files as typed :class:FileSpecification. |
page_count | int | Read | Return the current number of pages. |
info | dict[str, str] | Read/Write | Get or set the document metadata (info dictionary). |
xmp_metadata | XmpPacket | Read/Write | Get or set the document’s XMP metadata packet (catalog /Metadata). |
is_encrypted | bool | Read | Return True if document is encrypted. |
id | list[bytes] | None | Read | Return the two-element file-identifier array from the PDF trailer. |
version | str | Read/Write | PDF version string as it appears in the file header (e.g. |
outlines | OutlineCollection | Read | Bookmark tree for this document. |
permissions | int | Read | Access-permission flags (PDF /P value). |
is_pdfua_compliant | bool | Read | Heuristic PDF/UA catalog structure check (tagged PDF shell). |
file_name | str | None | Read | Gets the file name. |
Methods
| Signature | Description |
|---|---|
__init__(source: str | Path | bytes | bytearray | BinaryIO | None, options: Any, password: str | None, limits: PdfLoadLimits | None) | Create an empty document, or load source when one is supplied. |
add_attachment(name: str, content: bytes, mime: str | None, description: str | None, creation_date, mod_date, compress: bool) → Document | Embed content as a document-level file attachment named name. |
get_embedded_file(name: str) → FileSpecification | None | Return the embedded file named name as a :class:FileSpecification, |
or ``None`` when the document has no attachment with that name. |
| sync_metadata(direction: str) → Document | Synchronise the /Info dictionary and the XMP metadata packet. |
| open_streaming(path: str \| Path, password: str \| None, limits: PdfLoadLimits \| None) → Document | Open a PDF in streaming/lazy mode for memory-efficient page processing. |
| iter_pages() → Iterator[Page] | Iterate over the pages of the document one at a time. |
| iter_page_content_streams() → Generator[bytes, None, None] | Yield decoded content stream bytes for each page, one at a time. |
| render_page(page_index: int, dpi: float, scale: float, background: tuple[int, int, int], antialias: bool \| int) → RasterizedPage | Render a page to an RGB raster image. |
| save_page_as_image(page_index: int, destination: str \| Path, dpi: float, scale: float, background: tuple[int, int, int], antialias: bool \| int) → Path | Render a page and save it as .png or .tif/.tiff. |
| replace_text(search: str, replacement: str, page_index: int \| None, case_sensitive: bool, max_count: int) → int | Replace existing text in simple page-content text-showing operands. |
| redact_text(search: str, page_index: int \| None, case_sensitive: bool, max_count: int, overlay: bool, overlay_color: Sequence[float]) → int | Remove existing text from simple page-content text-showing operands. |
| load_from(source: str \| bytes \| bytearray \| Path \| BinaryIO, password: str \| None, limits: PdfLoadLimits \| None) → Document | Load a PDF from a file path, raw bytes, or a binary stream. |
| optimize(options: OptimizationOptions \| None, compress_streams: bool) → Document | Process the document and remove unused resources. |
| optimize_resources(options: OptimizationOptions \| None) → Document | Alias for :meth:optimize. |
| compress_streams() → Document | Compress uncompressed document streams. |
| is_pdfa_compliant(level: str) → bool | Check if the document complies with the specified PDF/A level. |
| validate_pdfa(level: str) → PdfAValidationResult | Validate the document against PDF/A standards (heuristic checks). |
| validate_pdfua() → PdfUaValidationResult | Validate catalog-level PDF/UA prerequisites (heuristic). |
| convert_to_pdfa(level: str, font_lookup_directory: str \| Path \| None) → list[str] | Convert the document to PDF/A format in-place. |
| convert_to_pdfua(language: str, title: str \| None, auto_tag: bool) → list[str] | Add the catalog-level PDF/UA prerequisites to the document in place. |
| auto_tag(image_alt: str \| Callable[[str], str] \| None) → int | Heuristically tag existing page content into the structure tree. |
| save(destination: str \| Path \| BinaryIO, save_format: Any, overwrite: bool) → Document | Save the document to a file path or a binary stream. |
| dispose() | Release the document and underlying engine resources (primary lifecycle API). |
| close() | Alias of :meth:dispose (matches .NET Close). |
| merge() → Document | Merge the supplied documents into this one. |
| encrypt(user_password: str, owner_password: str \| None, permissions: int) → Document | Encrypt the PDF document. |
| decrypt(password: str) → Document | Decrypt the PDF document. |
| change_passwords(old_password: str, new_user_password: str, new_owner_password: str \| None) → Document | Change document passwords. |
| validate() → bool | Validate the PDF document. |
| check() → bool | Check PDF integrity. |
| repair() → Document | Attempt to repair the PDF document. |
| flatten() → Document | Flatten annotations and forms. |
| generate_appearances(force: bool) → int | Synthesise missing annotation appearance streams across all pages. |
| generate_field_appearances() → int | Regenerate AcroForm field appearance streams from their values. |
| free_memory() → Document | Free memory by clearing caches. |