Text formatting

Generate PDF, Colors, Fonts
7/4/2014

This code sample shows how to format your text

1 using( FileStream file = new FileStream( @"..\..\TextFormatting.pdf", FileMode.Create, 2 FileAccess.Write ) ) 3 { 4 Document document = new Document(); 5 Section section = document.Sections.Add(); 6 section.PageSize = PageSize.A4; 7 8 TextParagraph textParagraph; 9 10 // Just display the dummy text without setting any additional properties 11 section.Paragraphs.Add( CreateTextParagraph( "TextParagraph with default properties:" ) ); 12 section.Paragraphs.Add( CreateTextParagraph( text, System.Drawing.Color.Blue ) ); 13 14 // Set alignment to right 15 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.HorizontalAlignment = "+ 16 "HorizontalAlignment.Right" ) ); 17 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 18 textParagraph.HorizontalAlignment = HorizontalAlignment.Right; 19 section.Paragraphs.Add( textParagraph ); 20 21 // Set alignment to center 22 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.HorizontalAlignment = "+ 23 "HorizontalAlignment.Center" ) ); 24 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 25 textParagraph.HorizontalAlignment = HorizontalAlignment.Center; 26 section.Paragraphs.Add( textParagraph ); 27 28 // Display the textparagraph justified 29 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.Justified = true" ) ); 30 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 31 textParagraph.Justified = true; 32 section.Paragraphs.Add( textParagraph ); 33 34 // Set linespacing 35 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.LineSpacing = 5" ) ); 36 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 37 textParagraph.LineSpacing = 5; 38 section.Paragraphs.Add( textParagraph ); 39 40 // Use First Line indentation 41 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.FirstLineIndentation = 20" ) ); 42 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 43 textParagraph.FirstLineIndentation = 20; 44 section.Paragraphs.Add( textParagraph ); 45 46 // Use hang indentation 47 section.Paragraphs.Add( CreateTextParagraph( "textParagraph.HangIndentation = 20" ) ); 48 textParagraph = CreateTextParagraph( text, System.Drawing.Color.Blue ); 49 textParagraph.HangIndentation = 20; 50 section.Paragraphs.Add( textParagraph ); 51 52 // Use different font 53 section.Paragraphs.Add(CreateTextParagraph("textParagraph.HangIndentation = 20")); 54 textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue,Font.TimesRoman, 28.0); 55 section.Paragraphs.Add(textParagraph); 56 57 document.Write( file ); 58 } 59 } 60 61 private static TextParagraph CreateTextParagraph( string text ) 62 { 63 return CreateTextParagraph( text, System.Drawing.Color.Black ); 64 } 65 66 private static TextParagraph CreateTextParagraph( string text, System.Drawing.Color textcolor ) 67 { 68 TextParagraph paragraph = new TextParagraph(); 69 Fragment textFragment = new Fragment( text ); 70 textFragment.TextColor = textcolor; 71 paragraph.Fragments.Add( textFragment ); 72 paragraph.SpacingAfter = 10; 73 return paragraph; 74 } 75 76 private static TextParagraph CreateTextParagraph(string text, System.Drawing.Color textcolor, 77 Font f, double fsize) 78 { 79 TextParagraph paragraph = new TextParagraph(); 80 Fragment textFragment = new Fragment(text); 81 textFragment.Font = f; 82 textFragment.FontSize = fsize; 83 textFragment.TextColor = textcolor; 84 paragraph.Fragments.Add(textFragment); 85 paragraph.SpacingAfter = 10; 86 return paragraph; 87 }