c# - Resize existing image in DocX using OpenXML sdk -


got template docx image placeholder replaced correct picture.

private void setimagepartdata(imagepart imagepart, byte[] data) {     if (imagepart != null)     {         using (var writer = new binarywriter(imagepart.getstream()))         {             writer.write(data);         }     } } 

but preserves placeholder size. how change actual image size? byte array aqquared image on server, size known.

if mean content control placeholder can use following code once needed:

//get sdtelement (can block, run... use base class) corresponding tag sdtelement block = doc.maindocumentpart.document.body.descendants<sdtelement>()   .firstordefault(sdt => sdt.sdtproperties.getfirstchild<tag>()?.val == contentcontroltag);  //get first drawing element , original sizes of placeholder sdt //i use sdt placeholder size maximum size calculate picture size correct ratios drawing sdtimage = block.descendants<drawing>().first(); double sdtwidth = sdtimage.inline.extent.cx; double sdtheight = sdtimage.inline.extent.cy; double sdtratio = sdtwidth / sdtheight;  *calculate final width/height of image*  //resize picture placeholder sdtimage.inline.extent.cx = finalwidth; sdtimage.inline.extent.cy = finalheight;  //change width/height of picture shapeproperties transform //this override above height/width until manually drag image example sdtimage.inline.graphic.graphicdata     .getfirstchild<documentformat.openxml.drawing.pictures.picture>()     .shapeproperties.transform2d.extents.cx = finalwidth; sdtimage.inline.graphic.graphicdata     .getfirstchild<documentformat.openxml.drawing.pictures.picture>()     .shapeproperties.transform2d.extents.cy = finalheight; 

but can use if using image in word document too. need locate correct drawing element contains reference imagepart. can use bottom part of code adjust image size. it's important adjust both transform2d x , y inline x , y or image size won't changed.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -