DocumentFormat.OpenXml 将图像添加到 Word 文档
我正在使用 openXml SDK 创建一个简单的 Word 文档。 到目前为止它正在发挥作用。 现在如何将文件系统中的图像添加到此文档中?我不在乎它在文档中的位置,只是它就在那里。 谢谢! 这是我到目前为止所拥有的。
string fileName = "proposal"+dealerId +Guid.NewGuid().ToString()+".doc";
string filePath = @"C:\DWSApplicationFiles\Word\" + fileName;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document, true))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
//create the body
Body body = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
DocumentFormat.OpenXml.Wordprocessing.Run runParagraph = new DocumentFormat.OpenXml.Wordprocessing.Run();
DocumentFormat.OpenXml.Wordprocessing.Text text_paragraph = new DocumentFormat.OpenXml.Wordprocessing.Text("This is a test");
runParagraph.Append(text_paragraph);
p.Append(runParagraph);
body.Append(p);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
I am creating a simple word doc, using the openXml SDK.
It is working so far.
Now how can I add an image from my file system to this doc? I don't care where it is in the doc just so it is there.
Thanks!
Here is what I have so far.
string fileName = "proposal"+dealerId +Guid.NewGuid().ToString()+".doc";
string filePath = @"C:\DWSApplicationFiles\Word\" + fileName;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document, true))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
//create the body
Body body = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
DocumentFormat.OpenXml.Wordprocessing.Run runParagraph = new DocumentFormat.OpenXml.Wordprocessing.Run();
DocumentFormat.OpenXml.Wordprocessing.Text text_paragraph = new DocumentFormat.OpenXml.Wordprocessing.Text("This is a test");
runParagraph.Append(text_paragraph);
p.Append(runParagraph);
body.Append(p);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种比上面发布的 msdn 页面中描述的方法更简单的方法,此代码是用 C++/CLI 编写的,但当然您可以用 C# 编写等效的代码
Here is a method that can be simpler than the one described in the msdn page posted above, this code is in C++/CLI but of course you can write the equivalent in C#
此代码对我有用: http://msdn.microsoft.com/en-us /library/bb497430.aspx
您的代码将图像添加到您的 docx 包中,但为了在文档中看到它,您必须在 document.xml 中声明它,即将其链接到您的物理图像。这就是为什么您必须编写 msdn 链接中列出的长函数的原因。
我的问题是如何给图片添加效果(编辑、裁剪、背景去除)。
如果您知道如何执行此操作,我将不胜感激您的帮助:)
This code worked for me: http://msdn.microsoft.com/en-us/library/bb497430.aspx
Your code adds image to your docx package, but in order to see it in the document you have to declare it in your document.xml i.e. link it to your physical image. That's why you have to write that long function listed in the msdn link.
My problem is how to add effects to pictures (editing, croping, background removal).
If you know how to do this I'd appreciate your help :)
如何:使用 Open XML API 将图像部件添加到 Office Open XML 包
http://msdn.microsoft.com/en-us/library/bb497430(v=office.12).aspx
How to: Add an Image Part to an Office Open XML Package by Using the Open XML API
http://msdn.microsoft.com/en-us/library/bb497430(v=office.12).aspx