Next we will introduce how to insert images in Word. By the way, the younger brother found that the msdn official website has a complete openxml tutorial. Although it is full of English, it is still very helpful.
Tips, there is no template in the original excerpt code. We found that the size of the picture inserted in Word is different from that in copy. We recommend that you insert an image in Word by yourself, decompile the C # code through openxml tools, change the image size, and decompile the code again.
Use byeond compare [http://www.scootersoftware.com/] To compare C # code, it will be found that because new DW. extent () {cx = 99213l, Cy = 792000l} is because of this set. In the future, we can use openxml tools for decompilation and comparison in many places. View the attribute position of the Set style.
Http://msdn.microsoft.com/en-us/library/office/bb491088 (V = Office.15). aspx
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using documentformat. openxml; using documentformat. openxml. packaging; using documentformat. openxml. wordprocessing; using system. io; using DW = documentformat. openxml. drawing. wordprocessing; Using PIC = documentformat. openxml. drawing. pictures; using a = documentformat. openxml. drawing; namespace ad Dpicturew.word {public class program {public static void main (string [] ARGs) {string picpath = "regular"; string filepath = "test.docx"; addpicturew.word (filepath, picpath );} public static void addpicturew.word (string filepath, string picturepath) {using (wordprocessingdocument Doc = wordprocessingdocument. open (filepath, true) {string pictype = picturepath. Split ('. '). last (); imageparttype; imagepart = NULL; // The image type is determined by the suffix. True indicates that the case-insensitive if (enum. tryparse <imageparttype> (pictype, true, out imageparttype) {imagepart = Doc. maindocumentpart. addimagepart (imageparttype);} imagepart. feeddata (file. open (picturepath, filemode. open); // read the binary stream addimagetobody (Doc, Doc. maindocumentpart. getidofpart (imagepart);} // Excerpted from http://msdn.m Icrosoft.com/en-us/library/office/bb497430 (V = Office.15). aspx? CS-save-lang = 1 & CS-lang = CSHARP # code-snippet-5
Private Static void addimagetobody (wordprocessingdocument worddoc, string relationshipid) {// define the reference of the image. vaR element = new drawing (New DW. inline (New DW. extent () {cx = 9910000l, Cy = 792000l}, // adjust the image size new DW. extends textent () {leftedge = 0l, topedge = 0l, rightedge = 0l, BottomEdge = 0l}, new DW. docproperties () {id = (uint32value) 1u, name = "Picture 1"}, new DW. nonvisualgraphicframedrawingproperties (new. graphicframelocks () {nochangeaspect = true}), new. graphic (new. graphicdata (new pic. picture (new pic. nonvisualpictureproperties (new pic. nonvisualdrawingproperties () {id = (uint32value) 0u, name = "New bitmap image.jpg"}, new pic. nonvisualpicturedrawingproperties (), new pic. blipfill (new. blip (new. blipextensionlist (new. blipextension () {uri = "{28a0092b-c50c-407e-a947-70e740481c1c}"}) {embed = relationshipid, compressionstate =. blipcompressionvalues. print}, new. stretch (new. fillrectangle (), new pic. shapeproperties (new. transform2d (new. offset () {x = 0l, y = 0l}, new. extents () {cx = 9910000l, Cy = 792000l}), // align with the above new. presetgeometry (new. adjustvaluelist () {preset =. shapetypevalues. rectangle}) {uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"}) {distancefromtop = (uint32value) 0u, distancefrombottom = (uint32value) 0u, distancefromleft = (uint32value) 0u, distancefromright = (uint32value) 0u, editid = "50d07946"}); // append the reference to body, the element shoshould be in a run. worddoc. maindocumentpart. document. body. appendchild (new paragraph (new run (element )));}}}
Openxml entry-insert an image in Word