Blog

Convert Text Document to PDF File

Posted by Mayur Dighe Blogs | Visual C# Sep 08, 2012
The Blog demonstrate a method to convert the text files into PDF files using iTextSharp PDF library.
Download Files: TextToPdf.rar

Description: 

About Classes used - 

#1: StreamReader class provides an access to read the data from Stream such as Text File.

#2: Document class allows creating a new instance for Creating PDF File.

#3: PdfWriter class, an instantaneous access to write a PDF document from an object of Document class. 

Namespace Required         -           System.IO,

iTextSharp,

iTextSharp.text,

iTextSharp.text.pdf 

Controls Used           -          

            1. TextBox Control (txtInput, txtOutput)

          2. Button Control (btnSelect, btnCreatePDF)         

Here I implemented the Code for converting Text Document into PDF Document using iTextSharp Tool.           

The Code:

1. Select the Text File (code for “Select File” Button).           

using (OpenFileDialog file = new OpenFileDialog())
{
    //Show the Dialog Box & Select the File
    file.ShowDialog();
    //Assign Input FileName to TextBox
    txtInput.Text = file.FileName;
}

//Assign Output FileName to TextBox

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

Listing 1

2. Convert Text File into a PDF File (code for “Create PDF” Button).

//Read the Data from Input File

StreamReader rdr = new StreamReader(txtInput.Text);

//Create a New instance on Document Class

Document doc = new Document();

//Create a New instance of PDFWriter Class for Output File

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

//Open the Document

doc.Open();

//Add the content of Text File to PDF File

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

//Close the Document

doc.Close();

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

//Open the Converted PDF File

System.Diagnostics.Process.Start(txtOutput.Text);

Listing 2

3. Now execute the Application and see the result (Figure 1).  

Intended Result:

New Picture (1).png

Figure 1 

Summary:

In this piece of writing, using C# environment, we have seen how to convert the Text file into PDF file using iTextSharp Tool (A free PDF library ported from Java iText).

post comment
     

Try using online document converter like Saaspose.PDF (http://saaspose.com/api/pdf ) that can convert your txt file to PDF and vice versa and also to many other formats.

Posted by zarfishan zahid Nov 06, 2012

Hey Kianu....Thanx for the Compliment...

Posted by Mayur Dighe Sep 26, 2012

Your concepts were simple to understand that I wondered why I in no way looked at it prior to. http://www.dapfor.com/en/net-suite/net-grid/features/data-source-binding

Posted by kianu rieves Sep 24, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter