在百度上找了许多PDF文件打印,但是符合我需求的打印方式还没看到,所以根据看了的范例后,研究了一下,做出来符合自己需求的打印方式分享一下。
1、先要引用一个插件,可以从网上搜索下载就好
2、前端JS,比较简单一句代码,重点放在后台操作。
3、后台代码。
1 public class PdfResult:ActionResult 2 { 3 private string FileName; 4 public event DocRenderHandler DocRenderEvent; 5 public delegate void DocRenderHandler(ref Document Doc); 6 7 ///8 /// 将FileName赋值一次 9 /// 10 /// 11 public PdfResult(string FileName)12 {13 this.FileName = FileName;14 }15 16 ///17 /// 向页面输出时才会执行该方法18 /// 19 /// 20 public override void ExecuteResult(ControllerContext context)21 {22 Document Doc = new Document();23 using (MemoryStream Memory=new MemoryStream())24 {25 PdfWriter PdfWriter = PdfWriter.GetInstance(Doc, Memory);26 if (DocRenderEvent != null)27 DocRenderEvent(ref Doc);28 HttpResponseBase Response = context.HttpContext.Response;29 Response.Clear();30 Response.Cache.SetCacheability(HttpCacheability.NoCache);31 Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);32 Response.ContentType = "application/pdf";33 Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length);34 Response.OutputStream.Flush();35 Response.OutputStream.Close();36 Response.Flush();37 }38 context.HttpContext.Response.End();39 }40 }
public ActionResult Print(string OrderID) { string FileName = OrderID + ".pdf"; PdfResult pr = new PdfResult(FileName); pr.DocRenderEvent += RenderPdfDoc; return pr; } private void RenderPdfDoc(ref Document Doc) { Doc.SetPageSize(PageSize.A4); Doc.SetMargins(60, 60, 20, 40); #region 相关元素准备 BaseFont bfChinese = BaseFont.CreateFont(@"C:\windows\fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font Font16 = new Font(bfChinese, 16); Font Font14 = new Font(bfChinese, 14); Font Font12 = new Font(bfChinese, 12); Font Font12Bold = new Font(bfChinese, 12, Font.BOLD); Font Font12Italic = new Font(bfChinese, 12, Font.BOLDITALIC); Font Font10Bold = new Font(bfChinese, 10, Font.BOLD); Paragraph parag; //Chunk chunk; PdfPTable table; #endregion #region 文件标题 Doc.Open(); Doc.AddAuthor("TiestoRay"); Doc.AddTitle("POP项目供应商结算单"); #endregion #region 正文 parag = new Paragraph("京东商城\r\nPOP项目供应商结算单", Font16); parag.Alignment = Element.ALIGN_CENTER; Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("供应商名称:", Font12Bold)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("结算单编号:", Font12Bold)); parag.Add(new Chunk("63930272255\r\n", Font12)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("店铺名称:", Font12Bold)); parag.Add(new Chunk("MOKO数码配件旗舰店\r\n", Font12)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("结算期间:", Font12Bold)); parag.Add(new Chunk("2017-11-30 至 2017-11-30\r\n", Font12)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("基准币种:", Font12Bold)); parag.Add(new Chunk("元\r\n\r\n", Font12)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("结算信息\r\n\r\n", Font16)); Doc.Add(parag); table = new PdfPTable(new float[] { 5, 5, 5, 5 }); table.WidthPercentage = 100f; table.AddCell(new Phrase("业务类型", Font12Bold)); table.AddCell(new Phrase("收付发票机构", Font12Bold)); table.AddCell(new Phrase("项目", Font12Bold)); table.AddCell(new Phrase("结算金额", Font12Bold)); Doc.Add(table); table = new PdfPTable(new float[] { 5, 5, 5, 5 }); table.WidthPercentage = 100f; table.AddCell(new Phrase("SOP", Font12)); table.AddCell(new Phrase("POP", Font12)); table.AddCell(new Phrase("货款:1097.70, 佣金:-84.50", Font12)); table.AddCell(new Phrase("1013.2", Font12)); Doc.Add(table); table = new PdfPTable(new float[] { 3, 3, 3, 3 }); table.WidthPercentage = 100f; table.AddCell(new Phrase("", Font12)); table.AddCell(new Phrase("", Font12)); table.AddCell(new Phrase("", Font12)); table.AddCell(new Phrase("1013.2", Font12)); Doc.Add(table); //Doc.NewPage();//换页 parag = new Paragraph(); parag.Add(new Chunk("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n供应商开票信息\r\n\r\n", Font16)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("公司名称:", Font12Bold)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("纳税人识别号:", Font12Bold)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("地址、电话:", Font12Bold)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("开户行及账号:", Font12Bold)); Doc.Add(parag); parag = new Paragraph(); parag.Add(new Chunk("尊敬的供应商,如数据核对无误,请及时确认结算单。\r\n\r\n", Font12Bold)); parag.Add(new Chunk("供应商签章(公章)\r\n\r\n\r\n\r\n\r\n\r\n\r\n", Font12Bold)); parag.Add(new Chunk("如有结算问题,可拨打商家客服电话,或联系商家在线客服咨询。", Font16)); Doc.Add(parag); Doc.Close(); #endregion }
4、配上效果图:
以上方法可以个人分享研究!
不可做商业项目,违者必究!