iTextSharp - Add image to PDF from Datatable -


i try create pdf report datatable. 1 of columns contents image. how can extract image datatable , insert pdf table? i'm using itextshap version 5.4.2.0. here code:

    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); <-- problem code                 table.addcell(img);                 table.addcell(new phrase(r[7].tostring(), hel8));             }         }         doc.add(table);     }               doc.close();             } 

update: @nekno, of suggestions worked.

but still need correct casting @ line:

byte[] byt = (byte[])r[6]; 

it gave me casting exception vs2008. added conversion function (pulled stackoverflow):

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;     } 

and revised code:

byte[] byt = imagetobyte((system.drawing.image)dt.rows[e][6]); 

thanks.

what problem? happens when use problem code?

try 1 of other image.getinstance() overloads:

you can pass byte array directly:

byte[] byt = (byte[])r[6]; image img = image.getinstance(byt); 

or can pass stream:

byte[] byt = (byte[])r[6]; memorystream ms = new memorystream(byt); image img = image.getinstance(ms); 

or can give itextsharp more info image format:

byte[] byt = (byte[])r[6]; memorystream ms = new memorystream(byt); system.drawing.image sdi = system.drawing.image.fromstream(ms); image img = image.getinstance(sdi, imageformat.png); 

if column can cast system.drawing.image, can use directly:

image img = image.getinstance((system.drawing.image)r[6], system.drawing.imaging.imageformat.png); 

Comments