Click here to Skip to main content
Click here to Skip to main content

Convert text into PDF using ASP.NET and C#

, 16 Dec 2011 CPOL
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.
Follow on   LinkedIn

Comments and Discussions

GeneralThanks PinmemberUMRINDERPAL4-Jan-15 0:50 
QuestionHow to write Dynamically created japanese to pdf PinmemberSneha Gupta19-Jun-14 22:42 
Questionthanks for sharing Pinmemberjacaboo29-Aug-13 21:51 
QuestionNot able to parse background color style Pinmemberrrrattling2-May-13 1:14 
AnswerRe: Not able to parse background color style PinmemberSayed Irfanul Hasan2-May-13 1:53 
GeneralMy vote of 5 PinmemberMd. Humayun Rashed29-Jul-12 20:31 
Suggestionpowerful c# ASP.NET library to convert anything a browser can render to PDF, SVG or IMAGE Pinmembernetsoftrev4-Mar-12 10:10 
GeneralRe: powerful c# ASP.NET library to convert anything a browser can render to PDF, SVG or IMAGE PinmemberMember 1078543530-Apr-14 0:55 
Generalvery good explanation......... Pinmembervempallibavaji16-Dec-11 19:59 
GeneralReason for my vote of 5 nice work PinmemberBilal Ahmed Lilla27-Nov-11 19:30 

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

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

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