C# 输出pdf文件流在页面上显示

1 不调用itextsharp.dll的操作

/// <summary>
        /// 生成pdf流
        /// </summary>
        /// <param name="fpath"></param>
        private void EditPDF(string fpath)
        {
            string path = fpath.Replace("\\", "/");
            FileStream MyFileStream = new FileStream(path, FileMode.Open);
            ViewPdf(MyFileStream);
        }
        /// <summary>
        /// 显示pdf
        /// </summary>
        /// <param name="fs"></param>
        private void ViewPdf(Stream fs)
        {
            byte[] buffer = new byte[fs.Length];
            fs.Position = 0;
            fs.Read(buffer, 0, (int)fs.Length);
            Response.Clear();
            Response.AddHeader("Content-Length", fs.Length.ToString());
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
            fs.Close();
            Response.BinaryWrite(buffer);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
        }

2 引用itextsharp.dll操作的,此操作可以对显示PDF进行打开即弹出打印操作及设置是否显示工具栏,打印按钮等.

private void EditPDF(string fpath)
        {
            
            string path = fpath.Replace("\\","/");
            PdfReader reader = new PdfReader(path); 
            MemoryStream ms = new MemoryStream();
            PdfStamper stamper = new PdfStamper(reader, ms);
            stamper.Writer.ViewerPreferences = PdfWriter.HideWindowUI;
            stamper.Writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, null, PdfWriter.AllowPrinting | PdfWriter.AllowFillIn);
            stamper.Writer.CloseStream = false;
            //直接弹出打印不用点击打印按钮
            //PdfAction.JavaScript("myOnMessage();", stamper.Writer);
            //stamper.Writer.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
            
            //StringBuilder script = new StringBuilder();
            //script.Append("this.print({bUI: false,bSilent: true,bShrinkToFit: true});").Append("\r\nthis.closeDoc();");
            //script.Append("var pp = this.getPrintParams();pp.printerName = '\\\\fpserver\\hp LaserJet 1010'; this.print(pp);");
            //script.Append("this.print(flase);");
            //stamper.Writer.AddJavaScript(script.ToString(),false);

//PdfContentByte cb = stamper.GetOverContent(1);
            //cb.Circle(250, 250, 50);
            //cb.SetColorFill(iTextSharp.text.Color.RED);
            //cb.SetColorStroke(iTextSharp.text.Color.WHITE);
            //cb.FillStroke();
            stamper.Close();

ViewPdf(ms);

}
        private void ViewPdf(Stream fs)
        {
            byte[] buffer = new byte[fs.Length];            
            fs.Position = 0;
            fs.Read(buffer, 0, (int)fs.Length);
            Response.Clear();
            //Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
            Response.AddHeader("Content-Length", fs.Length.ToString());
            //Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
            Response.ContentType = "application/pdf";
            fs.Close();

Response.BinaryWrite(buffer);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
        }

3 web页面显示方式

可以直接输出显示在当前页也可以在另外一个页面中引用下面显示

test.aspx表示直接输出显示的页面,

如果直接输出显示的话不好控制页面布局,在另外一个新页中显示的话容易控制页面布局.

<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="100%" height="100%" border="0"
            id="Object1" name="pdf" VIEWASTEXT>
            <param name="toolbar" value="false">
            <param name="_Version" value="65539">
            <param name="_ExtentX" value="20108">
            <param name="_ExtentY" value="10866">
            <param name="_StockProps" value="0">
            <param name="SRC" value="test.aspx?reportfile=<%=filepath %>">
           </object>

上一篇:网络应用发布到linux上的web服务器上页面上显示麻将牌式字符的问题


下一篇:Poj 1067 取石子游戏(NIM,威佐夫博奕)