wpf - C# OpenXML image in center -
i'm trying generate docx document image in center of document, have tried several things nothing has worked out. image showing in top left corner. function addimagetobody ms website (http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx). i've tried use horizontalposition class (http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx) has not worked me.
add image , call function:
maindocumentpart mainpart = document.maindocumentpart; imagepart imagepart = mainpart.addimagepart(imageparttype.png); using (filestream stream = new filestream(@"c:...\logo.png", filemode.open)) { imagepart.feeddata(stream); } addimagetobody(document, mainpart.getidofpart(imagepart));
and function:
private static void addimagetobody(wordprocessingdocument worddoc, string relationshipid) { int defx = 854; int defy = 350; int size = 3000; // define reference of image. var element = new documentformat.openxml.wordprocessing.drawing( new dw.inline( new dw.extent() { cx = defx * size, cy = defy * size }, new dw.effectextent() { leftedge = 0l, topedge = 0l, rightedge = 0l, bottomedge = 0l }, new dw.docproperties() { id = (uint32value)1u, name = "picture 1" }, new dw.nonvisualgraphicframedrawingproperties( new a.graphicframelocks() { nochangeaspect = true }), new a.graphic( new a.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 a.blip( new a.blipextensionlist( new a.blipextension() { uri = "{28a0092b-c50c-407e-a947-70e740481c1c}" }) ) { embed = relationshipid, compressionstate = a.blipcompressionvalues.print }, new a.stretch( new a.fillrectangle() { })), new pic.shapeproperties( new a.transform2d( new a.offset() { x = 1000l, y = 500l, }, new a.extents() { cx = defx * size, cy = defy * size }), new a.presetgeometry( new a.adjustvaluelist() ) { preset = a.shapetypevalues.rectangle })) ) { uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) ) { distancefromtop = (uint32value)999999, distancefrombottom = (uint32value)10000, distancefromleft = (uint32value)10000, distancefromright = (uint32value)10000, editid = "50d07946" }); // append reference body, element should in run. worddoc.maindocumentpart.document.body.appendchild(new documentformat.openxml.wordprocessing.paragraph(new documentformat.openxml.wordprocessing.run(element))); }
you need center paragraph holding image. can using paragraph properties (last line of code):
worddoc.maindocumentpart.document.body.appendchild( new documentformat.openxml.wordprocessing.paragraph(new documentformat.openxml.wordprocessing.run(element)) { paragraphproperties = new documentformat.openxml.wordprocessing.paragraphproperties() { justification = new documentformat.openxml.wordprocessing.justification() { val = documentformat.openxml.wordprocessing.justificationvalues.center } } });
Comments
Post a Comment