插入图像损坏打开 XML SDK 生成的 Word 文件
Inserting image corrupts Open XML SDK generated Word file
我已尝试调整 https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document 中的代码以将图像插入到我的 .Net Core MVC 应用程序中的 table 单元格中。但是,插入图像只会损坏 word 文件。
我注意到当我提取损坏的 docx 文件时,“media”文件夹位于“word”文件夹之外。但是,当我手动将图像添加到 docx 文件时,“media”文件夹出现在“word”文件夹中。
...code here...
ImagePart imagePart = wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(@$"{_env.WebRootPath}\images\mono.jpg", FileMode.Open))
{
imagePart.FeedData(stream);
}
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)11U, 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)10U, 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 = wordDoc.MainDocumentPart.GetIdOfPart(imagePart), 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 = "https://schemas.openxmlformats.org/drawingml/2006/picture" })) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" });
Table table0 = new Table();
TableProperties props0 = new TableProperties(...code here...);
table0.AppendChild<TableProperties>(props0);
var tr0 = new TableRow();
var tc0 = new TableCell();
var pp0 = new ParagraphProperties(new SpacingBetweenLines() { After = "0" }, new Justification() { Val = JustificationValues.Center });
var rn0 = new Run(element);
var pg0 = new Paragraph(pp0, rn0);
tc0.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1792" })), new HorizontalMerge { Val = MergedCellValues.Restart });
tc0.Append(pg0);
tr0.Append(tc0);
table0.Append(tr0);
body.Append(table0);
doc.Append(body);
...code here...
我想通了。 MSDN 文档是罪魁祸首。
MSDN 文档中的这一行:
) { Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })
无效,因为 https
应该是 http
。
使文档打开没有问题的更改。
我已尝试调整 https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document 中的代码以将图像插入到我的 .Net Core MVC 应用程序中的 table 单元格中。但是,插入图像只会损坏 word 文件。 我注意到当我提取损坏的 docx 文件时,“media”文件夹位于“word”文件夹之外。但是,当我手动将图像添加到 docx 文件时,“media”文件夹出现在“word”文件夹中。
...code here...
ImagePart imagePart = wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(@$"{_env.WebRootPath}\images\mono.jpg", FileMode.Open))
{
imagePart.FeedData(stream);
}
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)11U, 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)10U, 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 = wordDoc.MainDocumentPart.GetIdOfPart(imagePart), 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 = "https://schemas.openxmlformats.org/drawingml/2006/picture" })) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" });
Table table0 = new Table();
TableProperties props0 = new TableProperties(...code here...);
table0.AppendChild<TableProperties>(props0);
var tr0 = new TableRow();
var tc0 = new TableCell();
var pp0 = new ParagraphProperties(new SpacingBetweenLines() { After = "0" }, new Justification() { Val = JustificationValues.Center });
var rn0 = new Run(element);
var pg0 = new Paragraph(pp0, rn0);
tc0.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1792" })), new HorizontalMerge { Val = MergedCellValues.Restart });
tc0.Append(pg0);
tr0.Append(tc0);
table0.Append(tr0);
body.Append(table0);
doc.Append(body);
...code here...
我想通了。 MSDN 文档是罪魁祸首。
MSDN 文档中的这一行:
) { Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })
无效,因为 https
应该是 http
。
使文档打开没有问题的更改。