2013-07-11 44 views
1

我試圖使用iTextSharp創建pdf並將其轉換爲字節並使用保存對話框下載PDF文件。我已經使用MemoryStream將PDF轉換爲字節,並使用HttpResponse Page方法下載後面的PDF文件,但它似乎沒有工作。它沒有任何錯誤,單擊按鈕後沒有任何事情發生(使用Chrome,FireFox)請幫助我,並檢查我做錯了哪一部分。謝謝!將iTextSharp pdf保存爲字節並使用保存對話框下載

protected void btnPDF_Click(object sender, EventArgs e) 
{ 
byte[] pdfBytes; 
using (var ms = new MemoryStream())c 
{ 
    var doc1 = new Document(); 
    PdfWriter writer = PdfWriter.GetInstance(doc1, ms); 
    doc1.Open(); 

    PdfPTable table = new PdfPTable(1); 

    table.TotalWidth = 585f; 
    table.LockedWidth = true; 

    iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(); 

    var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg")); 
    doc1.Add(logo); 

    var titleFont = FontFactory.GetFont("Arial", 15, Font.BOLD); 
    doc1.Add(new Paragraph("Official Report. Member Report ID : " + DDLCase.SelectedValue, titleFont)); 

    var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD); 
    var phrase = new Phrase(); 

    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI"); 

    SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, mr.citizenreport, pr.policeid, pr.prdatetime, pr.policereport, aor.officialreport, mr.image1, mr.image2, mr.image3, mr.image4, mr.image5, pr.image1, pr.image2, pr.image3, pr.image4, pr.image5 from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro, AdminOfficialReport aor where mr.memberreportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and pr.policereportid=aor.policereportid", con); 
    con.Open(); 
    SqlDataReader dr; 

    dr = cm.ExecuteReader(); 

    if (dr.Read()) 
    { 

     phrase.Add(new Chunk("Full Name :", normalFont)); 
     phrase.Add(dr[0].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Contact :", normalFont)); 
     phrase.Add(dr[1].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Type Of Crime :", normalFont)); 
     phrase.Add(dr[2].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Location :", normalFont)); 
     phrase.Add(dr[3].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Citizen Report Date Time :", normalFont)); 
     phrase.Add(dr[4].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Citizen Report :", normalFont)); 
     phrase.Add(dr[5].ToString()); 

     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 
     phrase.Add(Chunk.NEWLINE); 

     phrase.Add(new Chunk("Citizen Images :", normalFont)); 

     phrase.Add(new Chunk("1.", normalFont)); 
     Byte[] bytes1 = (Byte[])dr[10]; 
     iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1); 
     image1.ScaleToFit(1150f, 1150f); 
     Chunk imageChunk1 = new Chunk(image1, 0, 0); 
     phrase.Add(imageChunk1); 

     phrase.Add(new Chunk("2.", normalFont)); 
     Byte[] bytes5 = (Byte[])dr[11]; 
     iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5); 
     image5.ScaleToFit(1150f, 1150f); 
     Chunk imageChunk5 = new Chunk(image5, 0, 0); 
     phrase.Add(imageChunk5); 

     phrase.Add(new Chunk("3.", normalFont)); 
     Byte[] bytes6 = (Byte[])dr[12]; 
     iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6); 
     image6.ScaleToFit(1150f, 1150f); 
     Chunk imageChunk6 = new Chunk(image6, 0, 0); 
     phrase.Add(imageChunk6); 

     phrase.Add(new Chunk("4.", normalFont)); 
     Byte[] bytes7 = (Byte[])dr[13]; 
     iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7); 
     image7.ScaleToFit(1150f, 1150f); 
     Chunk imageChunk7 = new Chunk(image7, 0, 0); 
     phrase.Add(imageChunk7); 

     phrase.Add(new Chunk("5.", normalFont)); 
     Byte[] bytes8 = (Byte[])dr[14]; 
     iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8); 
     image8.ScaleToFit(1150f, 1150f); 
     Chunk imageChunk8 = new Chunk(image8, 0, 0); 
     phrase.Add(imageChunk8); 

     table.AddCell(phrase); 
    } 
    dr.Close(); 
    doc1.Add(table); 
    doc1.NewPage(); 
    doc1.Add(table2); 
    doc1.Close(); 

    pdfBytes = ms.ToArray(); 
} 

Response.Clear(); 
Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "Report.pdf")); 
Response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length); 
} 

我測試了使用IE得到這個錯誤:

JScript的匿名函數[MicrosoftAjax.js]第6行腳本

對不起的人那裏只是這個代碼不代碼沒有錯誤使用AJAX控件UpdatePanel工作。感謝觀看,幫助。

回答

0

使用此

string fileName = string.Format(Guid.NewGuid()+".pdf"); 
htmlToPdf(Server.MapPath(@"~\temp\") + fileName); 
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName); 
Response.ContentType = "application/pdf"; 
Response.TransmitFile(filePath); 
Response.End(); 
相關問題