First download a control Http://sourceforge.net/projects/itextsharp/DLL file (itextsharp)
Then build a ASP.net project, right-click on the project name "Add Reference". Browse to the downloaded DLL file, and then place a lable control (named Lblarticle) and a TextBox input box control on the page, and then drag a button. The purpose of this article is to convert the text on the input box or lable control into a PDF file when we click the button
Next go to the CS file and enter the following code to add the namespace reference:
The code is as follows |
Copy Code |
Using System.IO; Using Itextsharp.text; Using ITextSharp.text.pdf; Using ITextSharp.text.html; Using ITextSharp.text.xml; Add the following code to the Click event for the button: 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; } |