2012-10-18 66 views
1

下面的代碼流的PDF文件瀏覽器,但我想保存到磁盤(C:\ myfile.pdf)...保存PDF到DISC

Dim FilePath As String = Server.MapPath("/docs/templates/page_1_cover.pdf") 
Dim reader As New PdfReader(FilePath) 

Dim output As MemoryStream = New MemoryStream() 
Dim stamper As PdfStamper = New PdfStamper(reader, output) 

stamper.AcroFields.SetField("APPLICANT NAME", "KnowlegeZone") 


reader.Close() 
stamper.Close() 


Response.AddHeader("Content-Disposition", "attachment; filename=YourPDF_I9.pdf") 
Response.ContentType = "application/pdf" 

Response.BinaryWrite(output.ToArray()) 
Response.End() 

我使用iTextSharp的。

回答

1

在這個解決方案,我可以用「PdfStamper」保存,而不是使用任何其它方法的文件。

Dim FilePath As String = Server.MapPath("/docs/templates/page_1_cover.pdf") 
Dim reader As New PdfReader(FilePath) 

Dim newfile As FileStream 
newfile = New FileStream(Server.MapPath("/docs/output/go.pdf"), FileMode.Create, FileAccess.Write) 

Dim stamper As PdfStamper = New PdfStamper(reader, newfile) 

stamper.AcroFields.SetField("APPLICANT NAME", "han") 


reader.Close() 
stamper.Close() 
4

這應該和調用File一樣簡單。 WriteAllBytes

Response.AddHeader("Content-Disposition", "attachment; filename=YourPDF_I9.pdf") 
Response.ContentType = "application/pdf" 

Dim data = output.ToArray(); 

File.WriteAllBytes("c:\\myfile.pdf",data) 

Response.BinaryWrite(data) 
Response.End() 
+0

會將此流的瀏覽器,並保存到磁盤? – highwingers

+0

是的。這將寫入文件並將其發送到瀏覽器。 – nunespascal

0
<%@ WebHandler Language="C#" Class="mergeByteForms" %> 
using System; 
using System.IO; 
using System.Web; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class mergeByteForms : IHttpHandler { 
    HttpServerUtility Server; 
    public void ProcessRequest (HttpContext context) { 
    Server = context.Server; 
    HttpResponse Response = context.Response; 
    Response.ContentType = "application/pdf"; 
    using (Document document = new Document()) { 
     using (PdfSmartCopy copy = new PdfSmartCopy(
     document, Response.OutputStream)) 
     { 
     document.Open(); 
     for (int i = 0; i < 2; ++i) { 
      PdfReader reader = new PdfReader(_getPdfBtyeStream(i.ToString())); 
      copy.AddPage(copy.GetImportedPage(reader, 1)); 
     } 
     } 
    } 
    } 
    public bool IsReusable { get { return false; } } 

// simulate your method to use __one__ byte stream for __one__ PDF 
    private byte[] _getPdfBtyeStream(string data) { 
// replace with __your__ PDF template 
    string pdfTemplatePath = Server.MapPath(
     "~/app_data/template.pdf" 
    ); 
    PdfReader reader = new PdfReader(pdfTemplatePath); 
    using (MemoryStream ms = new MemoryStream()) { 
     using (PdfStamper stamper = new PdfStamper(reader, ms)) { 
     AcroFields form = stamper.AcroFields; 
// replace this with your form field data 
     form.SetField("title", data); 
     // ... 
// this is __VERY__ important; since you're using the same fillable 
// PDF, if you don't set this property to true the second page will 
// lose the filled fields.   
     stamper.FormFlattening = true; 
     } 
     return ms.ToArray(); 
    } 
    } 
}