Asp. net2.0 perfect solution for exporting PDF files

Source: Internet
Author: User

PDF Introduction: PDF (Portable Document Format) is an electronic file Format developed by Adobe. This file format has nothing to do with the operating system platform. That is to say, PDF files are common in Windows, Unix, and Apple's Mac OS Operating Systems. This feature makes it an ideal document format for electronic document distribution and digital information dissemination on the Internet. More and more e-books, product descriptions, company announcements, online materials, and emails are beginning to use PDF files. PDF files have become an industrial standard for digital information.

Adobe designed the PDF file format to support the publishing and publishing of multimedia integrated information across platforms, especially the publishing of network information. To achieve this, PDF has many advantages that cannot be compared with other electronic document formats. PDF file formats can encapsulate text, fonts, formats, colors, and image files independent of devices and resolutions in one file. This format file can also contain electronic information such as hypertext links, sound and dynamic images. It supports special files and provides high integration and security reliability.

In daily work, you often need to export reports and webpages to PDF files. This article provides the perfect solution:

ASP. NET export to PDFFinal(In fact, both winform and console programs can be done in the same way ).

Implementation in this articleText,Image,Data TableExport
Core technical solution: Using itextsharp. dll

 

1. Download itextsharp. dll and ICSharpCode. SharpZipLib. dll
Http://sourceforge.net/project/showfiles.php? Group_id = 72954

Itextsharp.tutorial+1.zipSample fileProvidedVarious solutionsDue to time issues, I hope you can study other requirements on your own.

Itextsharp. dllItextsharp-4.0.3-dll.zip

ICSharpCode. SharpZipLib. dll http://download.csdn.net/down/135897 ICSharpCode. SharpZipLib. dll

The content in the SharpZipLib. dll class library implements the compression and decompression functions. It isOpen SourceOf

2. Reference itextsharp. dll and ICSharpCode. SharpZipLib. dll

3. Background code:

 

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 iTextSharp;
Using iTextSharp. text;
Using iTextSharp.text.pdf;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
...{
Static DataTable datatable = new DataTable ("testpdf ");
Protected void Page_Load (object sender, EventArgs e)
...{
// Determine whether it is a sending back page http://blog.csdn.net/21aspnet
If (! Page. IsPostBack)
...{
DataRow dr;
// Create a Column example to specify the type of the example. The default string is used here.
Datatable. Columns. Add (new DataColumn ("no "));
Datatable. Columns. Add (new DataColumn ("username "));
For (int I = 1; I <5; I ++)
...{
Dr = datatable. NewRow ();
Dr [0] = System. Convert. ToString (I );
Dr [1] = "qingyueer" + System. Convert. ToString (I );
Datatable. Rows. Add (dr );
}
}


}
Protected void button#click (object sender, EventArgs e)
...{


Try
...{
Document document = new Document ();
Using writer. getInstance (document, new FileStream (Server. MapPath ("Chap0101.pdf"), 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 ));

Document. Add (new Paragraph (this. TextBox1.Text. ToString (), fontChinese ));

ITextSharp. text. Image jpeg = iTextSharp. text. Image. getInstance (Server. MapPath ("pic015.jpg "));
Document. Add (jpeg );
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 ();
}
Catch (incluentexception de)
...{;
Response. Write (de. ToString ());
}
}
}

 

4. Front-end code:

<%... @ 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">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> clear moon make export PDF http://blog.csdn.net/21aspnet </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Export"/> </div>
</Form>
</Body>
</Html>

5. Foreground operations:

6. attribute description:

Itextsharp-4.0.3-dll.zipThe sample file contains almost all PDF Processing Requirements

Color:

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.