Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Tip/Trick

Convert text into PDF using ASP.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.54/5 (13 votes)
16 Dec 2011CPOL 99.2K   40   20
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:


C#
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:


C#
Document document = new Document();

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

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

    for (int k = 0; k < htmlarraylist.Count; k++)
    {
        document.Add((IElement)htmlarraylist[k]);

    }

    Paragraph mypara = new Paragraph(); 
    document.Add(mypara);

    document.Close();

    Response.Redirect("~/pdf/print.pdf");
}
catch (Exception ex)
{
    lblArticle.Text = ex.Message;
}

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) 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.

Comments and Discussions

 
GeneralThanks Pin
UMRINDERPAL3-Jan-15 23:50
UMRINDERPAL3-Jan-15 23:50 
QuestionHow to write Dynamically created japanese to pdf Pin
Sneha Gupta19-Jun-14 21:42
Sneha Gupta19-Jun-14 21:42 
QuestionNot able to parse background color style Pin
rrrattling2-May-13 0:14
rrrattling2-May-13 0:14 
AnswerRe: Not able to parse background color style Pin
Sayed Irfanul Hasan2-May-13 0:53
professionalSayed Irfanul Hasan2-May-13 0:53 
GeneralMy vote of 5 Pin
Member 1051082229-Jul-12 19:31
professionalMember 1051082229-Jul-12 19:31 
Generalvery good explanation......... Pin
vempallibavaji16-Dec-11 18:59
vempallibavaji16-Dec-11 18:59 
GeneralReason for my vote of 5 nice work Pin
Bilal Ahmed Lilla27-Nov-11 18:30
Bilal Ahmed Lilla27-Nov-11 18:30 
GeneralBe careful about using iTextSharp. It has some rather unusua... Pin
DaveRRR21-Nov-11 9:04
DaveRRR21-Nov-11 9:04 
Generalielement error "the type or namespace could not be found" ho... Pin
eku12318-Nov-11 18:44
eku12318-Nov-11 18:44 
GeneralRe: At first you have to download the itextsharp.dll file. You c... Pin
Sayed Irfanul Hasan18-Nov-11 18:54
professionalSayed Irfanul Hasan18-Nov-11 18:54 
GeneralRe: Add NameSpace using System.Collections.Generic; and replac... Pin
KAMAL BHARDWAJ13-Dec-11 20:42
KAMAL BHARDWAJ13-Dec-11 20:42 
GeneralReason for my vote of 4 Concise and well written. Screen sho... Pin
BrianBissell16-Nov-11 2:02
BrianBissell16-Nov-11 2:02 
GeneralBut how to COnvert HTML page into PDF ???????? Pin
lalupatel12-Nov-11 1:01
lalupatel12-Nov-11 1:01 
GeneralRe: The title says "Convert text into PDF using ASP.NET and C#" ... Pin
gorgias9915-Dec-11 22:43
gorgias9915-Dec-11 22:43 
GeneralRe: good.... Pin
vempallibavaji16-Dec-11 18:58
vempallibavaji16-Dec-11 18:58 
GeneralRe: But how to COnvert HTML page into PDF ???????? Pin
RedDot Vasilyev1-Dec-13 23:23
RedDot Vasilyev1-Dec-13 23:23 
Generalassa Pin
lalupatel12-Nov-11 1:00
lalupatel12-Nov-11 1: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.