Shape#
This class allows creating interconnected graphical elements on a PDF page. Its methods have the same meaning and name as the corresponding Page methods.
In fact, each Page draw method is just a convenience wrapper for (1) one shape draw method, (2) the Shape.Finish() method, and (3) the Shape.Commit() method. For page text insertion, only the Shape.Commit() method is invoked. If many draw and text operations are executed for a page, you should always consider using a Shape object.
Several draw methods can be executed in a row and each one of them will contribute to one drawing. Once the drawing is complete, the Shape.Finish() method must be invoked to apply color, dashing, width, morphing and other attributes.
Draw methods of this class (and Shape.InsertTextbox()) are logging the area they are covering in a rectangle (Shape.Rect). This property can for instance be used to set Page.CropBoxPosition.
Text insertions Shape.InsertText() and Shape.InsertTextbox() implicitly execute a “finish” and therefore only require Shape.Commit() to become effective. As a consequence, both include parameters for controlling properties like colors, etc.
Method / Attribute |
Description |
|---|---|
Update the page’s contents |
|
Draw a cubic Bezier curve |
|
Draw a circle around a point |
|
Draw a cubic Bezier using one helper point |
|
Draw a line |
|
Draw an ellipse |
|
Connect a sequence of points |
|
Draw a quadrilateral |
|
Draw a rectangle |
|
Draw a circular sector or piece of pie |
|
Draw a squiggly line |
|
Draw a zigzag line |
|
Finish a set of draw commands |
|
Insert text lines |
|
Fit text into a rectangle |
|
Stores the page’s document |
|
Draw commands since last |
|
Stores the page’s height |
|
Stores the current point |
|
Stores the owning page |
|
Rectangle surrounding drawings |
|
Accumulated text insertions |
|
Accumulated string to be stored in |
|
Stores the page’s width |
Class API
- class Shape#
- Shape(Page page)#
Create a new drawing. the Page object is being given the convenience method NewShape() to construct a Shape object. During instantiation, a check will be made whether we do have a PDF page. An exception is otherwise raised.
- Parameters:
page (Page) – an existing page of a PDF document.
- DrawSquiggle(Point p1, Point p2, float breadth: 2)#
Draw a squiggly (wavy, undulated) line from
Pointobjects p1 to p2. An integer number of full wave periods will always be drawn, one period having a length of 4 * breadth. The breadth parameter will be adjusted as necessary to meet this condition. The drawn line will always turn “left” when leaving p1 and always join p2 from the “right”.- Parameters:
- Return type:
- Returns:
the end point, p2.
Here is an example of three connected lines, forming a closed, filled triangle. Little arrows indicate the stroking direction.
Note
Waves drawn are not trigonometric (sine / cosine). If you need that, have a look at `https://github.com/ArtifexSoftware/MuPDF.NET/tree/main/Examples/DrawPolygon`_.
- DrawZigzag(Point p1, Point p2, float breadth: 2)#
Draw a zigzag line from
Pointobjects p1 to p2. Otherwise works exactly likeShape.DrawSquiggle().
- DrawPolyline(Point[] points)#
Draw several connected lines between points contained in the sequence points. This can be used for creating arbitrary polygons by setting the last item equal to the first one.
- DrawBezier(Point p1, Point p2, Point p3, Point p4)#
Draw a standard cubic Bézier curve from p1 to p4, using p2 and p3 as control points.
All arguments are
Points.- Return type:
- Returns:
the end point, p4.
Note
The points do not need to be different – experiment a bit with some of them being equal!
Example:
- DrawOval(Rect tetra)#
Draw an “ellipse” inside the given tetragon (quadrilateral). If it is a square, a regular circle is drawn, a general rectangle will result in an ellipse. If a quadrilateral is used instead, a plethora of shapes can be the result.
The drawing starts and ends at the middle point of the line
BottomLeft -> TopLeftcorners in an anti-clockwise movement.- Parameters:
- Return type:
- Returns:
the middle point of line
rect.BottomLeft -> rect.TopLeft, or resp.quad.LowLeft -> quad.UpperLeft. Look at just a few examples here.
- DrawCircle(Point center, float radius)#
Draw a circle given its center and radius. The drawing starts and ends at point
center - (radius, 0)in an anti-clockwise movement. This point is the middle of the enclosing square’s left side.This is a shortcut for
DrawSector(center, start, 360, fullSector=false). To draw the same circle in a clockwise movement, use-360as degrees.
- DrawCurve(Point p1, Point p2, Point p3)#
A special case of DrawBezier(): Draw a cubic Bezier curve from p1 to p3. On each of the two lines
p1 -> p2andp3 -> p2one control point is generated. Both control points will therefore be on the same side of the linep1 -> p3. This guaranties that the curve’s curvature does not change its sign. If the lines to p2 intersect with an angle of 90 degrees, then the resulting curve is a quarter ellipse (resp. quarter circle, if of same length).All arguments are
Point.- Return type:
- Returns:
the end point, p3. The following is a filled quarter ellipse segment. The yellow area is oriented clockwise:
- DrawSector(Point center, Point point, float angle, bool fullSector: true)#
Draw a circular sector, optionally connecting the arc to the circle’s center (like a piece of pie).
- Parameters:
center (Point) – the center of the circle.
point (Point) – one of the two end points of the pie’s arc segment. The other one is calculated from the angle.
angle (float) – the angle of the sector in degrees. Used to calculate the other end point of the arc. Depending on its sign, the arc is drawn anti-clockwise (positive) or clockwise.
fullSector (bool) – whether to draw connecting lines from the ends of the arc to the circle center. If a fill color is specified, the full “pie” is colored, otherwise just the sector.
- Return type:
- Returns:
the other end point of the arc. Can be used as starting point for a following invocation to create logically connected pies charts. Examples:

- DrawRect(Rect rect, float radius: 0)#
Draw a rectangle. The drawing starts and ends at the top-left corner in an anti-clockwise movement.
- Parameters:
rect (Rect) – where to put the rectangle on the page.
radius (float) – draw rounded rectangle corners. If not
null, specifies the radius of the curvature as a percentage of a rectangle side length. This must one or (a tuple of) two floats0 < radius <= 0.5, where 0.5 corresponds to 50% of the respective side. If a float, the radius of the curvature is computed asradius * min(width, height), drawing the corner’s perimeter as a quarter circle. If a tuple(rx, ry)is given, then the curvature is asymmetric with respect to the horizontal and vertical directions. A value ofradius=(0.5, 0.5)draws an ellipse.
- Return type:
- Returns:
top-left corner of the rectangle.
- DrawQuad(Quad quad)#
Draw a quadrilateral. The drawing starts and ends at the top-left corner (
Quad.UpperLeft) in an anti-clockwise movement. It is a shortcut ofShape.DrawPolyline()with the argument(ul, ll, lr, ur, ul).- Parameters:
quad (Quad) – where to put the tetragon on the page.
- Return type:
- Returns:
- Finish(float width: 1, float[] color: null, float fill: null, int lineCap: 0, int lineJoin: 0, string dashes: null, bool closePath: true, bool evenOdd: false, Morph morph: null, float strokeOpacity: 1, float fillOpacity: 1, int oc: 0)#
Finish a set of draw*() methods by applying Common Parameters to all of them.
It has no effect on
Shape.InsertText()andShape.InsertTextbox().The method also supports morphing the compound drawing using Point P and Matrix M.
- Parameters:
morph (Morph) – morph the text or the compound drawing around some arbitrary Point P by applying Matrix matrix to it. This implies that P is a fixed point of this operation: it will not change its position. Default is no morphing (null). The matrix can contain any values in its first 4 components, matrix.e == matrix.f == 0 must be true, however. This means that any combination of scaling, shearing, rotating, flipping, etc. is possible, but translations are not.
strokeOpacity (float) – set transparency for stroke colors. Value < 0 or > 1 will be ignored. Default is 1 (intransparent).
fillOpacity (float) – set transparency for fill colors. Default is 1 (intransparent).
evenOdd (bool) – request the “even-odd rule” for filling operations. Default is false, so that the “nonzero winding number rule” is used. These rules are alternative methods to apply the fill color where areas overlap. Only with fairly complex shapes a different behavior is to be expected with these rules. For an in-depth explanation, see Adobe PDF References, pp. 137 ff. Here is an example to demonstrate the difference.
oc (int) – the
xrefnumber of anOCGorOCMDto make this drawing conditionally displayable.
Note
For each pixel in a shape, the following will happen:
Rule “eveOdd” counts, how many areas contain the pixel. If this count is odd, the pixel is regarded inside the shape, if it is even, the pixel is outside.
The default rule “nonzero winding” in addition looks at the “orientation” of each area containing the pixel: it adds 1 if an area is drawn anti-clockwise and it subtracts 1 for clockwise areas. If the result is zero, the pixel is regarded outside, pixels with a non-zero count are inside the shape.
Of the four shapes in above image, the top two each show three circles drawn in standard manner (anti-clockwise, look at the arrows). The lower two shapes contain one (the top-left) circle drawn clockwise. As can be seen, area orientation is irrelevant for the right column (even-odd rule).
- InsertText(Point point, string text, string fontName, string fontFile, float fontSize: 11, bool setSample: false, int encoding: TEXT_ENCODING_LATIN, float[] color: null, float lineHeight: 0, float[] fill: null, int renderMode: 0, float borderWidth: 0.05f, int rotate: 0, Morph morph=null, float strokeOpacity: 1, float fillOpacity: 1, int oc: 0)#
Insert text lines start at point.
- Parameters:
point (Point) –
the bottom-left position of the first character of text in pixels. It is important to understand, how this works in conjunction with the rotate parameter. Please have a look at the following picture. The small red dots indicate the positions of point in each of the four possible cases.

text (str/sequence) – the text to be inserted. May be specified as either a string type or as a sequence type. For sequences, or strings containing line breaks n, several lines will be inserted. No care will be taken if lines are too wide, but the number of inserted lines will be limited by “vertical” space on the page (in the sense of reading direction as established by the rotate parameter). Any rest of text is discarded – the return code however contains the number of inserted lines.
lineHeight (float) – a factor to override the line height calculated from font properties. If not
null, a line height offontSize * lineHeightwill be used.strokeOpacity (float) – set transparency for stroke colors (the border line of a character). Only
0 <= value <= 1will be considered. Default is 1 (intransparent).fillOpacity (float) – set transparency for fill colors. Default is 1 (intransparent). Use this value to control transparency of the text color. Stroke opacity only affects the border line of characters.
rotate (int) – determines whether to rotate the text. Acceptable values are multiples of 90 degrees. Default is 0 (no rotation), meaning horizontal text lines oriented from left to right. 180 means text is shown upside down from right to left. 90 means anti-clockwise rotation, text running upwards. 270 (or -90) means clockwise rotation, text running downwards. In any case, point specifies the bottom-left coordinates of the first character’s rectangle. Multiple lines, if present, always follow the reading direction established by this parameter. So line 2 is located above line 1 in case of
rotate = 180, etc.oc (int) – the
xrefnumber of anOCGorOCMDto make this text conditionally displayable.
- Return type:
int
- Returns:
number of lines inserted.
For a description of the other parameters see Common Parameters.
- InsertTextbox(Rect rect, string buffer, string fontName, string fontFile, float fontSize: 11, bool setSimple: false, int encoding: TEXT_ENCODING_LATIN, float[] color: null, float[] fill: null, int renderMode: 0, float borderWidth: 0.05, int expandTabs: 1, int align: TEXT_ALIGN_LEFT, int rotate: 0, float lineHeight: 0, Morph morph: null, float strokeOpacity: 1, float fillOpacity: 1, int oc: 0)#
PDF only: Insert text into the specified rectangle. The text will be split into lines and words and then filled into the available space, starting from one of the four rectangle corners, which depends on
rotate. Line feeds and multiple space will be respected.- Parameters:
rect (Rect) – the area to use. It must be finite and not empty.
buffer (string/sequence) – the text to be inserted. Must be specified as a string or a sequence of strings. Line breaks are respected also when occurring in a sequence entry.
align (int) – align each text line. Default is 0 (left). Centered, right and justified are the other supported options, see Text Alignment. Please note that the effect of parameter value TEXT_ALIGN_JUSTIFY is only achievable with “simple” (single-byte) fonts (including the Base-14-Fonts).
lineHeight (float) – a factor to override the line height calculated from font properties. If not
null, a line height offontSize * lineHeightwill be used.expandTabs (int) – controls handling of tab characters
\tusing the spaces ofexpandTabsmethod per each line.strokeOpacity (float) – set transparency for stroke colors. Negative values and values > 1 will be ignored. Default is 1 (intransparent).
fillOpacity (float) – set transparency for fill colors. Default is 1 (intransparent). Use this value to control transparency of the text color. Stroke opacity only affects the border line of characters.
rotate (int) – requests text to be rotated in the rectangle. This value must be a multiple of 90 degrees. Default is 0 (no rotation). Effectively, the four values
0,90,180and270(=-90) are processed, each causing the text to start in a different rectangle corner. Bottom-left is90, bottom-right is180, and-90 / 270is top-right. See the example how text is filled in a rectangle. This argument takes precedence over morphing. See the second example, which shows text first rotated left by90degrees and then the whole rectangle rotated clockwise around is lower left corner.oc (int) – the
xrefnumber of anOCGorOCMDto make this text conditionally displayable.
- Return type:
float
- Returns:
If positive or zero: successful execution. The value returned is the unused rectangle line space in pixels. This may safely be ignored – or be used to optimize the rectangle, position subsequent items, etc.
If negative: no execution. The value returned is the space deficit to store text lines. Enlarge rectangle, decrease fontSize, decrease text amount, etc.
For a description of the other parameters see Common Parameters.
- Commit(bool overlay: true)#
Update the page’s
contentswith the accumulated drawings, followed by any text insertions. If text overlaps drawings, it will be written on top of the drawings.Warning
Do not forget to execute this method:
If a shape is not committed, it will be ignored and the page will not be changed!
The method will reset attributes
Shape.Rect,LastPoint,DrawCont,TextContandTotalCont. Afterwards, the shape object can be reused for the same page.- Parameters:
overlay (bool) – determine whether to put content in foreground (default) or background. Relevant only, if the page already has a non-empty
contentsobject.
———- Attributes ———-
- Height#
Copy of the page’s height
- Type:
float
- Width#
Copy of the page’s width.
- Type:
float
- DrawCont#
Accumulated command buffer for draw methods since last finish. Every finish method will append its commands to
Shape.TotalCont.- Type:
string
- TextCont#
Accumulated text buffer. All text insertions go here. This buffer will be appended to
TotalContShape.Commit(), so that text will never be covered by drawings in the same Shape.- Type:
string
- Rect#
Rectangle surrounding drawings. This attribute is at your disposal and may be changed at any time. Its value is set to null when a shape is created or committed. Every draw* method, and
Shape.InsertTextbox()update this property (i.e. enlarge the rectangle as needed). Morphing operations, however (Shape.Finish(),Shape.InsertTextbox()) are ignored.A typical use of this attribute would be setting
Page.CropBoxPositionto this value, when you are creating shapes for later or external use. If you have not manipulated the attribute yourself, it should reflect a rectangle that contains all drawings so far.If you have used morphing and need a rectangle containing the morphed objects, use the following code:
>>> # assuming ... Morph morph = new Morph(){P = point, M = matrix}; // ... recalculate the shape rectangle like so: shape.Rect = (shape.Rect - new Rect(point, point)) * ~matrix + new Rect(point, point);- Type:
- TotalCont#
Total accumulated command buffer for draws and text insertions. This will be used by
Shape.Commit().- Type:
string
Usage#
A drawing object is constructed by shape = page.NewShape(). After this, as many draw, finish and text insertions methods as required may follow. Each sequence of draws must be finished before the drawing is committed. The overall coding pattern looks like this:
.. note::
Each Finish() combines the preceding draws into one logical shape, giving it common colors, line width, morphing, etc. If ClosePath is specified, it will also connect the end point of the last draw with the starting point of the first one.
To successfully create compound graphics, let each draw method use the end point of the previous one as its starting point. In the above pseudo code, draw2 should hence use the returned Point of draw1 as its starting point. Failing to do so, would automatically start a new path and Finish() may not work as expected (but it won’t complain either).
Text insertions may occur anywhere before the commit (they neither touch
Shape.DrawContnorShape.LastPoint). They are appended to Shape.TotalCont directly, whereas draws will be appended by Shape.Finish.Each Commit takes all text insertions and shapes and places them in foreground or background on the page – thus providing a way to control graphical layers.
Only commit will update the page’s contents, the other methods are basically string manipulations.
Examples#
Create a full circle of pieces of pie in different colors:
Shape shape = page.NewShape(); // start a new shape float[] cols = (...); // a sequence of RGB color triples int pieces = len(cols); // number of pieces to draw float beta = 360.0f / pieces; // angle of each piece of pie center = new Point(...); // center of the pie p0 = new Point(...); // starting point for (int i = 0; i < pieces; i ++) Point p0 = shape.DrawSector(center, p0, beta, fullSector=true) // draw piece // now fill it but do not connect ends of the arc shape.Finish(fill: cols[i], closePath: false) shape.Commit(); // update the page
Here is an example for 5 colors:
Create a regular n-edged polygon (fill yellow, red border). We use DrawSector() only to calculate the points on the circumference, and empty the draw command buffer again before drawing the polygon:
Shape shape = page.NewShape(); // start a new shape float beta = -360.0f / n; // our angle, drawn clockwise Point center = new Point(...); // center of circle Point p0 = new Point(...); // start here (1st edge) List<Point> points = new List<Point>(p0); // store polygon edges for (int i = 0; i < n; i ++) // calculate the edges Point p0 = shape.DrawSector(center, p0, beta); points.Add(p0); shape.DrawCont = ""; // do not draw the circle sectors shape.DrawPolyline(points); # draw the polygon shape.Finish(color: {1,0,0}, fill: {1,1,0}, closePath: false); shape.Commit();
Here is the polygon for n = 7:
Common Parameters#
FontName (string)
In general, there are three options:
In this case, fontFile must be specified.
Choose a font already in use by the page. Then specify its reference name prefixed with a slash “/”, see example below.
Specify a font file present on your system. In this case choose an arbitrary, but new name for this parameter (without “/” prefix).
If inserted text should re-use one of the page’s fonts, use its reference name appearing in
Page.GetFonts()like so:Suppose the font list has the item [1024, 0, ‘Type1’, ‘NimbusMonL-Bold’, ‘R366’], then specify fontName = “/R366”, fontFile = null to use font NimbusMonL-Bold.
FontFile (string)
File path of a font existing on your computer. If you specify fontFile, make sure you use a fontName not occurring in the above list. This new font will be embedded in the PDF upon doc.save(). Similar to new images, a font file will be embedded only once. A table of MD5 codes for the binary font contents is used to ensure this.
SetSimple (bool)
Fonts installed from files are installed as Type0 fonts by default. If you want to use 1-byte characters only, set this to true. This setting cannot be reverted. Subsequent changes are ignored.
FontSize (float)
Font size of text, see:
fontSize.
Dashes (string)
Causes lines to be drawn dashed. The general format is
"[n m] p"of (up to) 3 floats denoting pixel lengths.nis the dash length,m(optional) is the subsequent gap length, andp(the “phase” - required, even if 0!) specifies how many pixels should be skipped before the dashing starts. Ifmis omitted, it defaults ton.A continuous line (no dashes) is drawn with
"[] 0"or null or"". Examples:
Specifying
"[3 4] 0"means dashes of 3 and gaps of 4 pixels following each other.
"[3 3] 0"and"[3] 0"do the same thing.For (the rather complex) details on how to achieve sophisticated dashing effects, see Adobe PDF References, page 217.
Color / Fill (list, tuple)
Stroke and fill colors can be specified as tuples or list of of floats from 0 to 1. These sequences must have a length of 1 (GRAY), 3 (RGB) or 4 (CMYK). For GRAY colorspace, a single float instead of the unwieldy (float,) or [float] is also accepted. Accept (default) or use
nullto not use the parameter.To simplify color specification, method GetColor() in Utils may be used to get predefined RGB color triples by name. It accepts a string as the name of the color and returns the corresponding triple. The method knows over 540 color names – see section ColorDatabase.
Please note that the term color usually means “stroke” color when used in conjunction with fill color.
If letting default a color parameter to
null, then no resp. color selection command will be generated. If fill and color are bothnull, then the drawing will contain no color specification. But it will still be “stroked”, which causes PDF’s default color “black” be used by Adobe Acrobat and all other viewers.
Width (float)
The stroke (“border”) width of the elements in a shape (if applicable). The default value is 1. The values width, color and fill have the following relationship / dependency:
If
fill=nullshape elements will always be drawn with a border - even ifcolor=null(in which case black is taken) orwidth=0(in which case 1 is taken).Shapes without border can only be achieved if a fill color is specified (which may be white of course). To achieve this, specify
width=0. In this case, thecolorparameter is ignored.
StrokeOpacity / FillOpacity (floats)
Both values are floats in range [0, 1]. Negative values or values > 1 will ignored (in most cases). Both set the transparency such that a value 0.5 corresponds to 50% transparency, 0 means invisible and 1 means intransparent. For e.g. a rectangle the stroke opacity applies to its border and fill opacity to its interior.
For text insertions (
Shape.InsertText()andShape.InsertTextbox()), use fill_opacity for the text. At first sight this seems surprising, but it becomes obvious when you look further down to render_mode: fill_opacity applies to the yellow and stroke_opacity applies to the blue color.
BorderWidth (float)
Set the border width for text insertions. New in v1.14.9. Relevant only if the render mode argument is used with a value greater zero.
RenderMode (int)
Integer in range of 8 which controls the text appearance (
Shape.InsertText()andShape.InsertTextbox()). See page 246 in Adobe PDF References. These methods now also differentiate between fill and stroke colors.
For default 0, only the text fill color is used to paint the text. For backward compatibility, using the color parameter instead also works.
For render mode 1, only the border of each glyph (i.e. text character) is drawn with a thickness as set in argument borderWidth. The color chosen in the color argument is taken for this, the fill parameter is ignored.
For render mode 2, the glyphs are filled and stroked, using both color parameters and the specified border width. You can use this value to simulate bold text without using another font: choose the same value for fill and color and an appropriate value for borderWidth.
For render mode 3, the glyphs are neither stroked nor filled: the text becomes invisible.
The following examples use border_width=0.3, together with a fontSize of 15. Stroke color is blue and fill color is some yellow.
![]()
Overlay (bool)
Causes the item to appear in foreground (default) or background.
Morph (Morph)
Causes “morphing” of either a shape, created by the Draw() methods, or the text inserted by page methods InsertTextbox() / InsertText(). If not null, it must be a pair (P, M), where P is a Point and M is a Matrix. The matrix can be anything except translations, i.e. matrix.e == matrix.f == 0 must be true. The point is used as a fixed point for the matrix operation. For example, if matrix is a rotation or scaling, then point is its center. Similarly, if matrix is a left-right or up-down flip, then the mirroring axis will be the vertical, respectively horizontal line going through point, etc.
Note
Several methods contain checks whether the to be inserted items will actually fit into the page (like
Shape.InsertText(), orShape.DrawRect()). For the result of a morphing operation there is however no such guaranty: this is entirely the programmer’s responsibility.
LineCap (deprecated: “roundCap”) (int)
Controls the look of line ends. The default value 0 lets each line end at exactly the given coordinate in a sharp edge. A value of 1 adds a semi-circle to the ends, whose center is the end point and whose diameter is the line width. Value 2 adds a semi-square with an edge length of line width and a center of the line end.
Changed in version 1.14.15
LineJoin (int)
Controls the way how line connections look like. This may be either as a sharp edge (0), a rounded join (1), or a cut-off edge (2, “butt”).
ClosePath (bool)
Causes the end point of a drawing to be automatically connected with the starting point (by a straight line).

