How to add autosized text to PDF

Manipulate PDF, Fonts
7/15/2015

This code sample shows you how you can add a piece of auto-sized text to your PDF document.

This can be done by creating a MultiLineTextShape with a textfragment of size 0 (which means autosize) and setting the boundaries of the text shape.

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 7 Document document = new Document(); 8 9 // append a new page (portrait Letter) 10 bool landscape = false; 11 Page page = new Page(PageSize.Letter, landscape); 12 document.Pages.Add(page); 13 14 // stamp a multiline text shape 15 // note that the font size is set to 0 - this will autosize the text 16 MultilineTextShape multilineText = new MultilineTextShape(); 17 Fragment fragment = new Fragment(text, Font.TimesRoman, 0); 18 multilineText.Fragments.Add(fragment); 19 20 // set boundaries of the text 21 multilineText.Width = page.Width / 4; 22 multilineText.Height = 75; 23 24 // center 25 TranslateTransform translate = new TranslateTransform(); 26 multilineText.Transform = translate; 27 translate.X = (page.Width - multilineText.Width) / 2; 28 translate.Y = (page.Height - multilineText.Height) / 2 + multilineText.Height; 29 30 // add a rectangle shape to visualize the boundary of the text 31 RectangleShape rectangle = new RectangleShape( 32 multilineText.X, multilineText.Y - multilineText.Height, 33 multilineText.Width, multilineText.Height, 34 null, new SolidBrush(System.Drawing.Color.Salmon)); 35 36 // add both shapes 37 page.Overlay.Add(rectangle); 38 page.Overlay.Add(multilineText); 39 40 using (FileStream file = new FileStream(@"..\..\autosizetext.pdf", FileMode.Create, FileAccess.Write)) 41 { 42 document.Write(file); 43 }

VB.NET code sample

1 Const text As String = "With PDFKit.NET you can Manipulate existing " + "PDF documents and forms using a highly intuitive object model. " + "Add pages, stamp content and fill forms." 2 3 ' create a new document 4 Dim document As New Document() 5 6 ' append a new page (portrait Letter) 7 Dim landscape As Boolean = False 8 Dim page As New Page(PageSize.Letter, landscape) 9 document.Pages.Add(page) 10 11 ' stamp a multiline text shape 12 ' note that the font size is set to 0 - this will autosize the text 13 Dim multilineText As New MultilineTextShape() 14 Dim fragment As New Fragment(text, Font.TimesRoman, 0) 15 multilineText.Fragments.Add(fragment) 16 17 ' set boundaries of the text 18 multilineText.Width = page.Width / 4 19 multilineText.Height = 75 20 21 ' center 22 Dim translate As New TranslateTransform() 23 multilineText.Transform = translate 24 translate.X = (page.Width - multilineText.Width) / 2 25 translate.Y = (page.Height - multilineText.Height) / 2 + multilineText.Height 26 27 ' add a rectangle shape to visualize the boundary of the text 28 Dim rectangle As New RectangleShape(multilineText.X, multilineText.Y - multilineText.Height, multilineText.Width, multilineText.Height, Nothing, New SolidBrush(System.Drawing.Color.Salmon)) 29 30 ' add both shapes 31 page.Overlay.Add(rectangle) 32 page.Overlay.Add(multilineText) 33 34 Using file As New FileStream("..\..\autosizetext.pdf", FileMode.Create, FileAccess.Write) 35 document.Write(file) 36 End Using