使用c#和openxml 2.0将图像从流添加到word文档
我一直在尝试一些新的东西,我需要从流中插入图像,但是每次我尝试插入图像时它都会损坏Word文档,我正在使用Office 2010。
我有以下代码将图像添加到Word文档中
private void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string documentName)
{
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 = documentName
}, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill(new A.Blip
{
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 })))))
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U
});
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Run(element));
}
谢谢 Quandary
,但这也没有帮助,所以我将图像保存到文件夹中并使用 Interop 中的 AddPicture 方法
i've been trying something new to me, i need to insert images from a stream but everytime i try to insert an image it corrupts the word document, i am using office 2010.
i have the following code adding the images to the word document
private void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string documentName)
{
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 = documentName
}, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill(new A.Blip
{
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 })))))
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U
});
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Run(element));
}
Thanks Quandary
but that didn't help as well, so i am saving the images to a folder and using the AddPicture Method in Interop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 docx 库?
http://docx.codeplex.com/
像这样:
http://cathalscorner.blogspot.com/2009/04/docx -version-1002-released.html
Why don't you use the docx library ?
http://docx.codeplex.com/
Like this:
http://cathalscorner.blogspot.com/2009/04/docx-version-1002-released.html