Layout text with MultilineTextShape

This sample demonstrates how to format and layout text with MultilineTextShape.

The following code adds a MultilineTextShape at the center of the page. A RectangleShape highlights the text block.

C#

 const string text =
      "With PDFKit.NET you can Manipulate existing " +
      "PDF documents and forms using a highly intuitive object model. " +
      "Add pages, stamp content and fill forms.";

// create a new document with a single page
Document document = new Document();
Page page = new Page(PageSize.Letter);
document.Pages.Add(page);

// add a shapes collection at the center of the page
// it will be the container for multiline text shapes
var shapes = new ShapeCollection
                  {
                     Width = page.Width/2, 
                     Height = page.Height*0.85
                  };
shapes.Transform = new TranslateTransform
                     {
                        X = (page.Width - shapes.Width)/2, 
                        Y = (page.Height - shapes.Height)/2
                     };
page.Overlay.Add(shapes);

// add a rectangle shape with a salmon background to visualize the container
// note that the rectangle is positioned relative to its container, not the page     
var rectangle = 
new RectangleShape(0, 0, shapes.Width, shapes.Height, null, new SolidBrush(Color.Salmon));
shapes.Add(rectangle);

// dock text to the top of the shapes collection - align left
const int bottomMargin = 20;
var fragment = new Fragment(text, Font.TimesRoman, 16);
var multilineText = new MultilineTextShape
                        {
                           Dock = DockStyle.Top, 
                           Margin = {Bottom = bottomMargin}
                        };
multilineText.Fragments.Add(fragment);
shapes.Add(multilineText);

// save result
using (FileStream file = new FileStream("dockmultilinetext.pdf", FileMode.Create, FileAccess.Write))
{
   document.Write(file);
}

As a result we get the multiline text on the page with the default formatting:

Default formatting

Control layout

The following snippets show how to control the layout of the text.

C#- Align right

multilineText.HorizontalAlignment = HorizontalAlignment.Right;

HorizontalAlignment Right

C# – Center

multilineText.HorizontalAlignment = HorizontalAlignment.Center;

Horizontal Alignment Center

C# – Justify

multilineText.Justified = true;

03

C# – Line spacing

multilineText.LineSpacing = 5;

04

C# – Indentation

multilineText.FirstLineIndentation = 20;

![05.png]

## C# - Hang indentation

``` csharp
multilineText.HangIndentation = 20;

06

Download PDFKit.NET 4.0
We will send you a download link
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.

Nuget ID

Use the NugetID and start right away, or download the package and install it handmatically