问题描述
我想在页面生成PDF文件,先做一个模板,然后把一些数据给填充进行去,点击按钮进行导出成PDF格式的文件!类似于一些网站,点击下载就能直接下载了!各位大侠帮帮忙!我知道要用到iTextsharp第三方控件。但是我现在掌握不好布局!就是把内部做成一张表格的形式!
解决方案
解决方案二:
自己顶!!!!
解决方案三:
usingSystem;usingSystem.IO;usingSystem.Xml;usingSystem.Data;usingSystem.Reflection;usingSystem.Security.Permissions;usingSystem.Text;usingSystem.Web;usingSystem.Collections;usingiTextSharp.text;usingiTextSharp.text.pdf;//创建DocumentDocumentdocument=null;//为该Document创建一个Writer实例PdfWriterwriter=null;//段Paragraphp=null;try{string[]company=ERP.Web.Common.Parameter.CompanyMessage();//字体BaseFontbaseFont=BaseFont.CreateFont(System.Web.HttpContext.Current.Server.MapPath("/Configuration/SIMYOU.TTF"),BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Fontfont=newFont(baseFont,10,Font.NORMAL);//内存流MemoryStreammemoryStream=newMemoryStream();document=newDocument(PageSize.A4,10,10,10,10);//页脚HeaderFooterfooter=newHeaderFooter(newPhrase("footertxt"),true);footer.Border=Rectangle.NO_BORDER;document.Footer=footer;//为该Document创建一个Writer实例writer=PdfWriter.GetInstance(document,memoryStream);writer.CloseStream=false;//打开document.Open();//添加LogoImageimage=Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath(company[7]));image.Alignment=Image.ALIGN_LEFT|Image.TEXTWRAP;image.ScaleToFit(280,10);image.ScalePercent(80);document.Add(image);image=null;//添加本公司信息p=newParagraph(company[0],font);p.Alignment=Element.ALIGN_RIGHT;document.Add(p);//添加地址string[]inovit=newstring[]{company[1],company[2]+company[3],company[4],company[5]+company[6]};for(inti=0;i<inovit.Length;i++){p=newParagraph(inovit[i],font);p.Alignment=Element.ALIGN_RIGHT;document.Add(p);p=null;}//添加横线PdfContentBytepcb=writer.DirectContent;pcb.MoveTo(10,750);pcb.LineTo(document.PageSize.Width-10,750);pcb.Stroke();pcb=null;//添加标题document.Add(newParagraph("n"));p=newParagraph(title,font);p.Alignment=Element.ALIGN_CENTER;document.Add(p);p=null;//添加公司信息if(companyMessage!=null){for(inti=0;i<companyMessage.Length;i++){if(companyMessage[i]!=""){p=newParagraph(companyMessage[i],font);p.Alignment=Element.ALIGN_LEFT;document.Add(p);p=null;}}}//添加产品描述document.Add(newParagraph("n"));p=newParagraph(company[10],font);p.Alignment=Element.ALIGN_LEFT;document.Add(p);p=null;//添加表信息if(dataTable!=null){Tabletable=newTable(dataTable.Columns.Count);table.Width=100;if(columnWidth!=null)table.Widths=columnWidth;table.AutoFillEmptyCells=true;table.BorderWidth=1;table.DefaultHorizontalAlignment=1;table.DefaultVerticalAlignment=1;table.Alignment=Element.ALIGN_MIDDLE;table.BorderColor=newColor(0,0,0);table.Padding=1;table.Spacing=0.1F;Cellcell=null;//表头for(inti=0;i<columnName.Length;i++){cell=newCell();cell.Header=true;cell.BackgroundColor=newColor(221,217,195);cell.Colspan=1;cell.Add(newPhrase(columnName[i],font));table.AddCell(cell);cell=null;}//表中数据for(inti=0;i<dataTable.Rows.Count;i++){for(intj=0;j<dataTable.Columns.Count;j++){table.AddCell(newPhrase(dataTable.Rows[i][j].ToString(),font));}}//添加表脚部if(!string.IsNullOrEmpty(tableFooter)){cell=newCell();//cell.Colspan=dataTable.Columns.Count;cell.Colspan=columnName.Length;cell.HorizontalAlignment=2;cell.Add(newPhrase(tableFooter,font));table.AddCell(cell);cell=null;}//添加Tabledocument.Add(table);}//添加底部if(pageFooter!=null){for(intpf=0;pf<pageFooter.Length;pf++){if(!string.IsNullOrEmpty(pageFooter[pf])){p=newParagraph(pageFooter[pf],font);p.Alignment=Element.ALIGN_LEFT;document.Add(p);p=null;}}}//重置页面数量document.ResetPageCount();//关闭目标文件document.Close();//关闭写入流writer.Close();//输出System.Web.HttpContext.Current.Response.ContentType="application/pdf";System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;System.Web.HttpContext.Current.Response.AddHeader("Content-disposition",string.Format("attachment;filename={0}",fileName));System.Web.HttpContext.Current.Response.OutputStream.Write(memoryStream.GetBuffer(),0,memoryStream.GetBuffer().Length);System.Web.HttpContext.Current.Response.OutputStream.Flush();System.Web.HttpContext.Current.Response.OutputStream.Close();}catch(Exceptione){stringee=e.Message;//关闭目标文件document.Close();//关闭写入流writer.Close();document=null;writer=null;p=null;}