Convert a Text File To a PDF When Uploading in ASP.Net

 iTextSharp is a very popular PDF Library to create, modify and do some additional manipulation with PDF files. This article explains how to convert a text file to a PDF when uploading.

Ground work

Before starting we need to download the iTextSharp PDF library from this URL. Alternatively we can download it from the NuGet Gallary into your project solution using the “NuGet Package Manager” if you are using Visual Studio. This can be depicted below with screen shots.

 
 
 

Code

For the sake of simplicity, I have designed the webform with a upload control and a button. So the markup looks like this:

  1. <!DOCTYPE html>  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:Label ID="lbl" runat="server" Text="Select a file to upload:"></asp:Label>  
  11.             <asp:FileUpload runat="server" ID="fu" /><br />  
  12.             <asp:Button runat="server" ID="btnUpload" Text="Upload" OnClick="btnUpload_Click" />  
  13.         </div>  
  14.     </form>  
  15. </body>  
  16. </html> 
Also the code behind contains the following code:
  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2.         {  
  3.             // Check that upload control had file  
  4.             if(fu.HasFile)  
  5.             {  
  6.                 // Get the Posted File  
  7.                 HttpPostedFile pf = fu.PostedFile;  
  8.                 Int32 fileLen;  
  9.                 // Get the Posted file Content Length  
  10.                 fileLen = fu.PostedFile.ContentLength;  
  11.                 // Create a byte array with content length  
  12.                 Byte[] Input = new Byte[fileLen];  
  13.                 // Create stream   
  14.                 System.IO.Stream myStream;  
  15.                  // get the stream of uploaded file  
  16.                 myStream = fu.FileContent;  
  17.                 // Read from the stream  
  18.                 myStream.Read(Input, 0, fileLen);  
  19.                 // Create a Document  
  20.                 Document doc = new Document();  
  21.                 // create PDF File and create a writer on it  
  22.                 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(string.Concat(Server.MapPath("~/Pdf/PdfSample"), ".pdf"), FileMode.Create));  
  23.                 // open the document  
  24.                 doc.Open();  
  25.                 // Add the text file contents   
  26.                 doc.Add(new Paragraph(System.Text.Encoding.Default.GetString(Input)));  
  27.                 // Close the document  
  28.                 doc.Close();  
  29.             }  
  30.         } 
When you run the application, it will display an upload control and a button to upload. After the conversion, the PDF file is stored under the “PDF” folder. As in the code it is necessary to create the folder named “PDF” before running the application within your solution.
 
Output
 
 
 


Similar Articles