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
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();
 
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)

About the Author

Sayed Irfanul Hasan
Software Developer Nascent Systems Ltd.
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

 
QuestionHow to write Dynamically created japanese to pdf PinmemberSneha Gupta19-Jun-14 21:42 
Questionthanks for sharing Pinmemberjacaboo29-Aug-13 20:51 
QuestionNot able to parse background color style Pinmemberrrrattling2-May-13 0:14 
AnswerRe: Not able to parse background color style PinmemberSayed Irfanul Hasan2-May-13 0:53 
GeneralMy vote of 5 PinmemberMd. Humayun Rashed29-Jul-12 19:31 
Suggestionpowerful c# ASP.NET library to convert anything a browser can render to PDF, SVG or IMAGE Pinmembernetsoftrev4-Mar-12 9:10 
GeneralRe: powerful c# ASP.NET library to convert anything a browser can render to PDF, SVG or IMAGE PinmemberMember 1078543529-Apr-14 23:55 
Generalvery good explanation......... Pinmembervempallibavaji16-Dec-11 18:59 
GeneralReason for my vote of 5 nice work PinmemberBilal Ahmed Lilla27-Nov-11 18:30 
GeneralBe careful about using iTextSharp. It has some rather unusua... PinmemberDaveRRR21-Nov-11 9:04 
Generalielement error "the type or namespace could not be found" ho... Pinmembereku12318-Nov-11 18:44 
GeneralRe: At first you have to download the itextsharp.dll file. You c... PinmemberSayed Irfanul Hasan18-Nov-11 18:54 
GeneralRe: Add NameSpace using System.Collections.Generic; and replac... PinmemberKAMAL BHARDWAJ13-Dec-11 20:42 
GeneralReason for my vote of 4 Concise and well written. Screen sho... PinmemberBrianBissell16-Nov-11 2:02 
GeneralBut how to COnvert HTML page into PDF ???????? Pinmemberlalupatel12-Nov-11 1:01 
GeneralRe: The title says "Convert text into PDF using ASP.NET and C#" ... Pinmembergorgias9915-Dec-11 22:43 
GeneralRe: good.... Pinmembervempallibavaji16-Dec-11 18:58 
GeneralRe: But how to COnvert HTML page into PDF ???????? PinmemberRedDot Vasilyev1-Dec-13 23:23 
Generalassa Pinmemberlalupatel12-Nov-11 1:00 
NewsIn health-care ruling, precedent trumped politics PinmemberAimeili11-Nov-11 15:55 

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 | Mobile
Web04 | 2.8.140718.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Sayed Irfanul Hasan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid