将多个 DOCX 文件附加在一起
我需要以编程方式使用 C# 将多个预先存在的 docx 文件附加到单个长 docx 文件中 - 包括特殊标记,如项目符号和图像。 页眉和页脚信息将被删除,因此这些信息不会造成任何问题。
我可以找到大量有关使用 .NET Framework 3 操作单个 docx 文件的信息,但没有关于如何合并文件的简单或明显的信息。 还有一个第三方程序(Acronis.Words)可以做到这一点,但价格昂贵得令人望而却步。
更新:
有人建议通过 Word 进行自动化,但我的代码将在 IIS Web 服务器上的 ASP.NET 上运行,因此使用 Word 对我来说不是一个选择。 很抱歉一开始没有提到这一点。
I need to use C# programatically to append several preexisting docx
files into a single, long docx
file - including special markups like bullets and images. Header and footer information will be stripped out, so those won't be around to cause any problems.
I can find plenty of information about manipulating an individual docx
file with .NET Framework 3, but nothing easy or obvious about how you would merge files. There is also a third-party program (Acronis.Words) that will do it, but it is prohibitively expensive.
Update:
Automating through Word has been suggested, but my code is going to be running on ASP.NET on an IIS web server, so going out to Word is not an option for me. Sorry for not mentioning that in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尽管提交了所有好的建议和解决方案,我还是开发了一个替代方案。 我认为您应该完全避免在服务器应用程序中使用 Word。 所以我使用了 OpenXML,但它不适用于 AltChunk。 我将文本添加到原始正文中,我收到一个字节 [] 列表而不是文件名列表,但您可以根据需要轻松更改代码。
我希望这可以帮助你。
In spite of all good suggestions and solutions submitted, I developed an alternative. In my opinion you should avoid using Word in server applications entirely. So I worked with OpenXML, but it did not work with AltChunk. I added text to original body, I receive a List of byte[] instead a List of file names but you can easily change the code to your needs.
I hope this helps you.
您不需要使用自动化。 DOCX 文件基于 OpenXML 格式。 它们只是 zip 文件,里面有一堆 XML 和二进制部分(想想文件)。 您可以使用打包 API(WindowsBase.dll 中的 System.IO.Packaging)打开它们,并使用框架中的任何 XML 类操作它们。
查看 OpenXMLDeveloper.org 了解详细信息。
You don't need to use automation. DOCX files are based on the OpenXML Formats. They are just zip files with a bunch of XML and binary parts (think files) inside. You can open them with the Packaging API (System.IO.Packaging in WindowsBase.dll) and manipulate them with any of the XML classes in the Framework.
Check out OpenXMLDeveloper.org for details.
这对于最初的问题来说已经很晚了,并且发生了很大的变化,但我想我会分享我编写合并逻辑的方式。 这利用了 Open XML Power Tools
目前,这在我们的应用程序中运行得很好。 我对代码做了一些更改,因为我的要求是每个文档都需要先处理。 因此传入的是一个 DTO 对象,其中包含模板字节数组和需要替换的各种值。 这是我的代码当前的样子。 这使得代码更进一步。
This is a very late to the original question and quite a bit has change but thought I would share the way I have written my merge logic. This makes use of the Open XML Power Tools
Currently this is working very well in our application. I have changed the code a little because my requirements is that each document that needs to be processed first. So what gets passed in is a DTO object with the template byte array and the various values that need to be replaced. Here is how my code currently looks. Which takes the code a little bit further.
我不久前编写了一个小测试应用程序来执行此操作。 我的测试应用程序使用的是 Word 2003 文档 (.doc),而不是 .docx,但我想过程是相同的 - 我认为您需要更改的就是使用主互操作程序集的较新版本。 使用新的 C# 4.0 功能,这段代码看起来会整洁很多...
I wrote a little test app a while ago to do this. My test app worked with Word 2003 documents (.doc) not .docx, but I imagine the process is the same - I should think all you'd have to change is to use a newer version of the Primary Interop Assembly. This code would look a lot neater with the new C# 4.0 features...
您想要使用 AltChunks 和 OpenXml SDK 1.0(如果可以的话,至少使用 2.0)。 查看 Eric White 的博客了解更多详细信息,这也是一个很棒的资源! 这是一个代码示例,即使不能立即工作,也可以帮助您入门。
You want to use AltChunks and the OpenXml SDK 1.0 (at a minimum, 2.0 if you can). Check out Eric White's blog for more details and just as a great resource!. Here is a code sample that should get you started, if not work immediately.
它非常复杂,因此代码超出了论坛帖子的范围,我会为您编写您的应用程序,但总结一下。
Its quit complex so the code is outside the scope of a forum post, I'd be writing your App for you, but to sum up.
我用 C# 创建了一个应用程序,将 RTF 文件合并到一个文档中,我希望它也适用于 DOC 和 DOCX 文件。
希望这可以帮助!
I had made an application in C# to merge RTF files into one doc,Iam hopeful it should work for DOC and DOCX files as well.
Hope this helps!
对于任何想要使用文件名列表的人:
For anyone who wants to work with a list of file names: