C#:MVC打印PDF文件

时间:2023-03-09 17:06:34
C#:MVC打印PDF文件

在百度上找了许多PDF文件打印,但是符合我需求的打印方式还没看到,所以根据看了https://www.cnblogs.com/TiestoRay/p/3380717.html的范例后,研究了一下,做出来符合自己需求的打印方式分享一下。

1、先要引用一个插件,可以从网上搜索下载就好

C#:MVC打印PDF文件

2、前端JS,比较简单一句代码,重点放在后台操作。

C#:MVC打印PDF文件

3、后台代码。

 public class PdfResult:ActionResult
{
private string FileName;
public event DocRenderHandler DocRenderEvent;
public delegate void DocRenderHandler(ref Document Doc); /// <summary>
/// 将FileName赋值一次
/// </summary>
/// <param name="FileName"></param>
public PdfResult(string FileName)
{
this.FileName = FileName;
} /// <summary>
/// 向页面输出时才会执行该方法
/// </summary>
/// <param name="context"></param>
public override void ExecuteResult(ControllerContext context)
{
Document Doc = new Document();
using (MemoryStream Memory=new MemoryStream())
{
PdfWriter PdfWriter = PdfWriter.GetInstance(Doc, Memory);
if (DocRenderEvent != null)
DocRenderEvent(ref Doc);
HttpResponseBase Response = context.HttpContext.Response;
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(Memory.GetBuffer(), , Memory.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
}
context.HttpContext.Response.End();
}
}
        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(, , , ); #region 相关元素准备
BaseFont bfChinese = BaseFont.CreateFont(@"C:\windows\fonts\simsun.ttc,1", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
Font Font16 = new Font(bfChinese, );
Font Font14 = new Font(bfChinese, );
Font Font12 = new Font(bfChinese, );
Font Font12Bold = new Font(bfChinese, , Font.BOLD);
Font Font12Italic = new Font(bfChinese, , Font.BOLDITALIC);
Font Font10Bold = new Font(bfChinese, , 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[] { , , , });
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[] { , , , });
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[] { , , , });
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、配上效果图:

C#:MVC打印PDF文件

以上方法可以个人分享研究!

不可做商业项目,违者必究!