ASP. net2.0 perfect solution for exporting PDF files
Author: qingyueer
Home page:Http://blog.csdn.net/21aspnet/Time: 2007.5.28
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, winform and consoleProgram).
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. BackgroundCode:
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 = 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:
Font fontchinese = new font (bfchinese, 12, Font. normal, new color (0, 0, 0); // black font fontchinese = new font (bfchinese, 12, Font. normal, new color (0,255, 0); // green
Note:
Itext supports annotations of different styles.
U text comment:
You can add a short text section to your document, but it is not part of the document content. The comment includes the title and content:
Annotation A = New Annotation (
"Authors ",
"Maybe it's because I wanted to be an author myself that I wrote itext .");
Alignment:
Cell. horizontalalignment = element. align_center;
Cell. verticalalignment = element. align_middle;
Underline/strikethrough:
Chunk chunk1 = new chunk ("this text is underlined", fontfactory. getfont (fontfactory. Helvetica, 12, Font. Underline ));
Chunk chunk2 = new chunk ("this font is of Type italic | strikethru", fontfactory. getfont (fontfactory. Helvetica, 12, Font. italic | font. strikethru ));
Encryption:
Public void setencryption (Boolean strength, string userpassword, string ownerpassword, int permissions );
Due to time issues: For more information, such as watermark cell spacing and border in the header and footer attributes directory, Please study the document yourself.
All the sample files you don't want to think of are available.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1627830