当打开在c#中创建的OpenXML Word文档时,图像大小不适用

本文关键字:文档 图像 不适用 Word OpenXML 创建 | 更新日期: 2023-09-27 17:51:15

我正在c# Web应用程序中创建一个OpenXML Word文档,并为其编写一个谷歌图表图像。我指定了图像的尺寸,看起来是正确的。然而,当我打开Word文档,图像是正确的高度,但不是正确的宽度。事实上,它几乎是一个正方形。

当我点击Word里面的图片,选择大小和位置,正确的尺寸就会显示出来。在本例中,它是2.08" * 5.04"。当我在没有更改任何内容的情况下点击"确定"时,图像会拉伸到合适的大小。

如果我试着确定它最初出现的大小,它相当接近定义宽度的一半,但这可能是巧合。

图像的原始大小实际上是3.13" (h) * 7.55" (w),但这似乎无关紧要。

我密切关注以下帖子:

  • 如何:在文字处理文档中插入图片(打开XMLSDK)
  • 使用OpenXML将图像插入DocX并设置大小

我猜我只是错过了某种设置,但我不知道是哪一个。段落或运行是否需要调整到整个页面的宽度?需要指定文档本身的宽度吗?

    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)));
    }

当打开在c#中创建的OpenXML Word文档时,图像大小不适用

这是一个相当古老的问题,但我认为无论如何都值得回答,因为它似乎经常出现。

我也使用了上面的示例,它工作得很好,除了cx和cy是硬编码的并且会扭曲图像。

我创建了一个类来处理我的图像,然后我用它来为元素添加细节,然后我可以使用它来附加到文档或附加到段落等。

EDIT:我在ImageDetails中添加了一个ID字段,因为当我向文档添加几个图像时,我需要更新ID。通过创建集合和CounterState类,我能够跟踪Id并确保它是惟一的。如果有人想看这个实现,请在评论中告诉我,我会把它包含在这个答案中。

所以…这个类对我的解决方案的要求有点特殊,但你需要什么就拿什么。

public class ImageDetails
{
    // required to set the image id which needs to be unique in the document
    // which is important if you're adding several images
    public UInt32 Id { get; set; } 
    public ImageDetails(string fileName)
    {
        this.ImageFile = fileName;
    }
    #region Properties
    private decimal _widthInCm;
    /// <summary>
    /// 
    /// </summary>
    /// <summary>
    /// Sets the width in cm (note this is a user set to calculate cx. Leave blank for the class to calculate the cx based on pixels)
    /// </summary>
    public decimal WidthInCm
    {
        set { _widthInCm = value; }
    }
    private decimal _heightInCm;
    /// <summary>
    /// Sets the height in cm (note this is a user set to calculate cy. Leave blank for the class to calculate the cy based on pixels)
    /// </summary>
    public decimal HeightInCm
    {
        set { _heightInCm = value; }
    }

    const int emusPerInch = 914400;
    const int emusPerCm = 360000;
    /// <summary>
    /// Returns the width in EMUS (English Metric Units)
    /// </summary>
    public long cx
    {
        get
        {
            if (_widthInCm > 0)
                return (long)Math.Round(_widthInCm * emusPerCm);
            else if (_image.Width > 0)
                return (long)Math.Round((_image.Width / _image.HorizontalResolution) * emusPerInch);
            else
            {
                throw new InvalidDataException("WidthInCm/WidthInPx has not been set");
            }
        }
    }
    /// <summary>
    /// Returns the height in EMUS (English Metric Units)
    /// </summary>
    public long cy
    {
        get
        {
            if (_heightInCm > 0)
                return (long)decimal.Round(_heightInCm * emusPerCm);
            else if (_image.Height > 0)
                return (long)Math.Round((_image.Height / _image.VerticalResolution) * emusPerInch);
            else
            {
                throw new InvalidDataException("HeightInCm/HeightInPx has not been set");
            }
        }
    }
    public int WidthInPx
    {
        get { return _image.Width; }
    }

    public int HeightInPx
    {
        get { return _image.Height; }
    }

    private string _imageFileName;
    private Image _image;
    /// <summary>
    /// Sets the Image file name and loads the Image object for later use
    /// </summary>
    public string ImageFile
    {
        get { return _imageFileName; }
        set
        {
            _imageFileName = value;
            // Limiting the time the image file is open in case others require it
            using (var fs = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                _image = Image.FromStream(fs);
            }
        }
    }
    /// <summary>
    /// Allows direct read/write access to the internal Image Object
    /// </summary>
    public Image ImageObject
    {
        get { return _image; }
        set { _image = value; }
    }
    #endregion

    /// <summary>
    /// This method resizes the image and replaces the internal Drawing.Image object with the new size
    /// </summary>
    /// <param name="targetWidth">New target width in px. The aspect ratio is maintained</param>
    public  void ResizeImage(int targetWidth)
    {
        if (_image == null)
            throw new InvalidOperationException("The Image has not been referenced. Add an image first using .ImageFile or .ImageObject");
        double percent = (double)_image.Width / targetWidth;
        int destWidth = (int)(_image.Width / percent);
        int destHeight = (int)(_image.Height / percent);
        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        try
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawImage(_image, 0, 0, destWidth, destHeight);
        }
        finally
        {
            g.Dispose();
        }
        _image = (Image)b;
    }
}

如果您需要预先调整图像的大小,只需调用:

ImageDetails image = new ImageDetails("image.png");
image.Resize(45); // set new image to a width of 45px. Height is automatically adjusted

然后我在image方法中使用了这个:

     private Drawing CreateImageElement(WordprocessingDocument wordDoc, string relationshipId, ImageDetails image)
    {
        // Define the reference of the image.
        return
             new Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = image.cx, Cy = image.cy},
                     new DW.EffectExtent()
                     {
                         LeftEdge = 0L,
                         TopEdge = 0L,
                         RightEdge = 0L,
                         BottomEdge = 0L
                     },
                     new DW.DocProperties()
                     {
                         Id = image.Id,
                         Name = FileManipulation.GetFileName(image.ImageFile) 
                     },
                     new DW.NonVisualGraphicFrameDrawingProperties(
                         new A.GraphicFrameLocks() { NoChangeAspect = true }),
                     new A.Graphic(
                         new A.GraphicData(
                             new PIC.Picture(
                                 new PIC.NonVisualPictureProperties(
                                     new PIC.NonVisualDrawingProperties()
                                     {
                                         Id = image.Id,
                                         Name = Path.GetFileName(image.ImageFile)
                                     },
                                     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 = image.cx, Cy = image.cy}),
                                     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"
                 });

    }

根据您的代码,您必须为Extent属性设置CxCy的值。

new DW.Extent() { Cx = widthEmus, Cy = heightEmus }

第一个是Inline,第二个是Transform2D

哎呀。有两个地方设置了大小。我错过了第二个