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();

转载于:https://www.cnblogs.com/ITCoNan/p/6004484.html

你可能感兴趣的