Display PDF in Web Application

前端 未结 5 696
轮回少年
轮回少年 2021-01-03 01:46

I have googled this, searched this, looked through SO and other sites (I\'ve been trying to read on this issue for hours now), but I still can\'t seem to find a satisfactory

相关标签:
5条回答
  • 2021-01-03 02:19

    ASP.Net has a ReportViewer server control that can be used to display PDF files.

    Much of the documentation about this feature is about how to generate a report and export it to PDF. MSDN really isn't very helpful. I suppose everyone is relying on Adobe Reader and not looking for an alternative?

    But it is possible also to import and display a PDF. This code seemed to work for this user:

    public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload)
    {
        string path = (string.IsNullOrEmpty(reportViewer.LocalReport.ReportPath)) ? reportViewer.ServerReport.ReportPath : reportViewer.LocalReport.ReportPath;
        RenderToPdf(reportViewer, forceDownload, System.IO.Path.GetFileNameWithoutExtension(path));
    }
    
    public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload, string fileNameWithoutExtension)
    {
        HttpContext context = HttpContext.Current;
    
        if (!context.Response.Buffer)
        {
            return; //can not clear the buffer, so exit
        }
    
        //define out properties
        Warning[] warnings;
        string mimeType, encoding, fileNameExtension;
        string[] streams;
    
        //get pdf content
        Byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
    
        //cancel and clear the existing output!
        context.Response.Clear();
        context.Response.ContentType = "application/pdf";
    
        //add a header so that the user can save the target as a downloaded file
        if (forceDownload)
        {
            context.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.pdf", fileNameWithoutExtension));
        }
    
        context.Response.BinaryWrite(pdfContent);
        context.Response.End();
    }
    
    0 讨论(0)
  • 2021-01-03 02:26

    This link might be useful to you,

    http://nilangshah.wordpress.com/2007/05/28/successfully-stream-a-pdf-to-browser-through-https/

    You can have pdf opened in a new tab by speccifying target="_blank" for the link. ByteArray mentioned in the blog is your BLOB from the DB. Hope this helps.

    0 讨论(0)
  • 2021-01-03 02:29

    This is how I do to open a PDF document in the browser from an ASP.NET (aspx) page. In the page OnLoad:

            this.Response.Clear();
            this.Response.Buffer = true;
            this.Response.ContentType = "application/pdf";
            this.Response.AddHeader("content-length", pdfReportStream.Length.ToString());
            this.Response.BinaryWrite(pdfReportStream.ToByteArray());
    
            this.Response.End();
    
    0 讨论(0)
  • 2021-01-03 02:31

    Have you tried adding a http header with content-disposition-inline? Also you may want to write the result directly to the output response instead of saving it. This would ensure the files actual path is not displayed as your writing it to the response directly.

    Eg

    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-disposition","inline");
    Response.BinaryWrite(myfilestream.ToArray());
    

    Where myfilestream is a memory stream or if you already have a byte array from your blob you can write it directly to the response without the toarray

    0 讨论(0)
  • 2021-01-03 02:33

    If you're looking for client side pdf client, you can have a look at PDF.js that can display any PDF with the default viewer included (you'll have to transform the viewer.html into a view if in MVC but it's quite easy). It's open source and alive.I had to adapt it a little to be able to show pdf annotations on iphone (digital signatures) by commenting some lines in the js.

    0 讨论(0)
提交回复
热议问题