Click here to Skip to main content
12,698,048 members (23,608 online)
Click here to Skip to main content
Add your own
alternative version

Stats

67.4K views
39 bookmarked
Posted

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

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)

Share

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

Pro
Pro

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 Automatically Removed 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 Automatically Removed Pin
4-Mar-12 10:10
membernetsoftrev4-Mar-12 10:10 
GeneralMessage Automatically Removed 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 

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.

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