데이터 테이블에서 PDF 보고서를 만들려고합니다. 내용 이미지 중 하나입니다. 데이터 테이블에서 이미지를 추출하고 PDF 테이블에 삽입하려면 어떻게해야합니까? iTextShap 버전 5.4.2.0을 사용하고 있습니다.iTextSharp - Datatable에서 PDF에 이미지 추가
public void Report(DataTable dt, string output)
{
Document doc = new Document(PageSize.LETTER, 50, 50, 80, 50);
PdfWriter PDFWriter = PdfWriter.GetInstance(doc, new FileStream(output, FileMode.Create));
PDFWriter.ViewerPreferences = PdfWriter.PageModeUseOutlines;
iTextSharp.text.Font hel8 = FontFactory.GetFont(BaseFont.HELVETICA, 8);
doc.Open();
PdfPTable table = new PdfPTable(dt.Columns.Count);
float[] widths = new float[] { 1.2f, 1.2f, 1.2f, 1.2f, 1f, 4f, 1f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell(new Phrase("NewCells"));
cell.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, hel8));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), hel8));
table.AddCell(new Phrase(r[1].ToString(), hel8));
table.AddCell(new Phrase(r[2].ToString(), hel8));
table.AddCell(new Phrase(r[3].ToString(), hel8));
table.AddCell(new Phrase(r[4].ToString(), hel8));
table.AddCell(new Phrase(r[5].ToString(), hel8));
byte[] byt = (byte[])r[6];
MemoryStream ms = new MemoryStream(byt);
System.Drwaing.Image sdi = System.Drawing.Image.FromStream(ms);
Image img = Image.GetInstance(sdi); <-- this is the problem code
table.AddCell(img);
table.AddCell(new Phrase(r[7].ToString(), hel8));
}
}
doc.Add(table);
}
doc.Close();
}
업데이트 : 여기에 코드입니다 @nekno, 제안의 모든 일을하고 있습니다.
는하지만 난 여전히 줄에서 캐스팅을 수정해야합니다
byte[] byt = (byte[])r[6];
그것은 VS2008에서 나에게 캐스팅 예외를했다.
byte[] ImageToByte(System.Drawing.Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
을 그리고 코드 수정 : 그래서 (유래에서 뽑아) 변환 기능을 추가
byte[] byt = ImageToByte((System.Drawing.Image)dt.Rows[e][6]);
감사합니다.
당신의 열이'System.Drawing.Image'에 캐스트 할 수있는 경우에, 당신은 직접 사용할 수 있습니다'이미지 IMG = Image.GetInstance ((System.Drawing.Image) R [6], 시스템 .Drawing.Imaging.ImageFormat.Png); ' – nekno