【问题标题】:How To Save a PDF如何保存 PDF
【发布时间】:2014-03-20 15:42:07
【问题描述】:

您好,我有一个 pdf 生成文件,当我单击按钮时,它可以很好地下载到计算机上。但是我需要将该文件保存到我系统中的文件夹中。

我尝试了很多不同的方法,但我无法正常工作是否有任何东西可以将其保存到我在 Visual Studio 的网站中创建的 PDF 文件夹中

更新代码

    Partial Class Pages_Payment
Inherits System.Web.UI.Page 



Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        btnPayment.Visible = False

    Response.ContentType = "application/pdf"

    Response.AddHeader("content-disposition", "attachment;filename=Receipt_" & Session("InvoNo") & ".pdf")

    Response.Cache.SetCacheability(HttpCacheability.NoCache)

    Dim sw As New StringWriter()

    Dim hw As New HtmlTextWriter(sw)

    pnlPerson.RenderControl(hw)

    Dim sr As New StringReader(sw.ToString())

    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)

    Dim htmlparser As New HTMLWorker(pdfDoc)

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()

    htmlparser.Parse(sr)


    pdfDoc.Close()

    Response.Write(pdfDoc)

    Response.End()


    Pages_Payment.SavePDF("~/PDF/Mypdf.pdf", pdfDoc)


End Sub

Public Shared Sub SavePDF(virtualPath As String, document As Document)
    Dim msOutput As New MemoryStream()
    Dim writer As New PdfWriter(document, msOutput)
    Dim filebytes As Byte() = msOutput.ToArray()
    Dim path As String = Server.MapPath(virtualPath)
    File.WriteAllBytes(path, filebytes)
End Sub

结束类

【问题讨论】:

  • 澄清一下,您想将 PDF 保存在服务器上的某个地方吗?
  • 是的,所以它就像 ("~/PDF/") 我想要文件去的地方
  • 如果是 iTextSharp,请参见此处的示例:stackoverflow.com/questions/12948053/save-pdf-to-disc
  • 我正在尝试将其添加到我的代码中,但我知道所有那些压模、阅读器的东西意味着要知道我是否添加正确。

标签: asp.net vb.net pdf


【解决方案1】:

基本上 PdfWriter 连接到 MemoryStream。将 MemoryStream 转换为字节数组,然后将字节保存到文件中。

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
    'btnPayment.Visible = False
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Receipt_" & Session("InvoNo") & ".pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    pnlPerson.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    Dim msOutput As New MemoryStream()
    Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, msOutput)    
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Dim filebytes As Byte() = msOutput.ToArray()
    Dim path As String = Server.MapPath("~/PDF/mypdf.pdf")
    File.WriteAllBytes(path, filebytes)
    Response.BinaryWrite(filebytes)
    Response.End()
End Sub

【讨论】:

  • 感谢回复我正在寻找转换为字节数组我真的不熟悉这整个领域所以我会尝试任何事情:)
  • 我把它粘贴进去了,但它突出了 writer 的一个问题——如果没有类的显式实例,就无法从共享方法或共享成员初始化程序中引用类的实例成员。 F:\BSNC\Pages\Payment.aspx.vb 206 30 F:\BSNC\
  • 重载解析失败,因为没有“新”可访问。是作家错误对不起另一个在服务器上。行
  • 谢谢,但这些错误出现在 dim writer 上的 Public Shared Sub SavePDF(virtualPath As String, document As Document) 块中,然后是 =server.mappath
  • 我能明白你的意思,但它在屏幕上给出了一个错误,所以它不会运行我得到两行的编译错误
猜你喜欢
  • 2020-09-10
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2013-10-04
  • 2011-09-07
相关资源
最近更新 更多