c# convert TXT to PDF

Generate PDF
9/17/2010

This article shows how to convert a plain .txt to PDF in c#.

Download ready-to-run Visual Studio 2008 project.

1 using System; 2 using System.IO; 3 4 using TallComponents.PDF.Layout; 5 using TallComponents.PDF.Layout.Paragraphs; 6 using TallComponents.PDF.Layout.Fonts; 7 8 namespace txt2pdf 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 Document document = new Document(); 15 16 Section section = new Section(); 17 section.PageSize = PageSize.Letter; 18 document.Sections.Add(section); 19 20 string[] lines = File.ReadAllLines("test.txt"); 21 22 foreach (string line in lines) 23 { 24 TextParagraph text = new TextParagraph(); 25 text.SpacingAfter = 12; 26 section.Paragraphs.Add(text); 27 28 Fragment fragment = new Fragment(); 29 fragment.FontSize = 12; 30 fragment.Font = Font.Courier; 31 fragment.Text = line; 32 text.Fragments.Add(fragment); 33 } 34 35 using (FileStream file = new FileStream( 36 "test.pdf", FileMode.Create, FileAccess.Write)) 37 { 38 document.Write(file); 39 } 40 } 41 } 42 }