. NET Dynamic generation of PDF files (using itextsharp)

Source: Internet
Author: User

1. on the front-end page:

Code
< Div >
< ASP: textbox ID = " Textbox1 " Runat = " Server " > </ ASP: textbox >

< ASP: button ID = " Button1 " Runat = " Server " Onclick = " Button#click " Text = " Export "   />

</Div> 

2. BackgroundCode:

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 =   New Datatable ( " Testpdf " );
Protected   Void Page_load ( Object Sender, eventargs E)
{

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 ( " Haha " ));
Datatable. Columns. Add ( New Datacolumn ( " Haha " ));
For ( Int I =   1 ; I <   5 ; I ++ )
{
Dr = Datatable. newrow ();
Dr [ 0 ] = System. Convert. tostring (I );
Dr [ 1 ] =   " Red Leaf lock Autumn "   + 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 ( " Chap0107.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 (NewParagraph (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(Documentexception de)
{
Response. Write (De. tostring ());
}
}
}

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.