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 the Nuget package or by using the 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.
protected void btnImport_Click(object sender, EventArgs e)
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
string sourceFiles = HttpContext.Current.Server.MapPath("~/SourceFiles/" + flupload.FileName);
file.SaveAs(sourceFiles);
}
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 convert it into a pdf file and store it into “DestFiles” the code looks like this.
protected void btnConvert_Click(object sender, EventArgs e)
{
string sourceFilePath = HttpContext.Current.Server.MapPath("~/SourceFiles/");
string[] fileNames = System.IO.Directory.GetFiles(sourceFilePath);
string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(fileNames[0]);
string outputFilePath = HttpContext.Current.Server.MapPath("~/DestFiles/" + fileNameWithoutExt + ".pdf");
using (StreamReader str = new StreamReader(fileNames[0]))
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(outputFilePath, FileMode.Create));
doc.Open();
doc.Add(new Paragraph(str.ReadToEnd()));
doc.Close();
}
}
In the first line, we will get the SourceFiles full path and retrieve all the files by using sourceFilePath in the next line. After that, we will retrieve only the filename which will be used to set the output file. In the next line, we will set the OutputFilePath with the filename as well as an extension as “.pdf”. 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 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 a 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.
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
string filename = HttpContext.Current.Server.MapPath("~/SourceFiles/" + flUploadPdf.FileName);
Session["fName"] = flUploadPdf.FileName;
file.SaveAs(filename);
}
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 convert it into a text file and store it in the “DestinationFiles” folder whose code looks like this,
protected void btnCovert_Click(object sender, EventArgs e)
{
string SourcePdfPath = HttpContext.Current.Server.MapPath("~/SourceFiles/" + Session["fName"]);
string outputPath = HttpContext.Current.Server.MapPath("~/DestinationFiles/");
StringBuilder text = new StringBuilder();
using (PdfReader reader = new PdfReader(SourcePdfPath))
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i));
}
}
using (StreamWriter outputFile = new StreamWriter(System.IO.Path.Combine(outputPath, "Pdf2Text.txt")))
{
outputFile.WriteLine(text);
}
}
In the first line, we will get the SourceFiles full path. In the next line, we will set the OutputPath. After that 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.