2010-03-03 4 views
5

Có thể tạo tài liệu PDF trong bộ nhớ với iTextSharp cho phép người dùng lựa chọn "mở" hoặc "lưu" ?, và nếu nó mở ra thì nó sẽ mở ra trong cửa sổ trình duyệt.mở tài liệu PDF từ bộ nhớ

Hiện tại, tôi chỉ lưu nó vào đĩa.

CHỈNH SỬA:

ok Tôi đã lo lắng. Tôi đã kết thúc việc phải ghi tập tin vào một thư mục, nhưng nó chỉ là tạm thời khi bị ghi đè mỗi lần. Dưới đây là giải pháp cho những gì nó có giá trị:

private void GeneratePDF() { 

    var doc1 = new Document(); 
    string path = Server.MapPath("~/pdfs/"); 
    string filepath = path + "Doc1.pdf"; 
    PdfWriter.GetInstance(doc1, new FileStream(filepath, FileMode.Create)); 

    doc1.Open(); 
    doc1.Add(new Paragraph("A new Document"));   
    doc1.Add(new Paragraph(DateTime.Now.ToString())); 

    doc1.Close(); 

    Response.Buffer = false; //transmitfile self buffers 
    Response.Clear(); 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=myPDF.pdf"); 
    Response.TransmitFile(filepath); 
    Response.End(); 

}

+0

Hãy xem xét đăng giải pháp của bạn làm câu trả lời và đánh dấu nó là được chấp nhận. – dckuehn

Trả lời

0

Bạn sẽ cần phải lưu nó vào một thư mục tạm thời, sau đó gọi Process.Start vào tệp.

0

Để mở/hiển thị tệp PDF, bạn có thể sử dụng thành phần acrobat activex sau khi lưu tệp vào thư mục tạm thời. Tôi không thể tìm thấy một kiểm soát miễn phí để hiển thị các tệp PDF trong một nghiên cứu trước đó.

3

Bạn có thể lưu tệp PDF vào bộ nhớ và ghi nó ra trình duyệt như thế này.

protected void Page_Load(object sender, EventArgs e) 
{ 
    MemoryStream ms; 

    using (ms = new MemoryStream()) 
    { 
     PdfWriter writer = PdfWriter.GetInstance(myPdfDoc, ms); 

     Response.ContentType = "application/pdf"; 
     Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
     Response.OutputStream.Flush(); 
     Response.OutputStream.Close(); 

    } 
} 
+0

Tôi đang tìm kiếm một cái gì đó như thế này, nhưng tôi nghĩ rằng một cái gì đó vẫn còn thiếu. Tôi có phải đặt Nội dung/Loại – Dkong

+0

Có, bạn rất có thể, hãy xem bản chỉnh sửa. –