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

Convert text into PDF using ASP.NET and C#

By , 16 Dec 2011
 

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot able to parse background color stylememberrrrattling2 May '13 - 0:14 
AnswerRe: Not able to parse background color stylememberSayed Irfanul Hasan2 May '13 - 0:53 
GeneralMy vote of 5memberMd. Humayun Rashed29 Jul '12 - 19:31 
Suggestionpowerful c# ASP.NET library to convert anything a browser can render to PDF, SVG or IMAGEmembernetsoftrev4 Mar '12 - 9:10 
Generalvery good explanation.........membervempallibavaji16 Dec '11 - 18:59 
GeneralReason for my vote of 5 nice workmemberBilal Ahmed Lilla27 Nov '11 - 18:30 
GeneralBe careful about using iTextSharp. It has some rather unusua...memberDaveRRR21 Nov '11 - 9:04 
Generalielement error "the type or namespace could not be found" ho...membereku12318 Nov '11 - 18:44 
GeneralRe: At first you have to download the itextsharp.dll file. You c...memberSayed Irfanul Hasan18 Nov '11 - 18:54 
GeneralRe: Add NameSpace using System.Collections.Generic; and replac...memberKAMAL BHARDWAJ13 Dec '11 - 20:42 
GeneralReason for my vote of 4 Concise and well written. Screen sho...memberBrianBissell16 Nov '11 - 2:02 
GeneralBut how to COnvert HTML page into PDF ????????memberlalupatel12 Nov '11 - 1:01 
GeneralRe: The title says "Convert text into PDF using ASP.NET and C#" ...membergorgias9915 Dec '11 - 22:43 
GeneralRe: good....membervempallibavaji16 Dec '11 - 18:58 
Generalassamemberlalupatel12 Nov '11 - 1:00 
NewsIn health-care ruling, precedent trumped politicsmemberAimeili11 Nov '11 - 15:55 

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

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