Skip to main content
Version: API v2 Preview

Working with Vector Graphics

PDF vector graphics are represented as paths. API v2 provides typed selected paths plus dedicated path, line, rectangle, and Bézier builders.

Complete path-editing workflow

Download paths.pdf as input.pdf. The program changes the first path's stroke color and saves output.pdf.

from pathlib import Path
from pdfdancer import Color, PDFDancer

with PDFDancer.open(Path("input.pdf")) as pdf:
paths = pdf.page(1).select_paths()
if not paths:
raise RuntimeError("The sample path was not found")
result = paths[0].edit().stroke_color(Color(220, 30, 30)).apply()
if not result.success:
raise RuntimeError(result.message)
pdf.save("output.pdf")

Open output.pdf and confirm that the first path has a red stroke. The remaining examples assume an open pdf session.

Draw a filled rectangle

from pdfdancer import Color

pdf.page(1).new_rectangle() \
.at_coordinates(72, 500) \
.with_size(220, 80) \
.stroke_color(Color(0, 0, 0)) \
.fill_color(Color(255, 245, 190)) \
.stroke_width(1.5) \
.add()

Edit an existing path

Select a path, open its edit session, set the desired appearance, and apply once. The edit-session method names follow each language's normal casing. Check the returned CommandResult before saving.

from pdfdancer import Color

path = pdf.page(1).select_path_at(72, 500, tolerance=1.0)
if path is None:
raise RuntimeError("Path not found")
result = path.edit().stroke_color(Color(220, 30, 30)).fill_color(Color(255, 230, 230)).apply()
if not result.success:
raise RuntimeError(result.message)

Create paths

  • A line builder configures two endpoints, stroke, and width.
  • A rectangle builder configures origin, dimensions, stroke, and optional fill.
  • A Bézier builder configures endpoints and two control points.
  • A path builder supports cursor operations, multiple segments, closing, rectangles, circles, dash patterns, and solid fills.

A circle is a path-builder convenience, not a separate public builder type.

Draw lines and Bézier curves

from pdfdancer import Color

line_added = pdf.page(1).new_line() \
.from_point(20, 40).to_point(120, 40) \
.stroke_color(Color(0, 0, 0)).stroke_width(2) \
.dash_pattern([4, 2]).add()

curve_added = pdf.page(1).new_bezier() \
.from_point(20, 100) \
.control_point_1(50, 140) \
.control_point_2(90, 60) \
.to_point(120, 100) \
.stroke_color(Color(30, 90, 220)).add()

if not line_added or not curve_added:
raise RuntimeError("Vector object could not be added")

Build a multi-segment path

closePath connects the current point to the subpath's starting point. It does not fill the path; set a fill color separately. dashPattern controls the stroke. solid removes a previously configured dash pattern.

added = pdf.page(1).new_path() \
.move_to(100, 100) \
.line_to(200, 100) \
.bezier_to(230, 100, 230, 180, 200, 180) \
.line_to(100, 180) \
.close_path() \
.stroke_color(Color(220, 30, 30)) \
.fill_color(Color(255, 230, 230)) \
.stroke_width(2) \
.dash_pattern([5, 2]) \
.add()

The path builder also provides rectangle(...) and circle(...) conveniences:

rectangle = pdf.page(1).new_path().rectangle(72, 300, 120, 60).add()
circle = pdf.page(1).new_path().circle(260, 330, 30).even_odd_fill().add()

Select and edit

Select paths at document or page scope, including coordinate-based selection. A path edit session batches stroke and fill color changes before applying them. Selected paths also support movement, deletion, and clearing clipping.

all_paths = pdf.select_paths()
page_paths = pdf.page(1).select_paths()
paths_at_point = pdf.page(1).select_paths_at(80, 720, tolerance=1.0)
path = pdf.page(1).select_path_at(80, 720, tolerance=1.0)

if path is not None:
moved = path.move_to(100, 700)
unclipped = path.clear_clipping()
deleted = path.delete()

The three operations in the final example are alternatives. Re-select the path after moving it before applying an operation that depends on its current position.

Geometry and appearance

Keep coordinates in PDF points. Stroke width, dash pattern, stroke color, fill color, and winding behavior affect rendering independently. Closing a path and filling it are separate decisions.

After clearing clipping or changing complex geometry, save and visually inspect the PDF; a successful command result does not by itself establish that the new shape matches the intended design.