. Net exports pdf files. C # exports pdf files,

Source: Internet
Author: User

. Net exports pdf files. C # exports pdf files,

I recently met a requirement to export a PDF file. I checked the code documents online and summarized the following code to successfully export the PDF file.

You need to download itextsharp. dll online before coding. This Assembly is required. The main user downloaded version 5.0. It seems that it is not easy to use the previous version 5.4.

Add reference directly after download.

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Webpdf. aspx. cs" Inherits = "Web export pdf. Webpdf" %> <! DOCTYPE html> 
Namespace Web export pdf {public partial class Webpdf: System. web. UI. page {static DataTable datatable = new DataTable ("testpdf"); protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {DataRow dr = null; // you can specify the type of the Column instance. The default string datatable is used here. columns. add (new DataColumn ("Number"); datatable. columns. add (new DataColumn ("file name"); for (int I = 0; I <5; I ++) {dr = datatable. newRow (); dr [0] = System. convert. toString (I); dr [1] = "test exporting PDF file" + System. convert. toString (I); datatable. rows. add (dr) ;}} protected void button#click (object sender, EventArgs e) {try {token en T document = new Document (); invalid 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); iTextSharp. text. font fontChinese = new iTextSharp. text. font (bfChinese, 12, iTextSharp. text. font. NORMAL, new BaseColor (, 0); // The entered content is based on the set word Body to the PDF file document. add (new Paragraph(this.txt pdf. value. toString (), fontChinese); // output the image to the PDF file iTextSharp. text. image 000001 = iTextSharp. text. image. getInstance (Server. mapPath ("Image/001.png"); document. add (000001); iTextSharp. text. image versions 02 = iTextSharp. text. image. getInstance (Server. mapPath ("Image/userIcon-1.png"); 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 (Exception ex) {Response. Write (ex. ToString ());}}}}

 

Pay attention to the two image images in the code. You can add them to the project at will. If no error is reported after all the above Code is written, you can export it successfully.

If some classes fail to be found, you need to reference this file.

To export different styles of pdf content, you need to write the corresponding code as needed.

 

The exported PDF file is saved in the project folder.

 

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.