Add multiline text to a PDF document

Generate PDF, Manipulate PDF
11/2/2011

Downloads

The MultilineTextShape lets you add multiline text to PDF with mixed formatting and line-breaking.

The content of the multiline text is specified as a collection of fragments. A fragment has properties that specify both content (text) and appearance (font, font size, color). All fragments will be displayed concatenated and broken across lines as needed.

C# code sample that add multiline text to PDF

The following adds a MultilineTextShape to the horizontal center of a PDF document:

C# code sample

1 const string text = 2 "With PDFKit.NET you can Manipulate existing " + 3 "PDF documents and forms using a highly intuitive object model. " + 4 "Add pages, stamp content and fill forms."; 5 6 // create a new document with a single page 7 Document document = new Document(); 8 Page page = new Page(PageSize.Letter); 9 document.Pages.Add(page); 10 11 // add a shapes collection at the center of the page 12 // it will be the container for multiline text shapes 13 ShapeCollection shapes = new ShapeCollection 14 { 15 Width = page.Width / 2, 16 Height = page.Height * 0.85 17 }; 18 shapes.Transform = new TranslateTransform 19 { 20 X = (page.Width - shapes.Width) / 2, 21 Y = (page.Height - shapes.Height) / 2 22 }; 23 page.Overlay.Add(shapes); 24 25 // dock text to the top of the shapes collection - align left 26 const int bottomMargin = 20; 27 Fragment fragment = new Fragment(text, Font.TimesRoman, 16); 28 MultilineTextShape multilineText = new MultilineTextShape 29 { 30 Dock = DockStyle.Top, 31 Margin = { Bottom = bottomMargin } 32 }; 33 multilineText.Fragments.Add(fragment); 34 shapes.Add(multilineText); 35 36 // save result 37 using (FileStream file = new FileStream(@"..\..\dockmultilinetext.pdf", FileMode.Create, FileAccess.Write)) 38 { 39 document.Write(file); 40 }

VB.NET

1 ' create a new document 2 Dim document As New Document() 3 4 ' append a new page (portrait Letter) 5 Dim landscape As Boolean = False 6 Dim page As New Page(PageSize.Letter, landscape) 7 document.Pages.Add(page) 8 9 ' stamp a text paragraph at the center 10 Dim multilineText As New MultilineTextShape() 11 Dim translate As New TranslateTransform() 12 multilineText.Transform = translate 13 14 Dim fragment As New Fragment(text, Font.TimesRoman, 12) 15 multilineText.Fragments.Add(fragment) 16 17 ' center horizontally 18 multilineText.Width = page.Width / 2 19 translate.X = (page.Width - multilineText.Width) / 2 20 21 ' Y corresponds to the top of the paragraph 22 translate.Y = page.Height / 2 23 24 page.VisualOverlay.Add(multilineText) 25 26 Using file As New FileStream("..\..\multilinetext.pdf", FileMode.Create, FileAccess.Write) 27 document.Write(file) 28 End Using

The text will be automatically split up into multiple lines based on the Width you specified. The resulting PDF will look like this:

add-multiline-text-to-pdf.png

In addition, you can set the Height property but this is used if the multiline text shape is auto-sized. This is the case if and only if there is exactly one fragment that has a zero font size.