从位图图片构造 OpenXmlElement 以附加到 运行 实例

Constructing an OpenXmlElement from bitmap picture to append into a Run instance

我有以下代码:

string replaceValueString = parameterValue.ToString();
Run replaceRun = new Run();
replaceRun.Append(new Text(replaceValueString));
contentControl.InsertAfterSelf(replaceRun);

我需要添加类似的逻辑,但我需要添加图片(或其他类型的图像实例)而不是文本,如下所示:

replaceRun.Append(new Picture(data));

我有一个Bitmapclass,我也可以通过byte[]或者图片的Steam。我唯一的问题是,我几乎没有发现如何构造这张继承自 OpenXmlCompositeElement 的图片 class 的示例。

有人可以为我提供一些示例和指南,以便将 Bitmap/data[]/Steam 图像转换为 DocumentFormat.OpenXml.Wordprocessing.Picture(或任何其他 OpenXmlElement)吗?

到目前为止我只找到了这个例子: https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document

...它创建了一个绘图 class 而不是图片,它使用了一些我无权访问的 DW SDK。

您提供的示例乍一看可能令人望而生畏,但如果您完成它,它会非常有用...

it uses some DW SDK, which I don't have access to.

DW 只是一些您毫无疑问已经可以访问的名称空间的别名。确保在 class 文件的顶部包含以下内容:

using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;

考虑到这一点。让我们从您的 Bitmap 开始吧。首先,您想用它来创建一个 ImagePart。下面的代码可能看起来像是直接向主文档添加图像,但它并不像那样工作。别担心,我们稍后会将其移至您的运行。

MainDocumentPart mainDocumentPart = wordDoc.MainDocumentPart;
ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Bmp);
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
    stream.Position = 0;
    imagePart.FeedData(stream);
}

我们需要获取图像部分的 ID,以便在创建 Drawing 对象时使用它。

string imagePartId = mainDocumentPart.GetIdOfPart(imagePart);

接下来,我们需要使用您不喜欢的代码 from here

让我们把目前所有的东西都放在一个函数中,该函数将从 Bitmap.

创建 Drawing
static Drawing ConvertBitmapToDrawing(WordprocessingDocument wordDoc, System.Drawing.Bitmap image)
{
    MainDocumentPart mainDocumentPart = wordDoc.MainDocumentPart;
    ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Bmp);
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        stream.Position = 0;
        imagePart.FeedData(stream);
    }

    string imagePartId = mainDocumentPart.GetIdOfPart(imagePart);

    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 = imagePartId, 
                                         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" });

    return element;
}

有了这个功能,你终于可以做这样的事情了:

Drawing drawing = ConvertBitmapToDrawing(wordProcessingDocument, myBitmap);
Run newRun = new Run(drawing);
contentControl.InsertAfterSelf(newRun);