Layout text with MultilineTextShape
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:
Control layout
The following snippets show how to control the layout of the text.
C#- Align right
multilineText.HorizontalAlignment = HorizontalAlignment.Right;
C# – Center
multilineText.HorizontalAlignment = HorizontalAlignment.Center;
C# – Justify
multilineText.Justified = true;
C# – Line spacing
multilineText.LineSpacing = 5;
C# – Indentation
multilineText.FirstLineIndentation = 20;
![05.png]
## C# - Hang indentation
``` csharp
multilineText.HangIndentation = 20;