Map System.Drawing.Image to proper PdfName.COLORSPACE and PdfName.FILTER in iTextSharp -


i'm using itextsharp update image object in pdf modified system.drawing.image. how set pdfname.colorspace , pdfname.filter based on system.drawing.image? i'm not sure system.drawing.image properties can used mappings.

private void setimagedata(pdfimageobject pdfimage, system.drawing.image image, byte[] imagedata) {     prstream imgstream = (prstream)pdfimage.getdictionary();     imgstream.clear();     imgstream.setdata(imagedata, false, prstream.no_compression);     imgstream.put(pdfname.type, pdfname.xobject);     imgstream.put(pdfname.subtype, pdfname.image);     imgstream.put(pdfname.width, new pdfnumber(image.width));     imgstream.put(pdfname.height, new pdfnumber(image.height));     imgstream.put(pdfname.length, new pdfnumber(imagedata.longlength));      // not sure how set these entries based on image properties     imgstream.put(pdfname.bitspercomponent, 8);     imgstream.put(pdfname.colorspace, pdfname.devicergb);     imgstream.put(pdfname.filter, pdfname.dctdecode); } 

i took advice of chris haas , cheated writing system.drawing.image temporary pdf , read out pdfimageobject.

using (memorystream ms = new memorystream()) {     using (itextsharp.text.document doc = new itextsharp.text.document())     {         using (itextsharp.text.pdf.pdfwriter writer = itextsharp.text.pdf.pdfwriter.getinstance(doc))         {             doc.open();              itextsharp.text.image image = itextsharp.text.image.getinstance(drawingimage, drawingimage.rawformat);             image.simplifycolorspace();              doc.add(image);             doc.close();         }     }     ... code opens mem stream pdfreader , retrieves image pdfimageobj... } 

that worked seemed allot of side stepping cheat. stepped through code in call doc.add(image) , found pdfimage object created itextsharp.text.image object , pdfimage object contained dictionary entries needed. decided cut corners on original cheat , come final solution:

private void setimagedata(pdfimageobject pdfimageobj, byte[] imagedata) {     itextsharp.text.image image = itextsharp.text.image.getinstance(imagedata);     image.simplifycolorspace();      pdfimage temppdfimage = new pdfimage(image, "tempimg", null);      prstream imgstream = (prstream)pdfimageobj.getdictionary();     imgstream.clear();     imgstream.setdataraw(imagedata);     imgstream.merge(temppdfimage); } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -