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 ());
}
}
}