SimplePdf
Overview
SimplePdf is a class in Aspose.PDF FOSS for Python.
Native Python PDF document representation.
This class provides 101 methods for working with SimplePdf objects in Python programs.
Available methods include: add, add_annotation, add_image, add_image_to_page, add_page_break, add_password, add_signature, add_text_to_page, append, auto_tag, build_outline_item, change_passwords, and 86 additional methods.
All public members are accessible to any Python application after installing the Aspose.PDF FOSS for Python package.
Properties: O, P, U, attachment_meta, attachment_read_meta, attachments, and 26 more.
Properties
| Name | Type | Access | Description |
|---|---|---|---|
load_limits | PdfLoadLimits | Read | Return the loading, processing, and authoring limits for this document. |
page_count | int | Read | Gets the page count. |
xmp_packet | XmpPacket | Read/Write | The document’s XMP metadata as an :class:XmpPacket. |
supports_incremental_update | bool | Read | Check if document supports incremental updates. |
pages | list[tuple[float, float, float, float]] | Read/Write | Gets or sets the pages. |
page_contents | list[bytes] | Read/Write | Gets or sets the page contents. |
images | dict[str, bytes] | Read/Write | Gets or sets the images. |
metadata | dict[str, str] | Read/Write | Gets or sets the metadata. |
watermark_text | str | None | Read/Write | Gets or sets the watermark text. |
encrypted | bool | Read/Write | Gets or sets the encrypted. |
password | str | None | Read/Write | Gets or sets the password. |
O | bytes | None | Read/Write | Gets or sets the o. |
U | bytes | None | Read/Write | Gets or sets the u. |
P | int | Read/Write | Gets or sets the p. |
encryption_key | bytes | None | Read/Write | Gets or sets the encryption key. |
encryption_algorithm | str | Read/Write | Gets or sets the encryption algorithm. |
signature | dict[str, str] | None | Read/Write | Gets or sets the signature. |
signing_creds | tuple[Any, Any] | None | Read/Write | Gets or sets the signing creds. |
pades | bool | Read/Write | Gets or sets the pades. |
certify_permissions | int | None | Read/Write | Gets or sets the certify permissions. |
extra_certs | list[Any] | None | Read/Write | Gets or sets the extra certs. |
timestamp_url | str | None | Read/Write | Gets or sets the timestamp url. |
timestamp_tsa | tuple[Any, Any] | None | Read/Write | Gets or sets the timestamp tsa. |
timestamp_timeout | float | Read/Write | Gets or sets the timestamp timeout. |
attachments | dict[str, bytes] | Read/Write | Gets or sets the attachments. |
attachment_meta | dict[str, dict] | Read/Write | Gets or sets the attachment meta. |
attachment_read_meta | dict[str, dict] | Read/Write | Gets or sets the attachment read meta. |
fonts | dict[str, Any] | Read/Write | Gets or sets the fonts. |
extgstates | dict[str, Any] | Read/Write | Gets or sets the extgstates. |
signatures | list[PdfSignature] | Read/Write | Gets or sets the signatures. |
pdf_version | str | Read/Write | Gets or sets the pdf version. |
file_id | list[bytes] | None | Read/Write | Gets or sets the file id. |
Methods
| Signature | Description |
|---|---|
traverse(node_ref) | Calls traverse(node_ref) on this SimplePdf instance. |
get_page_rotation(page_index: int) → int | Return the page’s clockwise rotation in degrees (0/90/180/270). |
set_page_rotation(page_index: int, degrees: int) | Set the page’s /Rotate, normalised to [0, 360). |
get_page_crop_box(page_index: int) | Return the page’s /CropBox as an (x0, y0, x1, y1) tuple, or None. |
set_page_crop_box(page_index: int, rect) | Set the page’s /CropBox to rect (x0, y0, x1, y1). |
from_file(path: str | Path, password: str | None, limits: PdfLoadLimits | None) → SimplePdf | Load PDF from file path, using memory-mapping for large files. |
from_bytes(data: bytes | bytearray, password: str | None, limits: PdfLoadLimits | None, _budget: _LoadBudget | None) → SimplePdf | Parse PDF from raw bytes using PdfCosParser. |
load_from(source: str | Path | bytes | bytearray, limits: PdfLoadLimits | None) → SimplePdf | Load from file path or bytes. |
from_file_lazy(path: str | Path, password: str | None, limits: PdfLoadLimits | None) → SimplePdf | Open a PDF in streaming/lazy mode for memory-efficient page processing. |
get_page_content(index: int) → bytes | Return the decoded content stream for the page at index. |
iter_page_content_streams() | Yield decoded content streams for each page, one at a time. |
from_bytes_safe(data: bytes | bytearray, password: str | None, limits: PdfLoadLimits | None) → SimplePdf | Load PDF with automatic repair on error. |
load_cos(source: str | Path | bytes | bytearray, limits: PdfLoadLimits | None) → SimplePdf | Load PDF using the generic COS parser (preserves all data). |
merge() → SimplePdf | Merge multiple PDFs into one, resolving resource name collisions |
and deduplicating. |
| get_safe_name(old_name, data_hash) | Calls get_safe_name(old_name, data_hash) on this SimplePdf instance. |
| sign(signature: PdfSignature, output_path: str) | Sign the PDF document. |
| save(path: str \| Path) | Calls save(path) on this SimplePdf instance. |
| save_cos(path: str \| Path) | Save PDF using the generic COS writer (preserves all data). |
| make_page_ref(page_index: int) → PdfIndirectReference | Calls make_page_ref(page_index) on this SimplePdf instance. |
| build_outline_item(item: dict, parent_ref: PdfIndirectReference) → PdfIndirectReference \| None | Calls build_outline_item(item, parent_ref) on this SimplePdf instance. |
| to_bytes() → bytes | Serialize PDF to bytes, preserving structure when possible. |
| garbage_collect() → int | Remove unreachable COS objects from the document. |
| save_incremental(path: str \| Path) | Save PDF using incremental update to preserve signatures. |
| to_bytes_incremental() → bytes | Serialize PDF using incremental update to preserve signatures. |
| dispose() | Release resources, including memory-mapped backing storage. |
| free_memory() | Free memory by clearing caches. |
| close() | Close the document, releasing resources. |
| get_next_attachment() → tuple[str, bytes] | Return the next attachment name and its data, advancing the cursor. |
| encrypt(user_password: str, owner_password: str, permissions: int, algorithm: str) | Enable encryption for the document. |
| check_pdfa_compliance(level: str) → list[str] | Return the list of PDF/A compliance errors (heuristic). |
| check_pdfa_compliance_detailed(level: str) → tuple[list[str], list[str]] | Check the document against PDF/A standards (heuristic). |
| check_pdfua_compliance() → tuple[list[str], list[str]] | Inspect catalog-level PDF/UA prerequisites (heuristic only). |
| decrypt(password: str) | Decrypt the document with password. |
| add_password(password: str) | Add password protection. |
| remove_password() | Remove password protection. |
| change_passwords(old_password: str, new_user_password: str, new_owner_password: str \| None) | Change document passwords. |
| struct_elem(tag: str, parent: Any) → tuple[PdfDictionary, Any] | Calls struct_elem(tag, parent) on this SimplePdf instance. |
| auto_tag(image_alt: str \| Callable[[str], str] \| None) → int | Heuristically tag existing page content into the structure tree. |
| 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: tuple) → int | Remove existing text from simple page-content text-showing operands. |
| resolver(name: str) | Calls resolver(name) on this SimplePdf instance. |
| width_of(code: int, _w, _f, _m) → float | Calls width_of(code, _w, _f, _m) on this SimplePdf instance. |
| code_to_cid(code: bytes, _m) → int \| None | Calls code_to_cid(code, _m) on this SimplePdf instance. |
| code_to_cid(code: bytes, _m) → int \| None | Calls code_to_cid(code, _m) on this SimplePdf instance. |
| width_of(cid: int, _w, _dw) → float | Calls width_of(cid, _w, _dw) on this SimplePdf instance. |
| finite_vertical_metric(value: Any) → float \| None | Calls finite_vertical_metric(value) on this SimplePdf instance. |
| vertical_metrics_of(cid: int, _metrics, _w1y, _v1y, _width_of) → tuple[float, float, float] | Calls vertical_metrics_of(cid, _metrics, _w1y, _v1y, _width_of) on this SimplePdf instance. |
| resolver(name: str \| None) | Calls resolver(name) on this SimplePdf instance. |
| add_text_to_page(page_index: int, text: str, x: float, y: float, font_size: float, font_name: str \| None, font: FontDescriptor \| bytes \| bytearray \| str \| Path \| None, color: Sequence[float], tag: str \| None, actual_text: str \| None, layout: TextLayoutOptions \| None) | Append positioned text to a page content stream. |
| add_image_to_page(page_index: int, data: bytes, x: float, y: float, width: float \| None, height: float \| None, pixel_width: int \| None, pixel_height: int \| None, color_space: str, bits_per_component: int, name: str \| None, tag: str \| None, alt: str \| None, actual_text: str \| None) → str | Register an image XObject and append a placement operation to a page. |
| draw_rectangle_on_page(page_index: int, x: float, y: float, width: float, height: float, stroke_color: Sequence[float] \| None, fill_color: Sequence[float] \| None, line_width: float, tag: str \| None, alt: str \| None, actual_text: str \| None) | Append a rectangle path to a page content stream. |
| draw_line_on_page(page_index: int, x1: float, y1: float, x2: float, y2: float, stroke_color: Sequence[float], line_width: float, tag: str \| None, alt: str \| None, actual_text: str \| None) | Append a stroked line segment to a page content stream. |
| add_image(name: str, data: bytes) | Calls add_image(name, data) on this SimplePdf instance. |
| hide_image(name: str) | Calls hide_image(name) on this SimplePdf instance. |
| replace_image(name: str, data: bytes) | Calls replace_image(name, data) on this SimplePdf instance. |
| save_image(name: str, path: str \| Path, color_space: str \| None) → Path | Save an extracted image as a real, openable image file. |
| get_attach_names() → list[str] | Return list of attachment names. |
| has_next_attachment() → bool | Check if there are more attachments to iterate over. |
| extract_image() | Prepare image iterator. |
| has_next_image() → bool | Returns true if next image is present. |
| get_next_image() → tuple[str, bytes] | Returns the next image. |
| extract_text() → str | Extract plain text from page contents. |
| get_text() → str | Returns the text. |
| get_next_page_text() → str | Return text for next page, advancing cursor. |
| has_next_page_text() → bool | Returns true if next page text is present. |
| extract_pages(selection: Iterable[int] \| slice) → SimplePdf | Return new PDF with selected pages. |
| delete_pages(start_index: int, count: int) | Delete range of pages. |
| delete(index: int) | Delete a single page at index with COS synchronization. |
| insert_pages(index: int, new_pages: list[tuple[float, float, float, float]], new_contents: list[bytes] \| None) | Insert multiple pages. |
| add(page: Any) | Add a page to the end with COS synchronization. |
| insert(index: int, page: Any) | Insert a page at index with COS synchronization. |
| add_page_break() | Add a blank page. |
| generate_annotation_appearance(page_index: int, annot_index: int, force: bool) → bool | Synthesise an /AP /N appearance for one annotation. |
| generate_appearances(page_index: int \| None, force: bool) → int | Generate missing appearances across a page (or all pages). |
| get_annotations(page_index: int) → list[dict[str, Any]] | Get annotations for a page. |
| add_annotation(page_index: int, data: dict[str, Any]) | Add an annotation to a page. |
| insert_annotation(page_index: int, annot_index: int, data: dict[str, Any]) | Insert an annotation at a specific index on a page. |
| clear_annotations(page_index: int) | Remove all annotations from a page. |
| update_annotation(page_index: int, annot_index: int, data: dict[str, Any]) | Update an existing annotation. |
| delete_annotation(page_index: int, annot_index: int) | Delete an annotation from a page. |
| append(other: SimplePdf) | Append another PDF’s pages (with COS synchronization). |
| extract_attachment() | Prepare attachment iteration. |
| get_attachment(name: str) → bytes | Get attachment by name. |
| add_signature(reason: str, contact: str, location: str) | Calls add_signature(reason, contact, location) on this SimplePdf instance. |
| validate(max_depth: int) → bool | Validate PDF structure and integrity. |
| check() → bool | Check PDF integrity. |
| repair() → bool | Attempt to repair PDF structure issues. |
| optimize(options: OptimizationOptions \| None, compress_streams: bool) | Perform PDF optimization driven by options. |
| lookup(cid: int, _d: bytes) | Calls lookup(cid, _d) on this SimplePdf instance. |
| stream_key(stream: PdfStream) | Calls stream_key(stream) on this SimplePdf instance. |
| compress_streams(include_fonts: bool) | Compress uncompressed streams using FlateDecode. |
| optimize_resources(options: OptimizationOptions \| None) | Optimize embedded resources (alias of :meth:optimize). |
| flatten() | Flatten annotations and form fields into static page content. |
| get_form_fields() → dict[str, dict[str, Any]] | Extract all form fields with values and types. |
| create_form_field(name: str, field_type: str, widgets: Sequence[dict[str, Any]], value: Any, flags: int, options: Sequence[Any] \| None, default_appearance: str \| None, alignment: int, caption: str, on_value: str) | Create a terminal AcroForm field and its widget annotations. |
| remove_form_field(name: str) → bool | Remove one terminal field and its widget annotations. |
| set_field_value(name: str, value: Any) | Set the value of a form field by name. |
| generate_field_appearances(drop_need_appearances: bool) → int | Regenerate /AP appearance streams for AcroForm fields from their values. |
| convert_to_pdfa(level: str, font_lookup_directory: str \| Path \| None) → list[str] | Convert this 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 this document in place. |
| set_watermark(text: str) | Set watermark text. |