Search code examples
c#.netms-wordopenxml

In docx created with OpenXML, can Aspect Ratio be preserved for image added with NoChangeAspect attribut?


I'm creating docx document and adding images to it.

I have done it by https://learn.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document?redirectedfrom=MSDN

I have seen that there is an attribute NoChangeAspect, used like

new DW.NonVisualGraphicFrameDrawingProperties(
     new A.GraphicFrameLocks() { NoChangeAspect = true }),

but the image take new aspect ratios, set by

new A.Extents() { Cx = 990000L, Cy = 792000L }),

I have seen that aspect ratios can be preserved by getting dimensions from System.Drawing.Bitmap created from stream, but can it be done without importing System.Drawing, just with NoChangeAspect?


Solution

  • For the <wp:inline> element, the <wp:extent> (i.e. new A.Extents() { Cx = 990000L, Cy = 792000L }),)is a required child element and the attributes of the extent element do not provide a way to trigger Word to read the extents from the blip (jpeg) file itself. Therefore, you will have to provide the extents in the markup by first getting them from the jpeg using System.Drawing or some other library like SixLabors/ImageSharp.