---
title: "Convert Text Document to PDF File"  
description: "In this blog I’m explaining about how to convert text document to pdf file in c#.Description:-About classes used:-1.OpenFileDialogThis class is use to"  
author: "Anonymous User"  
published: 2014-10-14  
updated: 2014-10-14  
canonical: https://www.mindstick.com/blog/698/convert-text-document-to-pdf-file  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# Convert Text Document to PDF File

In this blog I’m explaining about how to [convert text](https://www.mindstick.com/forum/12781/convert-text-to-image) document to pdf [file in c#](https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp).

Description:-

##### About classes used:-

1. [OpenFileDialog](https://www.mindstick.com/forum/1564/use-openfiledialog-to-get-path-of-open-database-c-sharp) This class is use to open file dialog for select [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file).that text file convert into [pdf file](https://www.mindstick.com/forum/572/pdf-file-download-in-zip-file-in-mvc-4).

\

2. [StreamReader](https://www.mindstick.com/forum/1934/check-if-a-streamreader-can-read-another-line) This class provides an access to read the data from Stream such as Text File.

\
3. Document This class allows creating a new [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) for Creating PDF File.

\
4. PdfWriter This class, an instantaneous access to write a [PDF document](https://answers.mindstick.com/qa/96548/how-to-edit-pdf-document-in-ms-word-2013) 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.

##### Button1:

```
  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");                }            }));            t.SetApartmentState(ApartmentState.STA);            t.Start();        }
```

##### Button2:

```
  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.Open();            doc.Add(new Paragraph(rdr.ReadToEnd()));            doc.Close();            MessageBox.Show("Conversion Successful....");            System.Diagnostics.Process.Start(txtOutput.Text);        }
```

##### Form_Load:

```
  private void Form1_Load(object sender, EventArgs e)        {            Form1.CheckForIllegalCrossThreadCalls = false;        }
```

##### Output:

![Convert Text Document to PDF File](https://www.mindstick.com/blogs/45f3d223-13b9-4dc4-a704-c5f58ec80f2b/images/17e05fa3-e796-429d-ada3-f77c7f73db1c.png)

in my next post i'll explain about [Substring methods in C#](https://www.mindstick.com/blog/702/Substring%20methods%20in%20C#.VPachfmUdz-)

---

Original Source: https://www.mindstick.com/blog/698/convert-text-document-to-pdf-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
