2013-04-22 154 views
1

我有一個pdf模板,我想爲數據庫中的每個記錄生成。 我發現這個,但它只適用於物理文件。如何使這項工作只在內存中? 我嘗試使用MemoryStream的,但我不能與PdfReaderitextsharp multipage pdf格式

閱讀本
using iTextSharp.text.pdf; 
using System; 
using System.IO; 

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     string uniqueidstr = Guid.NewGuid().ToString(); 

     string file = Server.MapPath("~/Reports/PrimaVisita.pdf"); 

     string MasterPdfFile = Server.MapPath("./MstrCopyFile" + uniqueidstr + ".pdf").ToString(); 

     PdfCopyFields copy = new PdfCopyFields(new FileStream(MasterPdfFile, FileMode.CreateNew)); 

     for (int i = 0; i < 10; i++) 
     { 
      PdfReader tmpReader = new PdfReader(file); 
      string tmpfilepath = Server.MapPath("./tmpFile" + uniqueidstr + i.ToString() + ".pdf").ToString(); 

      FileStream tmpOutFileStream = new FileStream(tmpfilepath, FileMode.CreateNew); 
      PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpOutFileStream); 
      AcroFields fields = tmpOutStamper.AcroFields; 
      fields.SetField("Cognome", "pippo" + i.ToString()); 
      fields.SetField("Nome", "pluto" + i.ToString()); 
      fields.SetField("DatiNascita", "paperino" + i.ToString()); 
      fields.SetField("Residenza", "topolino" + i.ToString()); 

      tmpOutStamper.Writer.CloseStream = false; 
      tmpOutStamper.FormFlattening = true; 
      tmpOutStamper.Close(); 
      tmpOutFileStream.Close(); 

      PdfReader nwPgReader = new PdfReader(tmpfilepath); 

      copy.AddDocument(nwPgReader); 
     } 

     copy.Close(); 
    } 
} 

錯誤是「沒有找到PDF頭標識。」 這裏是代碼

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string uniqueidstr = Guid.NewGuid().ToString(); 

     string file = Server.MapPath("~/Reports/PrimaVisita.pdf"); 

     MemoryStream masterStream = new MemoryStream(); 
     //string MasterPdfFile = Server.MapPath("./MstrCopyFile" + uniqueidstr + ".pdf").ToString(); 

     PdfCopyFields copy = new PdfCopyFields(masterStream); 

     for (int i = 0; i < 10; i++) 
     { 
      PdfReader tmpReader = new PdfReader(file); 
      string tmpfilepath = Server.MapPath("./tmpFile" + uniqueidstr + i.ToString() + ".pdf").ToString(); 
      MemoryStream tmpStream = new MemoryStream(); 

      //FileStream tmpOutFileStream = new FileStream(tmpfilepath, FileMode.CreateNew); 
      PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpStream); 
      AcroFields fields = tmpOutStamper.AcroFields; 
      fields.SetField("Cognome", "pippo" + i.ToString()); 
      fields.SetField("Nome", "pluto" + i.ToString()); 
      fields.SetField("DatiNascita", "paperino" + i.ToString()); 
      fields.SetField("Residenza", "topolino" + i.ToString()); 

      tmpOutStamper.Writer.CloseStream = false; 
      tmpOutStamper.FormFlattening = true; 
      tmpOutStamper.Close(); 
      //tmpOutFileStream.Close(); 

      PdfReader nwPgReader = new PdfReader(tmpStream); 

      copy.AddDocument(nwPgReader); 
     } 

     copy.Close(); 
    } 
+0

你說*我試圖用MemoryStream的,但我不能與PdfReader * ---當你嘗試過什麼事閱讀本? – mkl 2013-04-22 10:20:58

+0

坦克的答覆有一個關於pdf標題的錯誤 – Bruno 2013-04-22 10:40:51

+0

這聽起來像'MemoryStream'有一個不正確的位置閱讀。請將您使用的代碼添加到您的問題中。 – mkl 2013-04-22 11:49:16

回答

1

我在字節數組轉換MemoryStream的解決,現在它的工作原理。

坦克的支持

布魯諾

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string uniqueidstr = Guid.NewGuid().ToString(); 

     string file = Server.MapPath("~/Reports/PrimaVisita.pdf"); 

     MemoryStream masterStream = new MemoryStream(); 

     PdfCopyFields copy = new PdfCopyFields(masterStream); 

     for (int i = 0; i < 10; i++) 
     { 
      PdfReader tmpReader = new PdfReader(file); 
      MemoryStream tmpStream = new MemoryStream(); 
      tmpStream.Position = 0; 

      PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpStream); 
      AcroFields fields = tmpOutStamper.AcroFields; 
      fields.SetField("Cognome", "pippo" + i.ToString()); 
      fields.SetField("Nome", "pluto" + i.ToString()); 
      fields.SetField("DatiNascita", "paperino" + i.ToString()); 
      fields.SetField("Residenza", "topolino" + i.ToString()); 

      tmpOutStamper.Writer.CloseStream = false; 
      tmpOutStamper.FormFlattening = true; 
      tmpOutStamper.Close(); 
      byte[] buf = tmpStream.ToArray(); 

      PdfReader nwPgReader = new PdfReader(buf); 

      copy.AddDocument(nwPgReader); 
     } 

     copy.Close(); 


}