2010-05-12 70 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(); 
}