I'm trying to retrieve binary image data from my SQL server and export it into my pdf file by using the follow method
phrase.Add(new Chunk("Image :", normalFont));
Byte[] bytes = (Byte[])dr[0];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
phrase.Add(bytes);
table.AddCell(phrase);
When i'm trying to display binary image data into my webapp using the above method, it works perfectly fine. Unfortunately when i want to export the image into my pdf file it doesn't work.
I have error like this on the phrase.Add method. I know i'm doing something wrong but i cant figure it out
Here is my entire back-end code for my PDF button.
protected void btnPDF_Click(object sender, EventArgs e)
{
var doc1 = new Document();
var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop\\New folder", filename), FileMode.Create);
PdfWriter.GetInstance(doc1, output);
PdfPCell cell = null;
doc1.Open();
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 585f;
table.LockedWidth = true;
var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
doc1.Add(logo);
var titleFont = FontFactory.GetFont("Arial", 18, 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, pr.policeid, pr.prdatetime, mr.citizenreport, pr.policereport, aor.officialreport, mr.image1 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(new Chunk("Contact :", normalFont));
phrase.Add(dr[1].ToString());
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(new Chunk("Location :", normalFont));
phrase.Add(dr[3].ToString());
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(new Chunk("Police ID :", normalFont));
phrase.Add(dr[5].ToString());
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(new Chunk("Police Report Date Time :", normalFont));
phrase.Add(dr[6].ToString());
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(new Chunk("Citizen Report :", normalFont));
phrase.Add(dr[7].ToString());
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(new Chunk("Police Report :", normalFont));
phrase.Add(dr[8].ToString());
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(new Chunk("Official Report :", normalFont));
phrase.Add(dr[9].ToString());
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(Chunk.NEWLINE);
phrase.Add(new Chunk("Image :", normalFont));
Byte[] bytes = (Byte[])dr[0];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
phrase.Add(bytes);
table.AddCell(phrase);
}
dr.Close();
doc1.Add(table);
doc1.Close();