简体   繁体   English

使用MigraDoc生成DOC或DOCX

[英]Generating a DOC or DOCX using MigraDoc

I am working on a project where I need to create a Word file. 我正在一个需要创建Word文件的项目中工作。 For this purpose, I am using MigraDoc library for C#. 为此,我将MigraDoc库用于C#。

Using this library, I am easily able to generate a RTF file by writing : 使用此库,我可以通过编写以下代码轻松生成RTF文件:

Document document = CreateDocument();
RtfDocumentRenderer rtf = new RtfDocumentRenderer();
rtf.Render(document, "test.rtf", null);
Process.Start("test.rtf");

But the requirement now asks me to get a DOC or DOCX file rather than a RTF file. 但是要求现在要求我获取DOC或DOCX文件而不是RTF文件。 Is there a way to generate a DOC or DOCX file using MigraDoc? 有没有一种使用MigraDoc生成DOC或DOCX文件的方法? And if so, how may I achieve this? 如果是这样,我该如何实现?

MigraDoc cannot generate DOC or DOCX files. MigraDoc无法生成DOC或DOCX文件。 Since MigraDoc is open source, you could add a renderer for DOCX if you have the knowledge and the time. 由于MigraDoc是开源的,因此如果您有足够的知识和时间,则可以为DOCX添加渲染器。

MigraDoc as it is cannot generate DOC/DOCX, but maybe you can invoke an external conversion tool after generating the RTF file. MigraDoc本身不能生成DOC / DOCX,但是也许您可以在生成RTF文件后调用外部转换工具。
I don't know any such tools. 我不知道任何这样的工具。 Word can open RTF quickly and so far our customers never complained about getting RTF, not DOC or DOCX. Word可以快速打开RTF,到目前为止,我们的客户从未抱怨过要获得RTF,而不是DOC或DOCX。

Update (2019-07-29): The website mentions "Word", but this only refers to RTF. 更新(2019-07-29):该网站提到了“单词”,但这仅指RTF。 There never was an implementation for .DOC or .DOCX. 从来没有.DOC或.DOCX的实现。

It seems no any MigraDoc renders that support DOC or DOCX formats. 似乎没有任何支持DOCDOCX格式的MigraDoc渲染器。

On documentation page we can see one MigraDoc feature: 在文档页面上,我们可以看到一项MigraDoc功能:

Supports different output formats (PDF, Word, HTML, any printer supported by Windows) 支持不同的输出格式(PDF,Word,HTML,Windows支持的任何打印机)

But seems documentation says about RTF format that perfectly works with Word. 但是似乎文档说明了与Word完美配合的RTF格式。 I have reviewed MigraDoc repository and I do not see any DOC renders. 我已经审查了MigraDoc存储库 ,但没有看到任何DOC渲染图。 We can use only RTF converter for Word supporting. 我们只能将RTF转换器用于Word支持。 So we can't generate DOC file directly using this package. 因此,我们无法使用此包直接生成DOC文件。

But we can convert RTF to DOC or DOCX easily (and for free) using FreeSpire.Doc nuget package. 但是我们可以使用FreeSpire.Doc nuget包轻松(免费)将RTF转换为DOCDOCX

Full code example is here: 完整的代码示例在这里:

using MigraDoc.DocumentObjectModel;
using MigraDoc.RtfRendering;
using Spire.Doc;
using System.IO;

namespace MigraDocTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var stream = new MemoryStream())
            {
                // Generate RTF (using MigraDoc)
                var migraDoc = new MigraDoc.DocumentObjectModel.Document();
                var section = migraDoc.AddSection();
                var paragraph = section.AddParagraph();
                paragraph.AddFormattedText("Hello World!", TextFormat.Bold);
                var rtfDocumentRenderer = new RtfDocumentRenderer();
                rtfDocumentRenderer.Render(migraDoc, stream, false, null);

                // Convert RTF to DOCX (using Spire.Doc)
                var spireDoc = new Spire.Doc.Document();
                spireDoc.LoadFromStream(stream, FileFormat.Auto);
                spireDoc.SaveToFile("D:\\example.docx", FileFormat.Docx );
            }
        }
    }
}

您可以使用具有NuGet包的Microsoft的DocumentFormat.OpenXML库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM