iTextSharp的生成PDF,并直接显示它在浏览器 [英] iTextSharp generate PDF and show it on the browser directly

查看:1407
本文介绍了iTextSharp的生成PDF,并直接显示它在浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开ASP.Net使用iTextSharp的创建后的PDF文件?我不想把它保存在服务器上,而是直接的在生成新的PDF,它显示在浏览器即可。是否有可能这样做吗?

How to open the PDF file after created using iTextSharp on ASP.Net? I don't want to save it on the server, but directly when the new PDF is generated, it shows on the browser. Is it possible to do that?

下面是我的意思的例子:点击这里。但在这个例子中,该文件直接下载。

Here is the example for what I mean: Click here . But on this example, the file directly downloaded.

我怎么能这样做?

    Dim doc1 = New Document()

    'use a variable to let my code fit across the page...
    Dim path As String = Server.MapPath("PDFs")
    PdfWriter.GetInstance(doc1, New FileStream(path & "/Doc1.pdf", FileMode.Create))

    doc1.Open()
    doc1.Add(New Paragraph("My first PDF"))
    doc1.Close()

上面的code做保存PDF到服务器。

The code above do save the PDF to the server.

非常感谢你提前! :)

Thank you very much in advance! :)

推荐答案

与下面的code解决的问题:

The problem solved with the code below:

    HttpContext.Current.Response.ContentType = "application/pdf"
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)

    Dim pdfDoc As New Document()
    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream)

    pdfDoc.Open()
    'WRITE PDF <<<<<<

    pdfDoc.Add(New Paragraph("My first PDF"))

    'END WRITE PDF >>>>>
    pdfDoc.Close()

    HttpContext.Current.Response.Write(pdfDoc)
    HttpContext.Current.Response.End()

希望帮助! :)

Hope help! :)

这篇关于iTextSharp的生成PDF,并直接显示它在浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆