Home > DeveloperSection > Blogs > Convert Text Document to PDF File

Convert Text Document to PDF File

C# C#  .NET 
0 Comment(s)
 1378  View(s)
Rate this:

Convert Text Document to PDF File

In this blog I’m explaining about how to convert text document to pdf file in c#.


About classes used:-

1.       OpenFileDialog This class is use to open file dialog for select text file. that text file convert into pdf file.

2.       StreamReader This class provides an access to read the data from Stream such as Text File.

3.       Document This class allows creating a new instance for Creating PDF File.

4.       PdfWriter This class, an instantaneous access to write a PDF document from an object of Documentclass. 

About namespace used:-

1.  using iTextSharp.text;

2.  using iTextSharp.text.pdf;

3.  using System;

4.  using System.IO;

5.  using System.Threading;

6.       using System.Windows.Forms;

About controls used:-

1.       TextBox Control (txtInput, txtOutput)

2.       Button Control (btnSelect, btnCreatePDF)  

Here I’m implementing to convert text document into pdf file use iTextSharp dll.



  private void button1_Click(object sender, EventArgs e)


            var t = new Thread((ThreadStart)(() =>


                OpenFileDialog op = new OpenFileDialog();

                if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)


                    txtInput.Text = op.FileName;

                    txtOutput.Text = (txtInput.Text).Replace(".txt", ".pdf");







  private void button2_Click(object sender, EventArgs e)


            StreamReader rdr = new StreamReader(txtInput.Text);

            Document doc = new Document();


            PdfWriter.GetInstance(doc, new FileStream(txtOutput.Text, FileMode.Create));


            doc.Add(new Paragraph(rdr.ReadToEnd()));


            MessageBox.Show("Conversion Successful....");




  private void Form1_Load(object sender, EventArgs e)


            Form1.CheckForIllegalCrossThreadCalls = false;




in my next post i'll explain about Substring methods in C#

Don't want to miss updates? Please click the below button!

Follow MindStick