Text alignment and docking styles

Fonts, Manipulate PDF
7/17/2015

This sample shows how to use text alignment and how docking works.

Docking refers to the placement of shapes in a container; in this sample we will be placing several text fragments in a vertical manner using the docking style DockStyle.Top

C# code sample

1 // create a new document 2 Document document = new Document(); 3 4 // append a new page (portrait Letter) 5 bool landscape = false; 6 Page page = new Page( PageSize.Letter, landscape ); 7 document.Pages.Add( page ); 8 9 // add a shapes collection that will be the container for multiline text shapes 10 ShapeCollection shapes = new ShapeCollection(); 11 TranslateTransform translate = new TranslateTransform(); 12 shapes.Transform = translate; 13 shapes.Width = page.Width / 2; 14 shapes.Height = page.Height * 0.85; 15 translate.X = (page.Width - shapes.Width) / 2; 16 translate.Y = (page.Height - shapes.Height) / 2; 17 page.Overlay.Add( shapes ); 18 19 // add a rectangle shape to visualize the container 20 // note that the rectangle is positioned relative to its container, not the page 21 RectangleShape rectangle = new RectangleShape( 22 0, 0, 23 shapes.Width, shapes.Height, 24 null, new SolidBrush( System.Drawing.Color.Salmon ) ); 25 shapes.Add( rectangle ); 26 27 // dock text to the top of the shapes collection - align left 28 MultilineTextShape multilineText = new MultilineTextShape(); 29 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 30 multilineText.Dock = DockStyle.Top; 31 multilineText.Margin.Bottom = 10; 32 shapes.Add( multilineText ); 33 34 // dock text to the top of the shapes collection - align center 35 multilineText = new MultilineTextShape(); 36 multilineText.HorizontalAlignment = HorizontalAlignment.Center; 37 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 38 multilineText.Dock = DockStyle.Top; 39 multilineText.Margin.Bottom = 10; 40 shapes.Add( multilineText ); 41 42 // dock text to the top of the shapes collection - align right 43 multilineText = new MultilineTextShape(); 44 multilineText.HorizontalAlignment = HorizontalAlignment.Right; 45 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 46 multilineText.Dock = DockStyle.Top; 47 multilineText.Margin.Bottom = 10; 48 shapes.Add( multilineText ); 49 50 // dock text to the top of the shapes collection - justify 51 multilineText = new MultilineTextShape(); 52 multilineText.Justified = true; 53 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 54 multilineText.Dock = DockStyle.Top; 55 multilineText.Margin.Bottom = 10; 56 shapes.Add( multilineText ); 57 58 multilineText = new MultilineTextShape(); 59 multilineText.Justified = true; 60 multilineText.JustifyLastLine = true; 61 multilineText.Fragments.Add( new Fragment(text, Font.TimesRoman, 16) ); 62 multilineText.Dock = DockStyle.Top; 63 multilineText.Margin.Bottom = 10; 64 shapes.Add(multilineText); 65 66 // dock text to the top of the shapes collection - line spacing 67 multilineText = new MultilineTextShape(); 68 multilineText.LineSpacing = 5; 69 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 70 multilineText.Dock = DockStyle.Top; 71 multilineText.Margin.Bottom = 10; 72 shapes.Add( multilineText ); 73 74 // dock text to the top of the shapes collection - first line indentation 75 multilineText = new MultilineTextShape(); 76 multilineText.FirstLineIndentation = 20; 77 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 78 multilineText.Dock = DockStyle.Top; 79 multilineText.Margin.Bottom = 10; 80 shapes.Add( multilineText ); 81 82 // dock text to the top of the shapes collection - hang indentation 83 multilineText = new MultilineTextShape(); 84 multilineText.HangIndentation = 20; 85 multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) ); 86 multilineText.Dock = DockStyle.Top; 87 multilineText.Margin.Bottom = 10; 88 shapes.Add( multilineText ); 89 90 using ( FileStream file = new FileStream( @"..\..\dockmultilinetext.pdf", FileMode.Create, FileAccess.Write ) ) 91 { 92 document.Write( file ); 93 }

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 Shared Sub Main() 4 ' create a new document 5 Dim document As New Document() 6 7 ' append a new page (portrait Letter) 8 Dim landscape As Boolean = False 9 Dim page As New Page(PageSize.Letter, landscape) 10 document.Pages.Add(page) 11 12 ' add a shapes collection that will be the container for multiline text shapes 13 Dim shapes As New ShapeCollection() 14 Dim translate As New TranslateTransform() 15 shapes.Transform = translate 16 shapes.Width = page.Width / 2 17 shapes.Height = page.Height * 0.8 18 translate.X = (page.Width - shapes.Width) / 2 19 translate.Y = (page.Height - shapes.Height) / 2 20 page.Overlay.Add(shapes) 21 22 ' add a rectangle shape to visualize the container 23 ' note that the rectangle is positioned relative to its container, not the page 24 Dim rectangle As New RectangleShape(0, 0, shapes.Width, shapes.Height, Nothing, New SolidBrush(System.Drawing.Color.Salmon)) 25 shapes.Add(rectangle) 26 27 ' dock text to the top of the shapes collection - align left 28 Dim multilineText As New MultilineTextShape() 29 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 30 multilineText.Dock = DockStyle.Top 31 multilineText.Margin.Bottom = 10 32 shapes.Add(multilineText) 33 34 ' dock text to the top of the shapes collection - align center 35 multilineText = New MultilineTextShape() 36 multilineText.HorizontalAlignment = HorizontalAlignment.Center 37 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 38 multilineText.Dock = DockStyle.Top 39 multilineText.Margin.Bottom = 10 40 shapes.Add(multilineText) 41 42 ' dock text to the top of the shapes collection - align right 43 multilineText = New MultilineTextShape() 44 multilineText.HorizontalAlignment = HorizontalAlignment.Right 45 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 46 multilineText.Dock = DockStyle.Top 47 multilineText.Margin.Bottom = 10 48 shapes.Add(multilineText) 49 50 ' dock text to the top of the shapes collection - justify 51 multilineText = New MultilineTextShape() 52 multilineText.Justified = True 53 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 54 multilineText.Dock = DockStyle.Top 55 multilineText.Margin.Bottom = 10 56 shapes.Add(multilineText) 57 58 ' dock text to the top of the shapes collection - line spacing 59 multilineText = New MultilineTextShape() 60 multilineText.LineSpacing = 5 61 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 62 multilineText.Dock = DockStyle.Top 63 multilineText.Margin.Bottom = 10 64 shapes.Add(multilineText) 65 66 ' dock text to the top of the shapes collection - first line indentation 67 multilineText = New MultilineTextShape() 68 multilineText.FirstLineIndentation = 20 69 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 70 multilineText.Dock = DockStyle.Top 71 multilineText.Margin.Bottom = 10 72 shapes.Add(multilineText) 73 74 ' dock text to the top of the shapes collection - hang indentation 75 multilineText = New MultilineTextShape() 76 multilineText.HangIndentation = 20 77 multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16)) 78 multilineText.Dock = DockStyle.Top 79 multilineText.Margin.Bottom = 10 80 shapes.Add(multilineText) 81 82 Using file As New FileStream("..\..\dockmultilinetext.pdf", FileMode.Create, FileAccess.Write) 83 document.Write(file) 84 End Using 85 End Sub