C# ITextSharp pdf 自动打印

PDF生成后直接进入打印预览不用下载

using iTextSharp.text;
using iTextSharp.text.pdf;

Document pdfDoc = new Document(new Rectangle(mm(100.7f), mm(140.7f)), ml, mr, mt, mb);
MemoryStream pdfStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream);
pdfDoc.Open();
pdfDoc.NewPage();

....//生成pdf文档内容

//给pdfWriter添加自动打印的script代码
StringBuilder script = new StringBuilder();
script.Append("this.print({bUI: true,bSilent: true,bShrinkToFit: false});");
pdfWriter.ViewerPreferences = PdfWriter.HideMenubar | PdfWriter.HideToolbar | PdfWriter.HideWindowUI;
pdfWriter.AddJavaScript(script.ToString()

pdfDoc.Close();
pdfWriter.Close();

//输出到客户端缓存
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.AddHeader("Content-Disposition", "inline; filename=" + fileName + ".pdf");
Response.BinaryWrite(pdfStream.ToArray());
Response.Flush();
Response.End();

上一篇:leetcode 121. Best Time to Buy and Sell Stock ----- java


下一篇:Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成