2010-05-12 38 views
29

Cách tạo PDF trong bộ nhớ thay vì tệp vật lý bằng itextsharp.Tạo PDF trong bộ nhớ thay vì tệp vật lý

Mã bên dưới đang tạo tệp pdf thực.

Thay vì làm thế nào tôi có thể tạo ra một byte [] và lưu nó trong byte [] vì vậy mà tôi có thể trả lại thông qua một hàm

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 

Trả lời

25

Chuyển dòng phim bằng bộ nhớ.

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

đâu mã của bạn có new FileStream, vượt qua trong một MemoryStream bạn đã đã được tạo. (Đừng chỉ tạo ra nó inline trong lệnh gọi PdfWriter.GetInstance - bạn sẽ muốn để có thể tham khảo nó sau này.)

Sau đó gọi ToArray() trên MemoryStream khi bạn đã hoàn tất văn bản cho nó để có được một byte[] :

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

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

tôi đã không sử dụng iTextSharp, nhưng tôi nghi ngờ một số trong những loại thực hiện IDisposable - trong trường hợp này bạn nên tạo chúng trong using báo cáo quá.

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 không triển khai IDisposable để bạn không thể sử dụng nó trong tuyên bố sử dụng. Có lẽ đây chỉ là trong phiên bản tôi đang sử dụng mặc dù (5.0.5) như tôi biết đã có một số thay đổi lớp học từ phiên bản 4 – musefan

+3

@ musefan, có, trong 5.0.5 đó là trường hợp. Trong phiên bản hiện tại, 5.5, 'PdfWriter' mở rộng' DocWriter', thực hiện 'IDocListener' mở rộng' IDisposable'. Tôi không biết chính xác khi nào 'IDisposable' được thêm vào' IDocListener' nhưng đã một thời gian sau 5.0.5 và trước 5.5.0. –

14

Tôi chưa bao giờ sử dụng iTextPDF trước đây nhưng có vẻ thú vị nên tôi đã thử thách và tự mình thực hiện một số nghiên cứu. Dưới đây là cách phát trực tuyến tài liệu PDF qua bộ nhớ.

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(); 
} 
Các vấn đề liên quan