Skip to main content

Essential Operations

This guide covers the essential operations you'll use in every PDFDancer workflow. Examples progress from simple to complex, introducing new patterns step by step.


Opening a PDF Document

The simplest way to open a PDF:

from pdfdancer import PDFDancer

with PDFDancer.open("document.pdf") as pdf:
# Your operations here
pass

The with statement automatically closes the connection when done.

Opening from bytes:

from pathlib import Path

pdf_bytes = Path("document.pdf").read_bytes()
with PDFDancer.open(pdf_data=pdf_bytes) as pdf:
pass

Working with Pages

Access a specific page:

page = pdf.page(1)

Iterate through all pages:

for page in pdf.pages():
print(f"Page ID: {page.internal_id}")

Delete a page:

pdf.page(3).delete()
pdf.save("output.pdf")

Saving PDFs

Save to a file:

pdf.save("output.pdf")

Automatically creates parent directories if needed.

Get PDF as bytes (for streaming, S3 upload, etc.):

pdf_bytes = pdf.get_bytes()
s3_client.put_object(Bucket='my-bucket', Key='output.pdf', Body=pdf_bytes)

Editing Existing Content

Find all paragraphs on a page:

paragraphs = pdf.page(1).select_paragraphs()

Find paragraphs by text prefix:

headers = pdf.page(1).select_paragraphs_starting_with("Invoice #")

Replace text (simplest edit):

if headers:
headers[0].edit().replace("Invoice #12345").apply()

Add styling while editing:

from pdfdancer import Color

headers[0].edit() \
.replace("Invoice #12345") \
.font("Helvetica-Bold", 14) \
.color(Color(255, 0, 0)) \
.apply()

Adding New Content

Add a simple paragraph:

pdf.new_paragraph() \
.text("Hello World") \
.at(page_number=1, x=100, y=500) \
.add()

Add a paragraph with styling:

from pdfdancer import Color

pdf.new_paragraph() \
.text("Hello World") \
.font("Helvetica", 12) \
.color(Color(0, 0, 0)) \
.line_spacing(1.5) \
.at(page_number=1, x=100, y=500) \
.add()

Add an image:

pdf.new_image() \
.from_file("logo.png") \
.at(page=1, x=50, y=700) \
.add()

Working with Forms

Find all form fields on a page:

fields = pdf.page(1).select_form_fields()

Find form fields by name:

name_fields = pdf.page(1).select_form_fields_by_name("firstName")

Fill a form field:

if name_fields:
name_fields[0].edit().value("John Doe").apply()

Next Steps