2010-05-12 53 views
29

如何在內存流中創建PDF而不是使用itextsharp創建物理文件。在內存中創建PDF而不是物理文件

以下代碼正在創建實際的pdf文件。

相反,我怎麼可以創建一個byte [],並將其存儲在一個字節[],這樣我可以通過函數

using iTextSharp.text; 
using iTextSharp.text.pdf; 
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); 
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create)); 
doc.Open();//Open Document to write 
Paragraph paragraph = new Paragraph("This is my first line using Paragraph."); 
Phrase pharse = new Phrase("This is my second line using Pharse."); 
Chunk chunk = new Chunk(" This is my third line using Chunk."); 

doc.Add(paragraph); 

doc.Add(pharse); 

doc.Add(chunk); 
doc.Close(); //Close document 

回答

25

使用內存流切換文件流。

MemoryStream memStream = new MemoryStream(); 
PdfWriter wri = PdfWriter.GetInstance(doc, memStream); 
... 
return memStream.ToArray(); 
7

如果你的代碼已經new FileStream返回它,傳遞你已經一個MemoryStream已經創建。 (不要只是創建它內嵌在調用PdfWriter.GetInstance - 你會希望以後能夠參考一下吧。)

然後呼籲MemoryStreamToArray()當你向它寫完獲得byte[]

using (MemoryStream output = new MemoryStream()) 
{ 
    PdfWriter wri = PdfWriter.GetInstance(doc, output); 

    // Write to document 
    // ... 
    return output.ToArray(); 
} 

我沒有使用iTextSharp的,但我懷疑一些這些類型的實施IDisposable - 在這種情況下,你應該在using語句創建他們。

20
using iTextSharp.text; 
using iTextSharp.text.pdf; 
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); 

byte[] pdfBytes; 
using(var mem = new MemoryStream()) 
{ 
    using(PdfWriter wri = PdfWriter.GetInstance(doc, mem)) 
    { 
     doc.Open();//Open Document to write 
     Paragraph paragraph = new Paragraph("This is my first line using Paragraph."); 
     Phrase pharse = new Phrase("This is my second line using Pharse."); 
     Chunk chunk = new Chunk(" This is my third line using Chunk."); 

     doc.Add(paragraph); 

     doc.Add(pharse); 

     doc.Add(chunk); 
    } 
    pdfBytes = mem.ToArray(); 
} 
+0

PdfWriter沒有實現IDisposable,所以你不能在using語句中使用它。也許這只是我正在使用的版本(5.0.5),因爲我知道版本4 – musefan 2012-01-05 11:49:15

+3

@musefan有一些類更改,是的,在5.0.5中就是這樣。在當前版本5.5中,'PdfWriter'擴展了'DocWriter',它實現了'IDocListener',它擴展了'IDisposable'。我不知道什麼時候IDisposable被添加到IDocListener中,但是它在5.0.5之前和5.5.0之前有一段時間。 – 2014-02-28 23:55:05

14

我以前從未使用過iTextPDF但它聽起來很有意思,所以我上臺後,面臨的挑戰,並做了我自己的一些研究。以下是如何通過內存流式傳輸PDF文檔。

protected void Page_Load(object sender, EventArgs e) 
{ 
    ShowPdf(CreatePDF2()); 
} 

private byte[] CreatePDF2() 
{ 
    Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50); 

    using (MemoryStream output = new MemoryStream()) 
    { 
     PdfWriter wri = PdfWriter.GetInstance(doc, output); 
     doc.Open(); 

     Paragraph header = new Paragraph("My Document") {Alignment = Element.ALIGN_CENTER}; 
     Paragraph paragraph = new Paragraph("Testing the iText pdf."); 
     Phrase phrase = new Phrase("This is a phrase but testing some formatting also. \nNew line here."); 
     Chunk chunk = new Chunk("This is a chunk."); 

     doc.Add(header); 
     doc.Add(paragraph); 
     doc.Add(phrase); 
     doc.Add(chunk); 

     doc.Close(); 
     return output.ToArray(); 
    } 

} 

private void ShowPdf(byte[] strS) 
{ 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now); 

    Response.BinaryWrite(strS); 
    Response.End(); 
    Response.Flush(); 
    Response.Clear(); 
}