Quickstart

Quickstart

This page walks through the core create/modify/save/reload cycle: build a two-page PDF, attach an annotation to each page, generate the annotations' visible appearance, save the document, and reload it to confirm the changes survived the round trip.


Prerequisites

RequirementDetail
Python3.11 or later
Packageaspose-pdf-foss-for-python installed (Installation)

1. Create a document and add pages

Document() with no arguments creates an empty, in-memory PDF. document.pages is a mutable PageCollection; calling add() appends a blank page.

from aspose_pdf import Document

document = Document()
document.pages.add()
document.pages.add()

print("Page count:", document.page_count)

2. Add annotations and generate their appearance

Each page’s annotations property is an AnnotationCollection. add() takes a subtype name, a (left, bottom, right, top) rectangle, contents text, and an optional properties dict of PDF-name keyed values, returning the new Annotation. generate_appearance() renders the annotation’s visual content so it displays correctly in a PDF viewer.

document.pages[0].annotations.add("Square", (0, 0, 50, 50), "")

line = document.pages[1].annotations.add(
    "Line", (0, 0, 50, 50), "", properties={"L": [0, 0, 50, 50]}
)
line.generate_appearance()

3. Save and reload

Document.save() accepts a file path (or a writable stream). Document.load_from() reads a document back from a path, bytes, or stream.

document.save("quickstart.pdf")

reopened = Document()
reopened.load_from("quickstart.pdf")

print("Reloaded pages:", reopened.page_count)
print("Has appearance:", reopened.pages[1].annotations[0].has_appearance)

Next Steps

See Also