I am creating an OpenXML Word Document inside a C# Web Application and am writing a Google Chart Image to it. I'm specifying the dimensions of the image, it seems, correctly. However, when I open the Word Document, the image is the right height, but not the right width. In fact, it is almost a square.
When I click on the image inside of Word and go to Size and Position, the correct dimensions are displayed. In this case, it is 2.08" by 5.04". When I click "OK" after changing nothing, the image stretches to the right size.
If I try to identify what size it originally appears as, it is fairly close to half the defined width, but that may be a coincidence.
The native size of the image is actually 3.13" (h) by 7.55" (w), but that doesn't seem like it should matter.
I followed the following posts pretty closely:
- How to: Insert a picture into a word processing document (Open XML SDK)
- Inserting Image into DocX using OpenXML and setting the size
I'm guessing I'm just missing a setting of some sort, but I don't know which one. Does the Paragraph or the Run need to be sized to the entire width of the page? Does the width of the document itself need to be specified?
public void AddImage(string imageName, byte[] imageData)
{
var img = System.Drawing.Image.FromStream(new MemoryStream(imageData));
MainDocumentPart mainPart = doc.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
MemoryStream imageStream = new MemoryStream(imageData);
imagePart.FeedData(imageStream);
AddImageToBody(mainPart.GetIdOfPart(imagePart), img);
}
private void AddImageToBody(string relationshipId, System.Drawing.Image img)
{
var widthPx = img.Width;
var heightPx = img.Height;
var horzRezDpi = img.HorizontalResolution;
var vertRezDpi = img.VerticalResolution;
const int emusPerInch = 914400;
var widthEmus = (long)(widthPx / horzRezDpi * emusPerInch);
var heightEmus = (long)(heightPx / vertRezDpi * emusPerInch);
var maxWidthEmus = (long)(6.5 * emusPerInch);
if (widthEmus > maxWidthEmus)
{
var ratio = (heightEmus * 1.0m) / widthEmus;
widthEmus = maxWidthEmus;
heightEmus = (long)(widthEmus * ratio);
}
// Define the reference of the image.
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = widthEmus, Cy = heightEmus }, // Cx = 990000L, Cy = 792000L },
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 = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
) { Preset = A.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 should be in a Run.
var p = new Paragraph();
doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}