Products
PDFKit.NET 4.0
Created
3/12/2014
Tags
Generate PDF Shapes

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:

00.png

Control layout

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

C#- Align right

multilineText.HorizontalAlignment = HorizontalAlignment.Right;

02.png

C# - Center

multilineText.HorizontalAlignment = HorizontalAlignment.Center;

01.png

C# - Justify

multilineText.Justified = true;

03.png

C# - Line spacing

multilineText.LineSpacing = 5;

04.png

C# - Indentation

multilineText.FirstLineIndentation = 20;

![05.png]

## C# - Hang indentation

``` csharp
multilineText.HangIndentation = 20;

06.png