如何使用 OpenXml 2.0 将任何文件类型嵌入到 Microsoft Word 中
我花了很多时间试图找到一种使用 OpenXml 2.0 将任何文件嵌入 Microsoft Word 的好方法; Office 文档相当简单,但是其他文件类型(例如 PDF、TXT、GIF、JPG、HTML 等)又如何呢?
在 C# 中,有什么好方法可以让它适用于任何文件类型呢?
I spent a lot of time trying to figure out a good way to embed any file into Microsoft Word using OpenXml 2.0; Office documents are fairly easy but what about other file types such as PDF, TXT, GIF, JPG, HTML, etc....
What is a good way to get this to work for any file type, in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 OpenXml 2.0 将外部对象(PDF、TXT、GIF 等)嵌入到 Microsoft Word 中
(嗯,与 COM 合作)
我从这个网站得到了很多东西,所以在这里我提出并回答了我自己的问题,以便对我很难找到答案的主题做出一些回馈,希望它能够帮助人们。
有几个示例展示了如何使用 OpenXml 2.0 将 Office 文档嵌入到另一个 Office 文档中,但不存在且易于理解的是如何将几乎任何文件嵌入到 Office 文档中。
我从其他人的代码中学到了很多东西,所以这是我的尝试贡献。由于我已经使用 OpenXml 生成文档,并且需要将其他文件嵌入到 Word 中,因此我决定使用 OpenXml 和 COM(Microsoft Office 2007 dll)的协作来实现我的目标。如果您像我一样,“调用 OLE 服务器应用程序来创建 IStorage”对您来说意义不大。
在此示例中,我想展示如何使用 COM 以编程方式获取附加文件的 OLE 二进制数据信息,以及如何在 OpenXml 文档中使用该信息。基本上,我以编程方式查看 OpenXml 2.0 Document Reflector 以获取我需要的信息。
下面的代码分为几个类,但这里是我正在做的事情的概述:
我知道有很多代码,但没有太多解释……希望它很容易理解并能帮助人们解决问题
要求:
• DocumentFormat.OpenXml dll (OpenXml 2.0)
• WindowsBase DLL
• Microsoft.Office.Interop.Word dll(Office 2007 – 版本 12)
• 这是启动一切的主类,打开 WordProcessingDocument 和附加文件的类
• 该类位于OpenXml 帮助程序类,包含将对象嵌入到 OpenXml 文件中的所有逻辑
• 这是此过程中最重要的部分,它使用 Microsoft 的内部 OLE 服务器,创建二进制 DATA 和二进制 EMF 信息对于一个文件。您只需调用 OpenXmlEmbeddedObject 构造函数即可处理所有 get 操作。它将模仿您手动将任何文件拖到 Word 中时所发生的过程;当您执行此操作时,会进行某种转换,将文件转换为 OLE 对象,以便 Microsoft 可以识别该文件。
o 该类最重要的部分是 OleObjectBinaryData 和 OleImageBinaryData 属性;它们包含文件数据和“.emf”图像的 64 位字符串二进制信息。
o 如果您选择不将文件显示为图标,则“.emf”图像数据将创建文件的快照,例如 pdf 文件的第一页,您仍然可以双击打开该快照
o 如果您嵌入图像并选择不将其显示为图标,则 OleObjectBinaryData 和 OleImageBinaryData 属性将相同
Embedding Foreign Objects (PDF, TXT, GIF, etc…) into Microsoft Word using OpenXml 2.0
(Well, in collaboration with COM)
I got a lot from this site, so here I asked and answered my own question in order to give back a little on a topic in which I had difficulty finding answers on, hope it helps people.
There are several examples out there that show how to embed an Office Document into another Office Document using OpenXml 2.0, what’s not out there and easily understandable is how to embed just about any file into and Office Document.
I have learned a lot from other people’s code, so this is my attempt to contribute. Since I am already using OpenXml to generate documents, and I am in need of embedding other files into Word, I have decided use a collaboration of OpenXml and COM (Microsoft Office 2007 dll’s) to achieve my goal. If you are like me, “invoking the OLE server application to create an IStorage” doesn’t mean much to you.
In this example I’d like to show how I use COM to PROGRMATICALLY get the OLE-binary data information of the attached file, and then how I used that information within my OpenXml document. Basically, I am programmatically looking at the OpenXml 2.0 Document Reflector to get the information I need.
My code below is broken down into several classes, but here is an outline of what I am doing:
I know there is a lot of code, and not much explanation… Hopefully it is easy to follow and will help people out
Requirements:
• DocumentFormat.OpenXml dll (OpenXml 2.0)
• WindowsBase dll
• Microsoft.Office.Interop.Word dll (Office 2007 – version 12)
• This the main class that starts everything, opens a WordProcessingDocument and class to have the file attached
• This class in an OpenXml helper class, holds all the logic to embed an object into your OpenXml File
• This is the most important part of this process, it is using Microsoft's internal OLE Server, creates the Binary DATA and Binary EMF information for a file. All you have to here is call the OpenXmlEmbeddedObject constructor and all get’s taken care of. It will mimic the process that goes on when you manually drag any file into Word; there is some kind of conversion that goes on when you do that, turning the file into an OLE object, so that Microsoft can recognize the file.
o The most imporant parts of this class are the OleObjectBinaryData and OleImageBinaryData properties; they contain the 64Bit string binary info for the file data and ‘.emf’ image.
o If you choose to not display the file as an icon, then the ‘.emf’ image data will create a snapshot of the file, like the first page of the pdf file for example, in which you can still double-click to open
o If you are embedding an image and choose not to display it as an Icon, then the OleObjectBinaryData and OleImageBinaryData properties will be the same
似乎坏了,但
应该也能工作。
seems to be broken, but
should also work.
这是一个很好的答案,对我帮助很大,但是用户 bic 提到的潜在错误也存在
第
242 行、
于
第 264 行、
第 289 行和
第 293 行
所有这些都需要是“FullName”而不是“ ToString()”如果代码也应该使用相对路径。希望这对想要使用 D Lyonnais 代码的人有所帮助!
This is a great answer and it helped me a lot, but the potential bug that user bic mentioned also exists
in
at line 242,
in
at line 264,
line 289, and
line 293
All of these need to be "FullName" instead of "ToString()" if the code should work with relative paths as well. Hope this helps anyone who wants to use D Lyonnais's code!
复制您想要的代码编写方式的文档,然后使用 Open XML SDK Tool 2.5 for Microsoft Office 编写代码。该工具反映代码。您可以简单地复制粘贴。
Make a copy of the document how you wanted to get through code and then use Open XML SDK Tool 2.5 for Microsoft Office for writing code. This tool reflects code. which you can simply copy paste.
我的答案在这里 将告诉您如何执行此操作,但不会向您展示 SDK 或特定语言。
My answer here will tell you how to do this, but not show you with the SDK or a specific language.