Asp.net PDF file generation reference

Source: Internet
Author: User

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"> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.