【发布时间】:2015-05-29 14:53:16
【问题描述】:
我正在一个 C# Web 应用程序中创建一个 OpenXML Word 文档,并向它写入一个 Google 图表图像。我似乎正确地指定了图像的尺寸。但是,当我打开 Word 文档时,图像是正确的高度,而不是正确的宽度。事实上,它几乎是一个正方形。
当我在 Word 中单击图像并转到大小和位置时,会显示正确的尺寸。在这种情况下,它是 2.08" x 5.04"。当我什么都不做更改后单击“确定”时,图像会拉伸到正确的大小。
如果我尝试确定它最初显示的尺寸,它相当接近定义宽度的一半,但这可能是巧合。
图像的原始尺寸实际上是 3.13" (h) x 7.55" (w),但这似乎并不重要。
我非常关注以下帖子:
- How to: Insert a picture into a word processing document (Open XML SDK)
- Inserting Image into DocX using OpenXML and setting the size
我猜我只是缺少某种设置,但我不知道是哪一个。段落或运行是否需要调整到页面的整个宽度?是否需要指定文档本身的宽度?
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)));
}
【问题讨论】:
-
请把代码贴在这里
-
@bond 这很像我包含的链接,但你是对的。最好把代码展示出来。
-
@Bond 我错过了第二个设置图片大小的地方。