使用 OpenXML 添加图像时无法打开 Word 文档



将位图添加到 Docx 文件后,我发现当我尝试在 Word 中打开文档时Microsoft出现以下错误。

Word 在尝试打开文件时遇到错误。

我的代码如下:

using (WordprocessingDocument doc = WordprocessingDocument.Open(saveAsPath, true))
{
Bitmap bm = GeneratCadImage();

string fileName = @"C:test.bmp";
var codeBitmap = new Bitmap(fileName);
Image image = (Image)codeBitmap;    
Drawing drawing = ConvertBitmapToDrawing(doc, codeBitmap);
Run newRun = new Run(drawing);
//I've tried placing it in several places
OpenXmlElement contentControl = doc.MainDocumentPart.Document.Body.Descendants().Where(x => x != null).Last() ;
contentControl.InsertAfterSelf(newRun);    
}

关于将图像放置在哪个 ContentControl,我已经尝试了第一个,最后一个等,看看它的位置是否与它有关。

一个线索是,尽管图像在 10Mb 的量级,但文件大小只大了 7kb。

下面是一个相当冗长的方法,但其中大部分是从这里复制粘贴的:MSDN

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

编辑:

@pfx:我无法按原样运行您的代码,我收到以下错误:

无法插入 OpenXmlElement "newChild",因为它是 树

但是,我能够像这样运行它:

Drawing drawing = ConvertBitmapToDrawing(doc, codeBitmap);
Run newRun = new Run(drawing);
Paragraph para = new();
para.Append(newRun);
doc.MainDocumentPart.Document.Body.AppendChild(para);

但这产生了相同的结果,我无法打开word文档。

包含DrawingRun需要在Paragraph内,可以添加为Body的子项。

using (WordprocessingDocument doc = WordprocessingDocument.Open(saveAsPath, true))
{
Bitmap bitmap = Bitmap.FromFile(@"C:test.bmp") as Bitmap;
DocumentFormat.OpenXml.Wordprocessing.Drawing drawing = ConvertBitmapToDrawing(doc, bitmap);
doc.MainDocumentPart.Document.Body.AppendChild(
new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
new DocumentFormat.OpenXml.Wordprocessing.Run(drawing)
));
}

最新更新