2010-05-12 16 views
29

Come si crea un PDF in un memorystream anziché in un file fisico utilizzando itextsharp.Crea PDF in memoria anziché file fisico

Il codice seguente sta creando il file pdf effettivo.

Invece come posso creare un byte [] e conservarlo in byte [] in modo che possa restituirlo tramite una funzione

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 

risposta

25

Passare al filestream con un memorandtream.

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

Dove il codice ha new FileStream, passare un MemoryStream hai già creato. (Non basta crearla in linea nella chiamata a PdfWriter.GetInstance - si vorrà essere in grado di fare riferimento ad esso in seguito.)

quindi chiamare ToArray() sul MemoryStream quando hai finito di scrivere ad esso per ottenere un byte[] :

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

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

non ho usato iTextSharp, ma ho il sospetto alcuni di questi tipi implementano IDisposable - nel qual caso si dovrebbe essere la creazione di loro in using dichiarazioni troppo.

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 non implementa IDisposable, quindi non è possibile utilizzarlo in un'istruzione using. Forse questo è solo nella versione che sto usando (5.0.5) perché so che ci sono stati alcuni cambiamenti di classe dalla versione 4 – musefan

+3

@musefan, sì, in 5.0.5 è il caso. Nella versione attuale, 5.5, 'PdfWriter' estende' DocWriter' che implementa 'IDocListener' che estende' IDisposable'. Non so esattamente quando 'IDisposable' è stato aggiunto a' IDocListener' ma è passato del tempo dopo 5.0.5 e prima di 5.5.0. –

14

non ho mai usato iTextPDF prima, ma sembrava interessante così ho preso sulla sfida e ha fatto qualche ricerca per conto mio. Ecco come eseguire lo streaming del documento PDF tramite memoria.

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(); 
} 
Problemi correlati