簡體   English   中英

如何保存PDF

[英]How To Save a PDF

嗨,我有一個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

最終班

基本上,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM