This is an online example. Haha, it is very useful!
First download itextsharp. dll from the Internet and add references. The main program is as follows:
Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. IO; using iTextSharp. text; using iTextSharp.text.pdf; public partial class _ Default: System. web. UI. page {static DataTable datatable = new DataTab Le ("testpdf"); protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {DataRow dr; // you can specify the type of the Column instance. The default string able is used here. columns. add (new DataColumn ("Number"); datatable. columns. add (new DataColumn ("username"); for (int I = 1; I <5; I ++) {dr = datatable. newRow (); dr [0] = System. convert. toString (I); dr [1] = "test" + System. convert. toString (I); datatable. rows. add (dr) ;}} protected void button#click (object sender, EventArgs e) {try {Document docum Ent = new Document (); your writer. getInstance (document, new FileStream (Server. mapPath ("testpattern"), FileMode. create); document. open (); BaseFont bfChinese = BaseFont. createFont ("C :\\ WINDOWS \ Fonts \ simsun. ttc, 1 ", BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED); Font fontChinese = new Font (bfChinese, 12, Font. NORMAL, new Color (0, 0, 0); // output the text document according to the set font. add (new Paragraph (this. textBox1.Text. toString (), FontChinese); // output the image to the PDF file iTextSharp. text. image 000001 = iTextSharp. text. image. getInstance (Server. mapPath ("Images/gyl.jpg"); document. add (000001); iTextSharp. text. image versions 02 = iTextSharp. text. image. getInstance (Server. mapPath ("Images/yy.jpg"); document. add (rows 02); PdfPTable table = new PdfPTable (datatable. columns. count); for (int I = 0; I <datatable. rows. count; I ++) {for (int j = 0; j <Datatable. columns. count; j ++) {table. addCell (new Phrase (datatable. rows [I] [j]. toString (), fontChinese);} document. add (table); document. close (); Response. write ("<script> alert ('export successful! '); </Script> ");} catch (incluentexception de) {Response. Write (de. ToString ());}}}
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">