【问题标题】:Inserting Image into DocX using OpenXML and setting the size使用 OpenXML 将图像插入 DocX 并设置大小
【发布时间】:2011-12-26 08:20:06
【问题描述】:

我正在使用 OpenXML 将图像插入到我的文档中。微软提供的代码可以工作,但使图像更小:

public static void InsertAPicture(string document, string fileName)
        {
            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true))
            {
                MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

                using (FileStream stream = new FileStream(fileName, FileMode.Open))
                {
                    imagePart.FeedData(stream);
                }

                AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
            }
        }
        private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
        {
            // Define the reference of the image.
            var element =
                 new Drawing(
                     new DW.Inline(
                         new DW.Extent() { 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.
            wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
        }

我需要将图像设为原始大小。我怎样才能做到这一点? (我已经用谷歌搜索了如何在此过程之外执行此操作,但这不是我想要的。我必须假设给定代码中有某种大小属性)。

编辑:更新代码(仍然无效)

public static void InsertAPicture(string document, string fileName)
{
    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true))
    {
        MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }

        AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart), fileName);
    }
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string fileName)
{

    var img = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute));
    var widthPx = img.PixelWidth;
    var heightPx = img.PixelHeight;
    var horzRezDpi = img.DpiX;
    var vertRezDpi = img.DpiY;
    const int emusPerInch = 914400;
    const int emusPerCm = 360000;
    var maxWidthCm = 16.51;
    var widthEmus = (long)(widthPx / horzRezDpi * emusPerInch);
    var heightEmus = (long)(heightPx / vertRezDpi * emusPerInch);
    var maxWidthEmus = (long)(maxWidthCm * emusPerCm);
    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 = 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 = widthEmus, Cy = heightEmus }),
                                 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.
    wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}

【问题讨论】:

    标签: c# image openxml docx


    【解决方案1】:

    EMU (English Metric Unit -- read this for a good explanation) 中的大小在 Extents(Cx 和 Cy)中设置。 为了将图片放入 DocX,我通常会这样做:

    1. 获取图片的尺寸和分辨率
    2. 以 EMU 为单位计算图像的宽度:wEmu = imgWidthPixels / imgHorizo​​ntalDpi * emuPerInch
    3. 以 EMU 为单位计算图像的高度:hEmu = imgHeightPixels / imgVerticalDpi * emuPerInch
    4. 计算 EMU 中的最大页面宽度(我发现如果图像太宽,它不会显示)
    5. 如果图像在 EMU 中的宽度大于最大页面宽度,我会缩放图像的宽度和高度,使图像的宽度等于页面的宽度(当我说页面时,我指的是“可用”页面,即减去边距):

      var ratio = hEmu / wEmu;
      wEmu = maxPageWidthEmu;
      hEmu = wEmu * ratio;

    6. 然后我使用宽度作为 Cx 的值和高度作为 Cy 的值,都在DW.Extent 和A.Extents 中(A.Transform2D 的PIC.ShapeProperties)。

    请注意,emuPerInch 的值为 914400。
    我有这个正在运行(在服务中),但我现在没有代码。

    更新

    这是我使用的代码:

    var img = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute));
    var widthPx = img.PixelWidth;
    var heightPx = img.PixelHeight;
    var horzRezDpi = img.DpiX;
    var vertRezDpi = img.DpiY;
    const int emusPerInch = 914400;
    const int emusPerCm = 360000;
    var widthEmus = (long)(widthPx / horzRezDpi * emusPerInch);
    var heightEmus = (long)(heightPx / vertRezDpi * emusPerInch);
    var maxWidthEmus = (long)(maxWidthCm * emusPerCm);
    if (widthEmus > maxWidthEmus) {
      var ratio = (heightEmus * 1.0m) / widthEmus;
      widthEmus = maxWidthEmus;
      heightEmus = (long)(widthEmus * ratio);
    }
    

    在我的例子中,我的页面尺寸以厘米为单位,因此您在上面看到的 emusPerCm。

    更新 2(回答 @andw)

    要在尽可能短的时间内锁定文件,请使用FileStream 打开它并将生成的流传递给BitmapImage 的StreamSource 属性:

    var img = new BitmapImage();
    using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
        img.BeginInit();
        img.StreamSource = fs;
        img.EndInit();
    }
    // The file is now unlocked
    var widthPx = img.PixelWidth;
    ...
    

    【讨论】:

    • 图像 dpi 的宽度和高度是否恒定?这个怎么计算?
    • 我认为它们不是恒定的,但我还没有找到它们不是恒定的。如果您使用BitmapImage 类(System.Windows.Media.Imaging 命名空间)打开图像,则可以使用以下属性:PixelWidth、PixelHeight、DpiX、DpiY。
    • 我用新代码更新了我的帖子。我认为我的代码是正确的,但图像大小没有改变。
    • 您没有设置 DW.Extent。像这样: (...) new DW.Extent() { Cx = widthEmus, Cy = heightEmus }, (...)
    • @Arjan “魔法”。在我们的例子中是边距之间的可用宽度。我们由该服务生成的所有文档都有一个固定的左右边距,减去 A4 纸的宽度(纵向模式)大约为 15 厘米(我们的maxWidthCm)。您将其设置为对您的情况有用的值。
    【解决方案2】:

    您可以通过这种方式为图像赋予其原始大小:

    首先你得到文件的宽度和高度:

    int iWidth = 0;
    int iHeight = 0;
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("yourFilePath"))
    {
         iWidth = bmp.Width;
         iHeight = bmp.Height;
    }
    

    然后以这种方式将像素转换为EMU:

    iWidth = (int)Math.Round((decimal)iWidth * 9525);
    iHeight = (int)Math.Round((decimal)iHeight * 9525);
    

    最后在打开文件时给出值,我的意思是在代码的这一行中:

    new DW.Extent() { Cx = 990000L, Cy = 792000L },
    

    将 Cx 和 Cy 替换为您的计算值,如下所示:

    new DW.Extent() { Cx = iWidth, Cy = iHeight },
    

    请注意,您的代码中有两行必须提供计算值。

    然后你的图片是原始大小的,希望这对某人有帮助。

    【讨论】:

    • 请注意,您的代码中有两行必须给出计算值...这是关键部分
    【解决方案3】:

    此代码对我有用。

    来源:http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx

        public static void Do()
        {
            string filename = @"c:\temp\m.docx";
            byte[] reportData = GetWordReport();
           // File.WriteAllBytes(filename, reportData);
            //MessageBox.Show("File " + filename + " created");
        }
    
        private static byte[] GetWordReport()
        {
           // using (MemoryStream stream = new MemoryStream())
           // {
                //var template = GetTemplateData();
                //stream.Write(template, 0, template.Length);
                using (WordprocessingDocument docx = WordprocessingDocument.Open(@"c:\temp\m.docx", true))
                {
                    // Some changes on docx
                    docx.MainDocumentPart.Document = GenerateMainDocumentPart(6,4);
    
                    var imagePart = docx.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rIdImagePart1");
                    GenerateImagePart(imagePart);
                }
              //  stream.Seek(0, SeekOrigin.Begin);
               // return stream.ToArray();
           // }
            return null;
        }
    
        private static byte[] GetTemplateData()
        {
            using (MemoryStream targetStream = new MemoryStream())
            using (BinaryReader sourceReader = new BinaryReader(File.Open(@"c:\temp\m_2.docx", FileMode.Open)))
            {
                byte[] buffer = new byte[4096];
    
                int num = 0;
                do
                {
                    num = sourceReader.Read(buffer, 0, 4096);
                    if (num > 0)
                        targetStream.Write(buffer, 0, num);
                }
                while (num > 0);
                targetStream.Seek(0, SeekOrigin.Begin);
                return targetStream.ToArray();
            }
        }
    
        private static void GenerateImagePart(OpenXmlPart part)
        {
            using (Stream imageStream = File.Open(@"c:\temp\image002.jpg", FileMode.Open))
            {
                part.FeedData(imageStream);
            }
        }
    
        private static Document GenerateMainDocumentPart(int cx,int cy)
        {
            long LCX = cx*261257L;
            long LCY = cy*261257L;
    
    
    
    
            var element =
                new Document(
                    new Body(
                        new Paragraph(
                            new Run(
                                new RunProperties(
                                    new NoProof()),
                                new Drawing(
                                    new wp.Inline(
                                        new wp.Extent() { Cx = LCX, Cy = LCY },
                                        new wp.EffectExtent() { LeftEdge = 0L, TopEdge = 19050L, RightEdge = 0L, BottomEdge = 0L },
                                        new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = "Forest Flowers.jpg" },
                                        new wp.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 = "Forest Flowers.jpg" },
                                                        new pic.NonVisualPictureDrawingProperties()),
                                                    new pic.BlipFill(
                                                        new a.Blip() { Embed = "rIdImagePart1", 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 = LCX, Cy = LCY }),
                                                        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 }))
                        ) { RsidParagraphAddition = "00A2180E", RsidRunAdditionDefault = "00EC4DA7" },
                        new SectionProperties(
                            new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
                            new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U },
                            new Columns() { Space = ((UInt32Value)425U).ToString() },
                            new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 }
                        ) { RsidR = "00A2180E", RsidSect = "00A2180E" }));
            return element;
        }
    

    【讨论】:

    • 想在您找到这个的地方添加源代码?因为这不是你自己的“发现”。
    【解决方案4】:

    由于这是搜索图像和 OpenXml 时的第一个热门,而且世界已经变得更进一步,我想分享@ssarabando 的解决方案,用于计算适用于 System.Drawing 不适用的 .net 核心的 EMU可用,使用 ImageSharp(截至 2018 年 11 月仍为测试版):

    const int emusPerInch = 914400;
    const int emusPerCm = 360000;
    
    long widthEmus;
    long heightEmus;
    Image<Rgba32> img = Image.Load(sourceStream, new PngDecoder());
    
    switch (img.MetaData.ResolutionUnits)
    {
        case PixelResolutionUnit.PixelsPerCentimeter :
            widthEmus = (long)(img.Width / img.MetaData.HorizontalResolution * emusPerCm);
            heightEmus = (long)(img.Height / img.MetaData.VerticalResolution * emusPerCm);
            break;
        case PixelResolutionUnit.PixelsPerInch:
            widthEmus = (long)(img.Width / img.MetaData.HorizontalResolution * emusPerInch);
            heightEmus = (long)(img.Height / img.MetaData.VerticalResolution * emusPerInch);
            break;
        case PixelResolutionUnit.PixelsPerMeter:
            widthEmus = (long)(img.Width / img.MetaData.HorizontalResolution * emusPerCm * 100);
            heightEmus = (long)(img.Height / img.MetaData.VerticalResolution * emusPerCm * 100);
            break;
        default:
            widthEmus = 2000000;
            heightEmus = 2000000;
            break;
    }
    

    希望它能节省一些人的时间。

    【讨论】:

    • System.Drawing 在 .Net Core 中可用。不可用的是BitmapImage,但您可以使用Bitmap。请参阅@JCO9 的回答。
    【解决方案5】:

    这在 Net Core 中对我有用并实现了 here。

    long iWidth = 0;
    long iHeight = 0;
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fullPathToImageFile))
    {
        iWidth = bmp.Width;
        iHeight = bmp.Height;
    }            
    
    iWidth = (long)Math.Round((decimal)iWidth * 9525);
    iHeight = (long)Math.Round((decimal)iHeight * 9525);
    
    double maxWidthCm = 17.4; // Our current margins gives us 17.4cm of space
    const int emusPerCm = 360000;
    long maxWidthEmus = (long)(maxWidthCm * emusPerCm);
    if (iWidth > maxWidthEmus) {
        var ratio = (iHeight * 1.0m) / iWidth;
        iWidth = maxWidthEmus;
        iHeight = (long)(iWidth * ratio);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2023-02-02
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多