Recent projects need to be everywhere PDF files, and finally Internet search, found that itextsharp more useful, so did an example:
Public stringexportpdf () {//Itextsharp Usage//steps:1. ADD content to cell;2. Add cell to Table;3. ADD table to Document;4. ADD document to Rectangle; stringSabsolutepath = ControllerContext.HttpContext.Server.MapPath ( This. Path); stringFileName =string. Format ("notice_{0}.pdf", DateTime.Now.ToString ("YYYYMMDDHHMMSS")); Basefont BF= Basefont.createfont ("C:/windows/fonts/arial.ttf", Basefont.identity_h, basefont.not_embedded); Rectangle Rec=NewRectangle ( +, -); Document Document=NewDocument (REC); Document. SetMargins (10f, 10f, 10f, 10f); PDFWriter PDFWriter=pdfwriter.getinstance (document,NewSystem.IO.FileStream (Sabsolutepath +"\\"+FileName, System.IO.FileMode.Create)); Document. Open (); Font Font=NewFont (BF); Try{pdfptable pdftable=NewPdfptable (5); Pdftable. HorizontalAlignment=1; //Set Cell Width float[] Cellwidth = {3f, 5f, 5f,3.5f, 5f}; Pdftable. Setwidths (Cellwidth); //HeaderFont Pdffonta = Fontfactory.getfont ("Arial", -, Font.Bold); Font PDFFONTB= Fontfactory.getfont ("Arial", the, Font.Bold); Font PDFFONTC= Fontfactory.getfont ("Arial",Ten); //Image ObjectImage jpeg = image.getinstance (HttpContext.Server.MapPath (".. /images/buddy.jpg")); Pdfpcell Cell=NewPdfpcell (JPEG); Cell. Fixedheight=40f; Cell. Colspan=3; Cell. Rowspan=4; Cell. Border=0; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase (("did you Know?") , PDFFONTB)); Cell. Colspan=3; Cell. Rowspan=2; Cell. Border=0; Cell. BackgroundColor=NewBasecolor (247,238,214);//RGB Color ValueCell. HorizontalAlignment =1; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("\ntitle:hello world!\ndate:"+ DateTime.Now.ToString ("mm/dd/yyyy") +"\ n", PDFFONTC)); Cell. Colspan=4; Cell. Rowspan=2; Cell. Border=0; Cell. BackgroundColor=NewBasecolor (247,238,214);//RGB Color ValueCell. HorizontalAlignment =1; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("Notice", Pdffonta)); Cell. BackgroundColor=NewBasecolor ( the,0,0);//RGB Color ValueCell. Colspan =Ten; Cell. Padding=5f; Cell. HorizontalAlignment=1; Pdftable. Addcell (cell); string[] Assheader = {"DateTime","Name","Description","Icon","Notes"}; for(inti =0; i < assheader.length; i++) {cell=NewPdfpcell (NewPhrase (Assheader[i], PDFFONTB)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. BackgroundColor=NewBasecolor ( the,0,0); Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); } for(inti =0; I < -; i++) {cell=NewPdfpcell (NewPhrase (NewDateTime (). ToShortDateString (), PDFFONTC)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("Jack Chean", PDFFONTC)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("Just for my testing!!", PDFFONTC)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("ICON", PDFFONTC)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); Cell=NewPdfpcell (NewPhrase ("For tom!", PDFFONTC)); Cell. HorizontalAlignment=1; Cell. VerticalAlignment=Element.align_middle; Cell. Paddingbottom=4f; Cell. Paddingtop=4f; Pdftable. Addcell (cell); } //Very Important//You can display the header in each page through this properity:headerrowsPdftable. Headerrows =3; Document. ADD (pdftable); //Pager floatTableheight =pdftable. Calculateheights (); if(Tableheight < -&& tableheight > -) {document. NewPage (); } document. NewPage (); Document. Close (); } Catch(Exception ex) {document. ADD (NewParagraph (ex. Message.tostring (), font)); } returnFileName; }
View Code
The final display effect:
Reference article: http://blog.csdn.net/adgjlxxx/article/details/43307227
Export PDF file using Itextsharp