Convert Text File To PDF File And ViceVersa Using ItextSharp

Text File to PDF File

 
Here we will convert a text file to a pdf file.
 
ItextSharp is a .Net Pdf library for pdf conversion. Take a new solution in solution explorer and you can either add itextsharp dll by using nuget package or by using package manager console or you can directly add dll in references. In the below picture I show you an example of how to install itextsharp using the nuget package.
 
 
After you install itextsharp in your solution, please check your references to make sure that it exists.
 
 
Add two new folders in your solution and give the name “SourceFiles” and “DestFiles”. In “SourceFiles”, we will store the text (.txt) file and after conversion, the pdf file will be stored in “DestFiles”. The folder structure will look like this,
 
 
After that design your UI like this,
 
When I choose some text file and click on Import then the text file will be imported in “SourceFiles” and the code looks like this,
  1. protected void btnImport_Click(object sender, EventArgs e)  
  2.         {  
  3.             HttpPostedFile file = HttpContext.Current.Request.Files[0];  
  4.             string sourceFiles = HttpContext.Current.Server.MapPath("~/SourceFiles/"+flupload.FileName);  
  5.             file.SaveAs(sourceFiles);  
  6.         }  
In the import click event, we get the details of the .txt file in the HttpPostedFile object. After that, we created the SourceFiles where we will get the full path of the SourceFiles and we finally save that file in the SourceFiles location.
 
When I click on the Convert button then it will retrieve the .txt file from the “SourceFiles” folder and convert it into pdf file and stored it into “DestFiles” and the code looks like this,
  1. protected void btnConvert_Click(object sender, EventArgs e)  
  2.         {  
  3.             string sourceFilePath = HttpContext.Current.Server.MapPath("~/SourceFiles/");  
  4.             string[] fileNames = System.IO.Directory.GetFiles(sourceFilePath);  
  5.             string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(fileNames[0]);  
  6.             string outputFilePath = HttpContext.Current.Server.MapPath("~/DestFiles/"+fileNameWithoutExt+".pdf");  
  7.             StreamReader str = new StreamReader(fileNames[0]);  
  8.             Document doc = new Document();  
  9.             PdfWriter.GetInstance(doc, new FileStream(outputFilePath, FileMode.Create));  
  10.             doc.Open();  
  11.             doc.Add(new Paragraph(str.ReadToEnd()));  
  12.             doc.Close();  
  13.         }  
In the first line, we will get the SourceFiles full path and in the next line will retrieve all the files by using sourceFilePath. After that we will retrieve only the filename which will be used to set the outputFile . In the next line, we will set the OutputFilePath with filename as well as an extension as “.pdf”. In the StreamReader object will read characters from the text file. After that, we create a document object. In the next line, we will create the pdf file in the destination folder. However, we will open the document object and add the paragraph until the stream reader reaches to the end, and at last, we will close the document.
 
The Source folder looks like this,
 
 
 
And the Destination folder looks like this,
 
 

PDF File to TEXT File

 
Here we will convert Pdf file to a text file.
 
Take a new solution and add ItextSharp dll using the manage nuget package. Add two new folders “SourceFiles” and “DestFiles” inside the solution explorer. Design our UI the same as text to pdf conversion.
 
Now choose some pdf file and click on import then the pdf file will be imported in “SourceFiles” and whose code looks like this,
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2.         {  
  3.             HttpPostedFile file = HttpContext.Current.Request.Files[0];  
  4.             string filename = HttpContext.Current.Server.MapPath("~/SourceFiles/" + flUploadPdf.FileName);  
  5.             Session["fName"] = flUploadPdf.FileName;  
  6.             file.SaveAs(filename);  
  7.         }  
In the upload click event, we get the details of the .pdf file in the HttpPostedFile object. After that, we created the SourceFiles where we will get the full path of the SourceFiles and we finally save that file in the SourceFiles location. 
 
When I click on the convert button then it will retrieve the .pdf file from the “SourceFiles” folder and convert it into a text file and stored it in the “DestinationFiles” folder and whose code looks like this,
  1. protected void btnCovert_Click(object sender, EventArgs e)  
  2.         {  
  3.             string SourcePdfPath = HttpContext.Current.Server.MapPath("~/SourceFiles/" + Session["fName"]);  
  4.             string outputPath = HttpContext.Current.Server.MapPath("~/DestinationFiles/");  
  5.             StringBuilder text = new StringBuilder();  
  6.             using (PdfReader reader = new PdfReader(SourcePdfPath))  
  7.             {  
  8.                 for (int i = 1; i <= reader.NumberOfPages; i++)  
  9.                 {  
  10.                     text.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i));  
  11.                 }  
  12.             }  
  13.             using (StreamWriter outputFile = new StreamWriter(System.IO.Path.Combine(outputPath, "Pdf2Text.txt")))  
  14.             {  
  15.                 outputFile.WriteLine(text);  
  16.             }  
  17.         }  
In the first line, we will get the SourceFiles full path. In the next line, we will set the OutputPath. After that by using PdfReader will read the pdf and append it to the StringBuilder object. At last, we will write that text into the StreamWriter object, which will create a text file in our outputpath.
 
The Source folder looks like this,
 
And the Destination folder looks like this,