Sizing & positioning
Version 6.1.0
In this article
This article describes how the size and position of an object is changed.
Introduction
Sizing and positioning are done the same way for all objects derived from Shape. This includes TextFrame (see Contents / TextFrames), Image (see Contents / Images) and Chart objects, as well as parts of a chart it like TextArea, PlotArea, Series and Point objects (see Contents / Charts).
Setting the size
The size of a shape can be simply defined by setting its Width and Height properties:
// Set width and height.
textFrame.Width = Unit.FromCentimeter(6);
textFrame.Height = Unit.FromCentimeter(3);
See full Sizing sample in PDFsharp.Sample repository. Show resulting PDF
Width and Height are properties of the type Unit (see Unit for more information).
For Image objects, there are some additional size related properties, like ScaleWidth, ScaleHeight, LockAspectRatio and Resolution (see Contents / Images for more information).
Horizontal positioning
By default, an object derived from Shape will be horizontally aligned at the left page margin. You can vary the horizontal position by setting values for the Left and RelativeHorizontal properties.
You can assign some of the ShapePosition enum values to the Left property to set a horizontal alignment for the object. Valid values here are Left, Center, Right, Inside and Outside.
// Make textFrame right aligned.
// Without textFrame.RelativeHorizontal specified,
// the TextFrame will be positioned inside the page margins.
textFrame.Left = ShapePosition.Right;
See full HorizontalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
Setting the Inside or Outside value only makes sense inside of sections using mirrored margins (see Page setup for more information). Like in a book, on even pages outside aligned objects will be aligned left, while they will be right aligned on odd pages. In parallel, inside aligned objects will be aligned right on even and left on odd pages.
By default, the object will be aligned at the page margins, which is equivalent to RelativeHorizontal.Margin
.
To align it at the borders of the page instead, set the RelativeHorizontal property to RelativeHorizontal.Page
.
// Make textFrame right aligned inside the page borders.
textFrame.Left = ShapePosition.Right;
textFrame.RelativeHorizontal = RelativeHorizontal.Page;
See full HorizontalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
Alternatively to setting an alignment, you can set the Left property to a Unit value. This distance will be measured from the left page border or margin, dependent on the RelativeHorizontal value. To place a text frame 2 centimeters from the left page border, configure it like this:
// Set left position to 2 cm from the left page border.
textFrame.Left = Unit.FromCentimeter(2);
textFrame.RelativeHorizontal = RelativeHorizontal.Page;
See full HorizontalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
You can assign values of the type Unit to the Left property (see Unit for more information).
Vertical positioning
By default, an object derived from Shape will be vertically positioned at the current position in the document flow. You can vary the vertical position by setting values for the Top and RelativeVertical properties.
You can assign some of the ShapePosition enum values to the Top property to set a vertical alignment for the object.
Valid values here are Top, Center and Bottom.
You have to set the RelativeVertical property to RelativeHorizontal.Margin
or RelativeHorizontal.Page
to define if the object shall be aligned at the page margins or borders.
// Make textFrame bottom aligned inside the page margins.
textFrame.Top = ShapePosition.Bottom;
textFrame.RelativeVertical = RelativeVertical.Margin;
See full VerticalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
Alternatively to setting an alignment, you can set the Top property to a Unit value. With RelativeVertical unset, this distance will be measured form the current position in the document flow:
// Set top position to 2 cm.
// Without textFrame.RelativeVertical specified, the TextFrame will be
// positioned at the current position in document flow plus the Top value.
textFrame.Top = Unit.FromCentimeter(2);
See full VerticalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
You can assign values of the type Unit to the Top property (see Unit for more information).
You can also set RelativeVertical to RelativeHorizontal.Margin
or RelativeHorizontal.Page
to let the distance be measured from the upper page margin or border.
To place a text frame 2 centimeters from the upper page border, configure it like this:
// Set top position to 2 cm from the upper page border.
textFrame.Top = Unit.FromCentimeter(2);
textFrame.RelativeVertical = RelativeVertical.Page;
See full VerticalPositioning sample in PDFsharp.Sample repository. Show resulting PDF
You can assign values of the type Unit to the Top property (see Unit for more information).
WrapFormat
The WrapFormat class lets you set a wrap style and distances, that shall be kept free between the object and its surroundings.
For objects positioned in the document flow, you can set a wrap style. The default WrapStyle value used is TopBottom, which lets the object take its place inside the document flow.
By setting the wrap style to Through, you can let the following text flow in front of the image or insert another object at the same vertical position:
// Set WrapStyle to Through, as the next content shall not start below it,
// but at the same vertical position.
image.WrapFormat.Style = WrapStyle.Through;
See full WrapFormat sample in PDFsharp.Sample repository. Show resulting PDF
You can also set specific minimum distances from the object to its surroundings:
// Set WrapFormat distances.
textFrame.WrapFormat.DistanceTop = Unit.FromCentimeter(0.5);
textFrame.WrapFormat.DistanceRight = Unit.FromCentimeter(1);
textFrame.WrapFormat.DistanceBottom = Unit.FromCentimeter(1.5);
textFrame.WrapFormat.DistanceLeft = Unit.FromCentimeter(2);
See full WrapFormat sample in PDFsharp.Sample repository. Show resulting PDF
The distance properties are of the type Unit (see Unit for more information).