Recently to use Asp.net 2.0 generate PDF, read the next book, check the information, found that there can be components to help, you can download itextsharp (https://sourceforge.net/projects/itextsharp)
Download and reference the control in the project. For example
1 able content to PDF
First, create a datatable to convert to PDF as follows:
Using itextsharp;
Using itextsharp. text;
Using itextsharp.text.pdf;
Using system. IO;
/// <Summary>
/// Method of converting datatable into a PDF file
/// </Summary>
Public class tabletopdf
{
Public tabletopdf ()
{
}
/// <Summary>
/// Convert the data table to a PDF document
/// </Summary>
/// <Param name = "data"> data table </param>
/// <Param name = "pdffile"> full path of the target PDF file </param>
/// <Param name = "fontpath"> font path </param>
/// <Param name = "fontsize"> font size </param>
/// <Returns> whether the call is successful </returns>
Public static bool convertdatatabletopdf (datatable, string pdffilepath, string fontpath, float fontsize)
{
// Initialize a target document class
Document document = new document ();
// Call the PDF Writing Method stream
// Note: filemode-create indicates that if the target file does not exist, it is created. if it already exists, it is overwritten.
Using writer = Using writer. getinstance (document, new filestream (pdffilepath, filemode. Create ));
// Open the target Document Object
Document. open ();
// Create a font in the PDF document
Basefont = basefont. createfont (
Fontpath,
Basefont. identity_h,
Basefont. not_embedded );
// Create a font Based on the font path and font size attributes
Font font = new font (basefont, fontsize );
// Create a table in pdf format based on the data table content
Pdfptable table = new pdfptable (datatable. Columns. Count );
// Traverse the content of the original table
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 (), font ));
}
}
// Add converted table data to the target document
Document. Add (table );
// Close the target file
Document. Close ();
// Close the write stream
Writer. Close ();
Return true;
}
}
Then, in the event of the button to call the conversionCodeCan be called
/Save the target file under this project
// Simsun font
// Select 14 for the font size
// Mytb is the name of the Data able.
Tabletopdf. convertdatatabletopdf (mytb, server. mappath (". ") + @" \ tabletables "," C: \ winnt \ fonts \ simsun. TTC, 1 ", 14 );
2. Provide text content and generate a PDF file
For example, if you enter the text content and save path of the PDF file to be output, you can also output the PDF file.
/// <Param = "TXT">: content of the text to be output </param>
private void createtxt (string txt, string filepath)
{< br> // create a Document Object
document = new document ();
// instantiate the generated document
using writer. getinstance (document, new filestream (filepath, filemode. create);
// open the document
document. open ();
// Add text content to the document
document. add (new paragraph (txt);
// close the Document Object
document. close ();
}
3. Add the header and footer
Private void createauthorization header (string filepath, string headertxt, string footertxt)
{
// Create a Document Object
Document document = new document ();
// Create a document writing instance
Using writer. getinstance (document, new filestream (filepath, filemode. Create ));
// Add a footer
Headerfooter footer = new headerfooter (new phrase (footertxt), true );
Footer. Border = rectangle. no_border;
Document. footer = footer;
// Open the Document Content Object
Document. open ();
// Add a header
headerfooter header = new headerfooter (new phrase (headertxt), false);
document. header = header;
// design the content of each page
document. add (new paragraph ("this is first page");
// Add a new page
document. newpage ();
// Add text to page 2nd
document. add (new paragraph ("this is second page");
// reset the page quantity
document. resetpagecount ();
// close the Document Object
document. close ();
}