Paragraph Formatting

Paragraph formatting is used for arranging the document's layout. GemBox.Document provides this type of formatting through ParagraphFormat class.

You have various formatting options at your disposal, like alignment, indentation, spacing, borders, tabs and many more. The formatting can be set directly on the Paragraph elements by using Paragraph.ParagraphFormat property.

The following example demonstrates some available paragraph formatting options.

Screenshot of Word file with paragraph formatting
Word document with various paragraph formatting set with GemBox.Document
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "This paragraph has centered text.")
                {
                    ParagraphFormat =
                    {
                        Alignment = HorizontalAlignment.Center
                    }
                },
                new Paragraph(document, "This paragraph has the following properties:\nLeft indentation is 10 points.\nRight indentation is 20 points.\nHanging indentation is 30 points.")
                {
                    ParagraphFormat =
                    {
                        LeftIndentation = 10,
                        RightIndentation = 20,
                        SpecialIndentation = 30
                    }
                },
                new Paragraph(document, "This paragraph has the following properties:\nFirst line indentation is 40 points.\nLine spacing is exactly 30 points.\nSpace after and before are 10 points.")
                {
                    ParagraphFormat = new ParagraphFormat
                    {
                        SpecialIndentation = -40,
                        LineSpacing = 30,
                        LineSpacingRule = LineSpacingRule.Exactly,
                        SpaceBefore = 10,
                        SpaceAfter = 10
                    }
                }));

        Paragraph paragraph = new Paragraph(document, "This paragraph has borders and background color.");
        paragraph.ParagraphFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, new Color(237, 125, 49), 2);
        paragraph.ParagraphFormat.BackgroundColor = new Color(251, 228, 213);
        document.Sections[0].Blocks.Add(paragraph);

        document.Save("Paragraph Formatting.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "This paragraph has centered text.") With
                {
                    .ParagraphFormat = New ParagraphFormat() With
                    {
                        .Alignment = HorizontalAlignment.Center
                    }
                },
                New Paragraph(document, "This paragraph has the following properties:" & vbLf & "Left indentation is 10 points." & vbLf & "Right indentation is 20 points." & vbLf & "Hanging indentation is 30 points.") With
                {
                    .ParagraphFormat = New ParagraphFormat() With
                    {
                        .LeftIndentation = 10,
                        .RightIndentation = 20,
                        .SpecialIndentation = 30
                    }
                },
                New Paragraph(document, "This paragraph has the following properties:" & vbLf & "First line indentation is 40 points." & vbLf & "Line spacing is exactly 30 points." & vbLf & "Space after and before are 10 points.") With
                {
                    .ParagraphFormat = New ParagraphFormat() With
                    {
                        .SpecialIndentation = -40,
                        .LineSpacing = 30,
                        .LineSpacingRule = LineSpacingRule.Exactly,
                        .SpaceAfter = 10,
                        .SpaceBefore = 10
                    }
                }))

        Dim paragraph As New Paragraph(document, "This paragraph has borders and background color.")
        paragraph.ParagraphFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, New Color(237, 125, 49), 2)
        paragraph.ParagraphFormat.BackgroundColor = New Color(251, 228, 213)
        document.Sections(0).Blocks.Add(paragraph)

        document.Save("Paragraph Formatting.%OutputFileType%")

    End Sub
End Module

The direct formatting has the highest priority in formats resolution. If it's not present, then the formatting can be retrieved from a style that's applied to an element (ParagraphStyle.ParagraphFormat).

If there is no style, then the formatting can be retrieved from the document's default formatting (DocumentModel.DefaultParagraphFormat).

You can read more about this on Style Resolution example or on Formatting properties resolution help page.

Check next example or download examples from GitHub.