Today, the blog park to see someone published an article generating PFD, ready to leave a copy of the ready to use the time to facilitate the call;
First go to itextsharp website Download control (HTTPS://SOURCEFORGE.NET/PROJECTS/ITEXTSHARP)
Refer the downloaded control to your project, the main BLL file is: Itextsharp.dll file
1. Generate PDF files according to the DataTable
Add Itextsharp Reference
Using Itextsharp;
Using Itextsharp.text;
Using ITextSharp.text.pdf;
Using System.IO;
Application code
<summary>///Convert data sheet to PDF document///</summary>/<param name= "Data" > Datasheet data </para m>//<param name= "pdffile" > Destination PDF file full path </param>//<param name= "Fontpath" > fonts in the same path </p aram>//<param name= "FontSize" > Font size </param>//<returns> return call Success </returns> public static bool Convertdatatabletopdf (DataTable DataTable, String Pdffilepath, String fontpath, float FontSize) {//Initialize a Target document class documents document = new document (); Call the Write method stream for the PDF//Note that filemode-create indicates that if the destination file does not exist, it is created and overwritten if it already exists. PDFWriter writer = pdfwriter.getinstance (document, New FileStream (Pdffilepath, FileMode.Create)); Open the target Document object documents. Open (); Create a font in a PDF document Basefont Basefont = Basefont.createfont (Fontpath, Basefont.identit Y_h, basefont.not_embedded); Depending on the fontpath and font size attributes Create fonts font font = new Font (BASEFONT, FontSize); Create a table in PDF format based on the contents of the datasheet pdfptable table = new Pdfptable (DataTable. Columns.count); Traverse the contents 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 the converted table data document to the target documents. ADD (table); Close the target file document. Close (); Writes off write stream writer. Close (); return true; }//<summary>//Generate Datasets///</summary>//<returns></returns> Private DataSet Getset () {DataSet ds = new DataSet (); String sql = "SELECT * from T_ajaxxml"; string config = configurationmanager.connectionstrings["config"]. ConnectionString; using (SqlConnection CNN = new SqlConnection (Config)) {using (SqlCommand CMM = new Sq Lcommand (sql, CNN)) {SqlDataAdapter dapter = new SqlDataAdapter (CMM); Dapter. Fill (DS); }} return DS; Call method: Convertdatatabletopdf (Getset (). Tables[0], Server.MapPath ("~/table.pdf"), "c:\\windows\\fonts\\simsun.ttc,1", 14);
2. Generate PDF files based on text content
private void Createtxt (String txt, string filepath) { //Create Document Object Documents = new file (); Instantiate the generated document pdfwriter.getinstance (documents, New FileStream (filepath, FileMode.Create)); Open Document Documents . Open (); Add text content to document . ADD (new Paragraph (TXT)); Close Document object Documents . Close (); }
3, Generate header footer (but iTextSharp5.5 this version does not have this method, trouble the pro help find one)
private void Createpdfheader (string filepath, String headertxt, String footertxt) {//Create Document Object Document document = new document (); Create document Write instance Pdfwriter.getinstance (document, New FileStream (filepath, filemode.create)); Pdfpageeventhelper hh = new Pdfpageeventhelper (); Add footer HeaderFooter footer = new HeaderFooter (new Phrase (Footertxt), true); Footer. Border = Rectangle.no_border; Document. Footer = Footer; Open Document Content Object documents. Open (); Add header HeaderFooter Header = new HeaderFooter (new Phrase (Headertxt), false); Document. Header = header; Design the contents of each page document. ADD (New Paragraph ("This is first page"); Add a new page document. NewPage (); The text document is added on page 2nd. ADD (New Paragraph ("This is Second page"); Resets the number of pages document. Resetpagecount (); Close Document object documents. Close (); }