Paragraph
Version 6.1.0
In this article
- Access the ParagraphFormat object
- Alignment
- Space before & space after
- Indents
- Line spacing
- Further options
This article describes the format properties of a Paragraph object.
Access the ParagraphFormat object
The paragraph format properties can be found in the ParagraphFormat object. You can set them directly or via a style (see Document / Formatting for more information).
Using a style, you can access the ParagraphFormat object with style.ParagraphFormat
.
To format a paragraph (see Contents / Text) directly, you can access the ParagraphFormat object with paragraph.Format
.
See AccessParagraphFormat sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
You can also set paragraph format properties via object.Format
or a style for some other objects of the document.
This applies to headers and footers (HeaderFooter objects, see Contents / Headers & footers) as well as Table objects and its child Column, Row and Cell objects (see Contents / Tables).
Also for Chart objects and its belonging TextArea and DataLabel objects (see Contents / Charts) you can set the paragraph format this way.
See Document / Formatting for general information about how inheritance will carry these settings to the contained text.
For specific information about inheritance in tables, see Contents / Tables.
For formatting the font of a paragraph, check Font.
Alignment
You can set the Alignment property like this:
// Set the paragraph alignment to Left.
format.Alignment = ParagraphAlignment.Left;
See full Alignment sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
While Left is the default, ParagraphAlignment enum defines diverse types of alignment:
Center aligns the text horizontally centered.
Right aligns the text to the right margin.
Justify stretches the text between the left and the right margin.
Space before & space after
The properties SpaceBefore and SpaceAfter define the space between two paragraphs. Just assign a value like this:
// Set SpaceBefore and SpaceAfter.
format.SpaceBefore = Unit.FromPoint(10);
format.SpaceAfter = Unit.FromPoint(20);
See full SpaceBeforeAndAfter sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
SpaceBefore and SpaceAfter are properties of the type Unit (see Unit for more information).
Between two paragraphs on a page, the maximum of SpaceAfter of the first paragraph and SpaceBefore of the second paragraph is used.
For the first paragraph of a page, SpaceBefore is always ignored.
Indents
Using indents, you can add additional space to the left or right margin for a paragraph. Use LeftIndent to add space to the left margin and RightIndent to add space to the right margin.
With FirstLineIndent you can add space to the left margin only for the first line of the paragraph. FirstLineIndent is added to LeftIndent to calculate the start point of the first line.
This example uses all the indent format properties:
// Set left indent to 2 cm.
format.LeftIndent = Unit.FromCentimeter(2);
// Set right indent to 2 cm.
format.RightIndent = Unit.FromCentimeter(2);
// Set first line indent to 1 cm.
format.FirstLineIndent = Unit.FromCentimeter(1);
See full Indents sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
The indent properties are of the type Unit (see Unit for more information).
You can also assign negative values to let the actual margins appear smaller.
Line spacing
To set a line spacing, you first have to choose a line spacing rule. Dependent on your choice you may have to assign a concrete value or factor to the LineSpacing property.
The LineSpacingRule enum value used by default is Single:
// Set LineSpacingRule to Single.
format.LineSpacingRule = LineSpacingRule.Single;
See full LineSpacing sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
Single means the line spacing is the value that is calculated for a single line, depending on the actual font and its size. This line spacing is usually a little bit bigger than the font size. When choosing Single, the value of the LineSpacing property is ignored, as it is not necessary.
Assigning the LineSpacingRule values OnePtFive or Double means the line spacing will be 1.5 or two times the Single line spacing. When choosing one of these values, the value of the LineSpacing property is also ignored, as it is not necessary.
By using the Multiple LineSpacingRule value, you can provide an own factor to multiply with the Single line spacing:
// Set LineSpacingRule to Multiple.
format.LineSpacingRule = LineSpacingRule.Multiple;
// Set LineSpacing to 2.5 (which is the factor, when using Multiple).
format.LineSpacing = 2.5;
See full LineSpacing sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
LineSpacing is actual a property of the type Unit (see Unit for more information). But, when using Multiple, simply assign an integer or a double to it.
Using AtLeast or Exactly you have to provide an explicit line spacing size to LineSpacing.
If AtLeast is chosen, the line spacing will be at least the value assigned to LineSpacing. If the text or a part of the text uses a font size that requires a bigger line spacing, the actual line spacing will increase for the affected lines.
// Set LineSpacingRule to AtLeast.
format.LineSpacingRule = LineSpacingRule.AtLeast;
// Set LineSpacing to 15 pt.
format.LineSpacing = Unit.FromPoint(15);
See full LineSpacing sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
LineSpacing is a property of type Unit (see Unit for more information).
If Exactly is chosen, the line spacing will be exactly the value assigned to LineSpacing. If the text or a part of the text uses a font size that requires a bigger line spacing, the actual line spacing will NOT increase for the affected lines.
// Set LineSpacingRule to Exactly.
format.LineSpacingRule = LineSpacingRule.Exactly;
// Set LineSpacing to 15 pt.
format.LineSpacing = Unit.FromPoint(15);
See full LineSpacing sample in Paragraph in PDFsharp.Sample repository. Show resulting PDF
LineSpacing is a property of the type Unit (see Unit for more information).
Further options
There are some more functionalities to manage the paragraph format that are not described here. These are:
- Tab stops (see Tab stops)
- ListInfo, which is used to format a paragraph as a list item (see Contents / Lists)
- OutlineLevel, which is used to add headings to the document outline (see Document / Document structure)
- PageBreakBefore, KeepWithNext, KeepTogether and WidowControl, which control the page flow (see Document / Page control)
- Borders (see Borders & line format)
- Shading (see Shading & fill format)