Primitives
Primitives
This guide covers the small, immutable-in-practice value types that carry geometry and color data through the rest of the API: 2D and 3D points, rectangles, transformation matrices, and color values. You will not usually import these directly by name in simple workflows — they show up as property types on higher-level objects such as annotations, image placements, and 3D views — but knowing their shape makes those objects’ properties easy to read and construct.
Points: Point and Point3D
Point is a plain 2D coordinate with x and y float properties. Point3D is its 3D counterpart, constructed as Point3D(x, y, z) and exposing x, y, and z float properties. Point is the type used for GradientAxialShading.start / end; Matrix3D-based transforms and 3D views operate in the same coordinate space that Point3D describes.
Rectangles
Rectangle(x, y, width, height) describes a position and size, and additionally exposes computed left, bottom, and right float properties alongside x, y, width, and height. Rectangle is the type behind PDF3DAnnotation.rect, so a 3D annotation’s on-page bounds are read and set through the same shape as any other rectangle in the API.
Transformation Matrices: Matrix and Matrix3D
Matrix holds a 2D affine transform as six float components — a, b, c, d, e, f — and provides two methods: translate(x, y) to apply a translation in place, and multiply(other) to compose it with another Matrix, returning the resulting Matrix.
Matrix3D is the 3D counterpart: a 3x3 rotation/scale block (m11 through m33) plus a dx, dy, dz translation component, constructed with no arguments (Matrix3D()) and populated by setting its properties. PDF3DView.ctm is typed as Matrix3D | None — the current transformation matrix for a saved 3D view.
Color Primitives: Color, ColorPrimitive, and GradientAxialShading
Three small types carry color and transparency data:
Color(pattern_color_space, r, g, b)— a color value with apattern_color_spaceproperty (typedGradientAxialShading | None) for pattern-based fills, and anrfloat property for its red channel.ColorPrimitive()— a minimal color primitive exposing a singletransparency: intproperty.GradientAxialShading(start_color, end_color, start, end)— describes an axial (linear) gradient between two colors:start_colorandend_colorareColorproperties, andstart/endarePointproperties marking the gradient’s axis endpoints.
These three types compose: a Color can reference a GradientAxialShading as its pattern color space, and that shading in turn is defined using two more Color values and two Point values. PDF3DAnnotation.background_color is typed Color | None, so 3D annotation backgrounds are set using the same Color type described here.
Tips and Best Practices
- Construct
Rectangle,Point, andPoint3Dwith positional arguments in the documented order (x, y[, z]/x, y, width, height) — these types take plain floats, not keyword-only arguments. - Use
Matrix.multiply()to compose transforms rather than manually combining the sixa–fcomponents by hand. - When a property’s type is optional (for example
PDF3DAnnotation.background_color: Color | NoneorPDF3DView.ctm: Matrix3D | None), check forNonebefore reading nested properties on it. - Reach for
GradientAxialShadingonly when you need a two-color linear gradient as a pattern color space; for a flat, single color useColordirectly. Rectangle’sleft,bottom, andrightproperties are derived fromx,y, andwidth/height— treat them as a convenience view of the same rectangle rather than independent state.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Rectangle or point coordinates appear swapped or scaled incorrectly | Positional arguments passed in the wrong order to Rectangle(x, y, width, height) or Point3D(x, y, z) | Double-check argument order against the constructor signature — these types have no keyword-only enforcement |
AttributeError when reading a color or matrix property | Property accessed on a None value from an optional field (e.g. background_color, ctm) | Check the value is not None before accessing nested properties |
| Gradient renders as a single flat color | GradientAxialShading.start and end Point values are identical or very close together | Ensure start and end mark two visually distinct points along the intended gradient axis |
FAQ
Do I need to import Point, Rectangle, or Matrix directly in normal usage?
Usually not — you will most often encounter these as the type of a property (like PDF3DAnnotation.rect or PDF3DView.ctm) rather than constructing them from scratch, though nothing prevents constructing them directly when you need a standalone value.
What is the difference between Color and ColorPrimitive?
Color carries RGB-style channel data plus an optional pattern_color_space for gradient fills. ColorPrimitive is a much smaller type that exposes only a transparency value.
How do I combine two transforms with Matrix?
Call matrix_a.multiply(matrix_b), which returns the composed Matrix. Use translate(x, y) for a simple translation without building a full matrix multiplication.
Is Matrix3D used anywhere outside of 3D annotations?
Within this cluster, Matrix3D appears as the type of PDF3DView.ctm — the current transformation matrix associated with a saved 3D view.
API Reference Summary
| Class/Method | Description |
|---|---|
Point | 2D coordinate (x, y float properties) |
Point3D | 3D coordinate (x, y, z float properties) |
Rectangle | Position and size (x, y, width, height, plus computed left, bottom, right) |
Matrix | 2D affine transform (a–f components); translate / multiply methods |
Matrix3D | 3D transform matrix (m11–m33 rotation/scale block, dx/dy/dz translation) |
Color | Color value with optional pattern_color_space gradient fill |
ColorPrimitive | Minimal color primitive exposing transparency |
GradientAxialShading | Axial (linear) gradient between two Color values along two Point endpoints |