c# - Insert an image into a .docx file -
i developing program has feature dynamically create docx files. title suggests, having problem inserting images docx file. 1 of issues, see it, using c# 2.0. (when answering question, stress not wish switch c# 3.0, please not try persuade me.)
have taken @ msdn article @ http://msdn.microsoft.com/en-us/library/office/bb497430.aspx, when converted c# 3.0 code msdn uses c# 2.0, document not open, , gives me error: "the file testing.docx cannot opened because there problems contents," , "no error detail available."
here code:
imagepart ip_part; wordprocessingdocument wpd_doc = wordprocessingdocument.create("c:\\convert\\testing.docx", wordprocessingdocumenttype.document); public testing() { wpd_doc.addmaindocumentpart(); wpd_doc.maindocumentpart.document = new document(); wpd_doc.maindocumentpart.document.body = new body(); ip_part = wpd_doc.maindocumentpart.addimagepart(imageparttype.png); system.io.filestream fs_stream = new system.io.filestream("image.png", system.io.filemode.open); ip_part.feeddata(fs_stream); addimagetobody("image.png", wpd_doc.maindocumentpart.getidofpart(ip_part)); appendtext("here test bulleted list:"); wpd_doc.maindocumentpart.document.save(); //close document. wpd_doc.close(); } private void addimagetobody(string s_imagepath, string s_relationshipid) { //openxmlelement oxe_element = new numbering(); drawing d_drawing = new drawing(); drawword.inline i_inline = new drawword.inline(); drawword.extent e_extent = new drawword.extent(); e_extent.cx = 600075; //63px / 96dpi * 914400 e_extent.cy = 600075; //63px / 96dpi * 914400 i_inline.extent = e_extent; drawword.docproperties dp_prop = new drawword.docproperties(); //dp_prop.id = uint.parse(system.datetime.now.minute.tostring() + system.datetime.now.second.tostring() + system.datetime.now.millisecond.tostring()); dp_prop.id = 1; dp_prop.name = "picture 1"; dp_prop.description = "an automated image."; i_inline.docproperties = dp_prop; drawword.nonvisualgraphicframedrawingproperties nvgfdp_prop = new drawword.nonvisualgraphicframedrawingproperties(); draw.graphicframelocks gfl_locks = new draw.graphicframelocks(); gfl_locks.nochangeaspect = true; nvgfdp_prop.graphicframelocks = gfl_locks; i_inline.nonvisualgraphicframedrawingproperties = nvgfdp_prop; draw.graphic g_graphic = new draw.graphic(); draw.graphicdata gd_data = new documentformat.openxml.drawing.graphicdata(); gd_data.uri = datetime.now.minute.tostring() + datetime.now.second.tostring() + datetime.now.millisecond.tostring(); drawpic.picture p_pic = new drawpic.picture(); drawpic.nonvisualpictureproperties nvpp_prop = new drawpic.nonvisualpictureproperties(); drawpic.nonvisualdrawingproperties nvdp_prop = new drawpic.nonvisualdrawingproperties(); nvdp_prop.id = uint.parse(system.datetime.now.minute.tostring() + system.datetime.now.second.tostring() + system.datetime.now.millisecond.tostring()); //nvdp_prop.name = s_imagepath; nvdp_prop.name = "new_image.png"; nvpp_prop.nonvisualdrawingproperties = nvdp_prop; drawpic.nonvisualpicturedrawingproperties nvpdp_prop = new drawpic.nonvisualpicturedrawingproperties(); nvpp_prop.nonvisualpicturedrawingproperties = nvpdp_prop; p_pic.nonvisualpictureproperties = nvpp_prop; drawpic.blipfill bf_fill = new drawpic.blipfill(); draw.blip b_blip = new draw.blip(); draw.extensionlist el_list = new draw.extensionlist(); draw.blipextension be_extension = new draw.blipextension(); be_extension.uri = "{28a0092b-c50c-407e-a947-70e740481c1c}"; el_list.append(be_extension); b_blip.append(el_list); b_blip.embed = s_relationshipid; b_blip.compressionstate = draw.blipcompressionvalues.print; bf_fill.blip = b_blip; draw.stretch s_stretch = new draw.stretch(); draw.fillrectangle fr_rect = new draw.fillrectangle(); s_stretch.fillrectangle = fr_rect; bf_fill.append(s_stretch); p_pic.blipfill = bf_fill; drawpic.shapeproperties sp_prop = new drawpic.shapeproperties(); draw.transform2d t2d_transform = new draw.transform2d(); draw.offset o_offset = new draw.offset(); o_offset.x = 0; o_offset.y = 0; t2d_transform.offset = o_offset; draw.extents e_extents = new draw.extents(); e_extents.cx = 600075; //63px / 96dpi * 914400 e_extents.cy = 600075; //63px / 96dpi * 914400 t2d_transform.extents = e_extents; sp_prop.transform2d = t2d_transform; draw.presetgeometry pg_geom = new draw.presetgeometry(); draw.adjustvaluelist avl_list = new draw.adjustvaluelist(); pg_geom.adjustvaluelist = avl_list; pg_geom.preset = draw.shapetypevalues.rectangle; sp_prop.append(pg_geom); p_pic.shapeproperties = sp_prop; gd_data.append(p_pic); g_graphic.graphicdata = gd_data; i_inline.graphic = g_graphic; d_drawing.inline = i_inline; //oxe_element.append(d_drawing); //run r_run = new run(d_drawing); wpd_doc.maindocumentpart.document.body.appendchild(new paragraph(new run(d_drawing))); }
(the variable names bad, test program, did not spend time. also, have lines spaced mimic xml format msdn had in example. if use c# 3.0 syntax, would, using visual studio 2005.)
i have found answer. surprisingly, did not find webpage--because google pages down odd keyword searching--that worked: http://blog.stuartwhiteford.com/?p=33. though written collection initializations, able change them fit c# 2.0 standards.
Comments
Post a Comment