Click here to Skip to main content
15,885,365 members
Articles / Web Development / HTML

iTextSharp: Convert a Text File to PDF While Uploading in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
18 Feb 2015CPOL 22.4K   12   9
iTextSharp: Convert a text file to PDF while uploading in ASP.NET

iTextSharp is the most popular PDF Library to create, modify and do some additional manipulation with PDF files. In this article, I’m going to explain how to convert a text file to PDF while uploading.

Ground Work

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

Image 1

Image 2

Image 3

Code

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

HTML
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl" runat="server" 
            Text="Select a file to upload:"></asp:Label>
            <asp:FileUpload runat="server" ID="fu" /><br />
            <asp:Button runat="server" ID="btnUpload" 
            Text="Upload" OnClick="btnUpload_Click" />
        </div>
    </form>
</body>
</html>

Also the code behind contains the below code:

C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            // Check that upload control had file
            if(fu.HasFile)
            {
                // Get the Posted File
                HttpPostedFile pf = fu.PostedFile;
                Int32 fileLen;
                // Get the Posted file Content Length
                fileLen = fu.PostedFile.ContentLength;
                // Create a byte array with content length
                Byte[] Input = new Byte[fileLen];
                // Create stream
                System.IO.Stream myStream;
                 // get the stream of uploaded file
                myStream = fu.FileContent;
                // Read from the stream
                myStream.Read(Input, 0, fileLen);
                // Create a Document
                Document doc = new Document();
                // create PDF File and create a writer on it
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream
                (string.Concat(Server.MapPath("~/Pdf/PdfSample"), 
                ".pdf"), FileMode.Create));
                // open the document
                doc.Open();
                // Add the text file contents
                doc.Add(new Paragraph(System.Text.Encoding.Default.GetString(Input)));
                // Close the document
                doc.Close();
            }
        }

When you run the application, it will display an upload control and a button to upload.
After the conversion, the PDF file stored under the “PDF” folder. As per the code, it is necessary to create the folder named “PDF” before you run the application, within your solution.

 

 

Image 4

 

 

Image 5

Image 6

Happy coding!!!

Image 7

This article was originally posted at http://www.aspdotnetchamp.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hi All, I am Francis from India. Working as a Senior Software Engineer. I have 6+ Years of experience in ASP.Net and C#/VB.Net. Also founder of ASPDotNetChamp, where i blog my day to day ASP.Net Experiences, Problems and Interview Questions.

Comments and Discussions

 
Questiongeneration of pdf Pin
Member 1172706828-May-15 16:25
Member 1172706828-May-15 16:25 
Questionusing iText- PDF Form Pin
rbama19-Apr-15 17:26
rbama19-Apr-15 17:26 
GeneralTake Care of AGPL Pin
tngraf00124-Feb-15 1:10
tngraf00124-Feb-15 1:10 
GeneralRe: Take Care of AGPL Pin
Fannie0823-Apr-18 3:53
Fannie0823-Apr-18 3:53 
GeneralRe: Take Care of AGPL Pin
tngraf0013-Apr-18 20:47
tngraf0013-Apr-18 20:47 
GeneralMy vote of 4 Pin
mmosoll20-Feb-15 2:25
mmosoll20-Feb-15 2:25 
GeneralRe: My vote of 4 Pin
Francis S Michael20-Feb-15 13:32
professionalFrancis S Michael20-Feb-15 13:32 
GeneralMy vote of 3 Pin
pravin prajapati19-Feb-15 21:22
professionalpravin prajapati19-Feb-15 21:22 
GeneralRe: My vote of 3 Pin
Francis S Michael20-Feb-15 13:30
professionalFrancis S Michael20-Feb-15 13:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.