Skip to content

Drawing

Vector graphics in a PDF are operators in a page’s content stream. The creator package wraps the common ones as components you build and hand to c.Draw, so you work in colors and coordinates instead of re, c and S. Anything the components don’t cover can still be written to the content stream directly.

The components split into two families, and the split matters more than the shapes do.

FamilyComponentsPositioning
BlocksLine, Rectangle, EllipseAbsolute or relative. Margins and fit modes apply. Can go in a table or grid cell.
PathsPolyline, Polygon, Curve, PolyBezierCurve, CurvePolygonAbsolute page coordinates only.

Blocks default to absolute positioning. Switched to relative with SetPositioning(creator.PositionRelative), they flow with the surrounding content: they land at the current context position, honor SetMargins, advance the context after themselves, and respond to SetFitMode(creator.FitModeFillWidth) by expanding to the available width. Their constructor coordinates are then used only for size and orientation.

Paths have no such mode. Their points are page coordinates, they leave the context untouched, and they ignore margins and fit modes because they have no such setters. Rectangle and Ellipse are members of the block family, so a plain rectangle is the better choice than a four-point Polygon unless you actually need the polygon’s holes.

Coordinates

Every component measures from the upper left corner of the page, with y growing downward. That is upside down relative to PDF’s own coordinate system, and each component flips y itself on the way out. It’s consistent across the creator, so a paragraph at y = 100 and a line at y = 100 are at the same height.

Sizes are in points, 72 to the inch. A default letter page is 612 by 792.

Colors

Colors are creator.Color values, built with ColorRGBFromHex("#ff0000"), ColorRGBFrom8bit, ColorRGBFromArithmetic, the CMYK and gray equivalents, or taken from the constants ColorBlack, ColorWhite, ColorRed, ColorGreen, ColorBlue and ColorYellow.

Fill and border are separate concerns with separate opacities on the shapes that have both. Fill and border opacity go into an ExtGState, so a partly transparent shape adds a graphics state resource to the page.

Beyond the components

contentstream/draw holds the primitives the components are built from: draw.Point, draw.CubicBezierCurve, draw.LineStyle and the draw.BasicLine, draw.Rectangle and draw.Polygon shapes. You pass these types in when constructing a polyline or a Bezier path.

For an operator the creator has no component for, build the content with contentstream.NewContentCreator() and append it to a page with page.AppendContentBytes.

Where to look

GuideCovers
Draw linesLine width, color, dash patterns, opacity, absolute versus relative positioning, and lines inside divisions and table cells.
Draw shapesRectangles, ellipses, polylines, polygons and the three Bezier components.
Last updated on