This article demonstrates how to use the iTextSharp PDF Library to convert multiple text files into a single combined PDF file and also convert to individual PDF files with the same GUI.
Description
About the classes used:
- The StreamReader class provides read access using a Stream such as for a Text File.
- The Document class allows creation of a new PDF File.
- The PdfWriter class provides instantaneous access to write a PDF document from an object of the Document class.
Namespaces Required:
- System.IO,
- iTextSharp,
- iTextSharp.text,
- iTextSharp.text.pdf,
- iTextSharp.text.pdf.draw
Controls Used:
- TextBox Control (txtOutput)
- Button Control (btnSelect, btnCreatePDF, btnClear)
- ListBox Control (lbInputs)
- RadioButton Control (rbCombined, rbIndividual)
Here I implemented the code for converting multiple Text Documents into a single combined PDF and into individual PDFs using the iTextSharp Tool.
The Code
1. Variable Declaration
Document doc; //Create a New instance on Document Class
Listing 1
2. Select the Input Text Files (code for the "Select Files" Button); see:
using (OpenFileDialog file = new OpenFileDialog())
{
//Allow to select multiple files
file.Multiselect = true;
//Allow to select only *.txt Files
file.Filter = "Only Text Documents|*.txt";
//Show the Dialog box to selcet file(s)
file.ShowDialog();
//Add the input file names to ListBox
lbInputs.Items.AddRange(file.FileNames);
}
Listing 2
3. Function to Create a New instance of the PDFWriter Class:
void GetInstance(String str)
{
//Create a New instance of PDFWriter Class for Output File
PdfWriter.GetInstance(doc, new FileStream(str, FileMode.Create));
}
Listing 3
4. Convert Text Files into PDF File(s) (code for "Create PDF" Button):
//For Combined PDF Option is Selected
//Create a New instance on Document Class
doc = new Document();
//Create a new instance of PDFWriter Class for output file
GetInstance(txtOutput.Text);
//Open the Document
doc.Open();
foreach (String var in lbInputs.Items)
{
//Access Each file in ListBox using StreamReader Class
using (StreamReader rdr = new StreamReader(var))
{
//Add New Page to the Output file
doc.NewPage();
//Add the File name of text file to PDF File
doc.Add(new Paragraph("File Name : " + var + "\n\n"));
//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 4
//For Individual PDFs Option is Selected
//Create a Output Directory for storing individual PDF files
Directory.CreateDirectory(txtOutput.Text
foreach (String var in lbInputs.Items)
{
//Create a New instance on Document Class
doc = new Document();
//Create a New Instance of FileInfo Class
//to get the Extension of selected file
FileInfo file = new FileInfo(var);
//Create a new instance of PDFWriter Class for output file
GetInstance(txtOutput.Text +
file.Name.Replace(file.Extension, ".pdf"));
using (StreamReader rdr = new StreamReader(var))
{
//Open the Document
doc.Open();
//Add the content of Text File to PDF File
doc.Add(new Paragraph(rdr.ReadToEnd().ToString()));
//Close the Document
doc.Close();
}
}
MessageBox.Show("Conversion Successful....");
Listing 5
5. Now execute the application and see the result (Figure 1).
Intended Result
Figure 1
Summary
In this piece of writing, using the C# environment, we have seen how to convert Multiple Text files into a Single PDF file & into individual PDF files, using the iTextSharp Tool (a free PDF library ported from Java iText).