Layout text

Generate PDF
7/4/2014

This code sample shows how to use document layout options

1 using(FileStream file = new FileStream("LayoutText.pdf", FileMode.Create, FileAccess.Write ) ) 2 { 3 // create a new document 4 Document document = new Document(); 5 6 // add a section that has A4-sized pages 7 Section section = document.Sections.Add(); 8 section.PageSize = PageSize.A4; 9 10 // set left and right margin to 150 points which equals to 2.08 inch or 5.29 cm 11 section.Margin.Left = 150; 12 section.Margin.Right = 150; 13 14 TextParagraph textParagraph = new TextParagraph(); 15 textParagraph.SpacingBefore = 10; 16 textParagraph.Justified = true; 17 textParagraph.Fragments.Add(new Fragment("Left and right margin are set to 150 points.")); 18 section.Paragraphs.Add( textParagraph ); 19 20 // left aligned paragraph 21 textParagraph = new TextParagraph(); 22 textParagraph.SpacingBefore = 10; 23 textParagraph.Border = new Border(new SolidBrush(System.Drawing.Color.LightBlue)); 24 textParagraph.HorizontalAlignment = HorizontalAlignment.Left; 25 textParagraph.Fragments.Add( new Fragment( "Left" ) ); 26 section.Paragraphs.Add( textParagraph ); 27 28 // center aligned paragraph 29 textParagraph = new TextParagraph(); 30 textParagraph.SpacingBefore = 10; 31 textParagraph.Border = new Border( new SolidBrush( System.Drawing.Color.LightBlue ) ); 32 textParagraph.HorizontalAlignment = HorizontalAlignment.Center; 33 textParagraph.Fragments.Add( new Fragment( "Center" ) ); 34 section.Paragraphs.Add( textParagraph ); 35 36 // right aligned paragraph 37 textParagraph = new TextParagraph(); 38 textParagraph.SpacingBefore = 10; 39 textParagraph.Border = new Border( new SolidBrush( System.Drawing.Color.LightGreen ) ); 40 textParagraph.HorizontalAlignment = HorizontalAlignment.Right; 41 textParagraph.Fragments.Add( new Fragment( "Right" ) ); 42 section.Paragraphs.Add( textParagraph ); 43 44 document.Write( file ); 45 }