Click here to Skip to main content
13,410,713 members (32,979 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 9 Nov 2011

Convert text into PDF using ASP.NET and C#

, 16 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Text to PDF Convert

Quote: First of all, download the DLL file (itextsharp). You can download it from here. Then open a new project in ASP.NET where language is C# .NET. Right click on the project name and select "Add Reference". Then browse the DLL file. Now go to your .aspx page and drag a label or textbox and a button. Our aim is, there will be some text in the label or textbox and when we click the button, it will create a PDF file having the text in it. Here, I am using a Label for HTML format and my label name is lblArticle. And I have created a folder in my project named pdf.

Now go to your .cs page, add the namespace, and copy the following code:

using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml; 

Then go to your button click event and copy the following code:

Document document = new Document();

    PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/") + "pdf/" + "print.pdf", FileMode.Create));

    List<ielement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(
                      new StringReader(lblArticle.Text), null);

    for (int k = 0; k < htmlarraylist.Count; k++)


    Paragraph mypara = new Paragraph(); 


catch (Exception ex)
    lblArticle.Text = ex.Message;


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


About the Author

Sayed Irfanul Hasan
Software Developer (Senior) Grameen Communications
Bangladesh Bangladesh
I have been working in .Net platform over 4 years, I am expert in both Desktop and Web Application. I used both C# and VB for my code. I love to face challenge in coding, because I always believe that "Failure doesn't kill you... it increases your desire to make something happen.

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
UMRINDERPAL4-Jan-15 0:50
memberUMRINDERPAL4-Jan-15 0:50 
QuestionHow to write Dynamically created japanese to pdf Pin
Sneha Gupta19-Jun-14 22:42
memberSneha Gupta19-Jun-14 22:42 
QuestionMessage Closed Pin
29-Aug-13 21:51
memberjacaboo29-Aug-13 21:51 
QuestionNot able to parse background color style Pin
rrrattling2-May-13 1:14
memberrrrattling2-May-13 1:14 
AnswerRe: Not able to parse background color style Pin
Sayed Irfanul Hasan2-May-13 1:53
memberSayed Irfanul Hasan2-May-13 1:53 
GeneralMy vote of 5 Pin
Md. Humayun Rashed29-Jul-12 20:31
memberMd. Humayun Rashed29-Jul-12 20:31 
SuggestionMessage Closed Pin
4-Mar-12 10:10
membernetsoftrev4-Mar-12 10:10 
GeneralMessage Closed Pin
30-Apr-14 0:55
memberMember 1078543530-Apr-14 0:55 
Generalvery good explanation......... Pin
vempallibavaji16-Dec-11 19:59
membervempallibavaji16-Dec-11 19:59 
GeneralReason for my vote of 5 nice work Pin
Bilal Ahmed Lilla27-Nov-11 19:30
memberBilal Ahmed Lilla27-Nov-11 19:30 
GeneralBe careful about using iTextSharp. It has some rather unusua... Pin
DaveRRR21-Nov-11 10:04
memberDaveRRR21-Nov-11 10:04 
Generalielement error "the type or namespace could not be found" ho... Pin
eku12318-Nov-11 19:44
membereku12318-Nov-11 19:44 
GeneralRe: At first you have to download the itextsharp.dll file. You c... Pin
Sayed Irfanul Hasan18-Nov-11 19:54
memberSayed Irfanul Hasan18-Nov-11 19:54 
GeneralRe: Add NameSpace using System.Collections.Generic; and replac... Pin
KAMAL BHARDWAJ13-Dec-11 21:42
memberKAMAL BHARDWAJ13-Dec-11 21:42 
GeneralReason for my vote of 4 Concise and well written. Screen sho... Pin
BrianBissell16-Nov-11 3:02
memberBrianBissell16-Nov-11 3:02 
GeneralBut how to COnvert HTML page into PDF ???????? Pin
lalupatel12-Nov-11 2:01
memberlalupatel12-Nov-11 2:01 
GeneralRe: The title says "Convert text into PDF using ASP.NET and C#" ... Pin
gorgias9915-Dec-11 23:43
membergorgias9915-Dec-11 23:43 
GeneralRe: good.... Pin
vempallibavaji16-Dec-11 19:58
membervempallibavaji16-Dec-11 19:58 
GeneralRe: But how to COnvert HTML page into PDF ???????? Pin
RedDot Vasilyev2-Dec-13 0:23
memberRedDot Vasilyev2-Dec-13 0:23 
Generalassa Pin
lalupatel12-Nov-11 2:00
memberlalupatel12-Nov-11 2:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180221.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Sayed Irfanul Hasan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid