如何将RTF文件转换为pdf文件?

6

我该如何将RTF文件转换为PDF文件?我有Adobe PDF打印机,我应该使用它吗?如果是这样,我如何以编程方式访问它?


@Malfist - 我已按照Henk的建议更新了下面的答案,以提供更完整的答案。 - Andrew
4个回答

2
您可以在生产机器上允许的情况下使用虚拟打印机doPdfhttp://www.dopdf.com/。这将把几乎任何文件类型转换为PDF格式,而不仅仅是RTF。一旦安装,它会在打印管理器中出现为另一个打印机。
要在WinForms代码中使用它,可以改编MSDN打印示例http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx中的代码。
private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            streamToPrint = new System.IO.StreamReader
               (@"F:\temp\labTest.txt");
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrinterSettings.PrinterName = "doPDF v6";//<-------added
                pd.PrintPage += new PrintPageEventHandler
                   (this.pd_PrintPage);
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

我需要添加的代码部分仅为上述标记的部分,例如 pd.PrinterSettings.PrinterName = "doPDF v6"。
可能有一种更优雅且健壮的打印机枚举方法,可以针对此进行测试,以查看打印驱动程序是否存在,也许针对配置文件设置。
更新: 处理多个页面在此方法中已得到解决:this.pd_PrintPage,与 MSDN 示例相同。 PrintDocument 支持从和到页面打印。 DoPdf 将自动弹出 fileSaveAsDialog 对话框,因此文件可以保存为 pdf 文档。
那么 RTF 呢? 这是一种微软格式,似乎不受支持。本文http://msdn.microsoft.com/en-us/library/ms996492.aspx使用 RichTextBox 作为起点,并通过使用 P/Invoke 利用 Win32 的功能来打印 RTF 作为 WYSIWG。该控件定义了自己的页面长度方法,替换了上面代码片段中使用的方法,仍然使用 PrintDocument,因此易于使用。您可以使用 Rtb.rtf 方法分配任何 rtf。

接近了,但完整的答案应该包括如何打印RTF(带分页等)。此外,您可能需要一个PDF打印机,让您设置输出文件名。 - H H
@Henk,我已经编辑了这篇文章(在结尾处添加了一段内容),应该可以解决你提出的所有问题。 - Andrew
不起作用,因为它只打印RTF文件的文本部分,即不可读的部分。它打印的是如果我在记事本中打开RTF文件时会打印出来的内容。 - Malfist
@Malfist - 你是否使用我在答案更新部分引用的文章来修改上面的代码?我看到的是Rtb显示的可读输出,而不是你所描述的行为,这完全违背了练习的目的。 - Andrew

2
你可以使用PDF打印机,但是你仍然需要解决一些问题。为了处理跨越多个页面的文本,你需要阅读这篇文章创建一个RichTextbox的子类来处理EM_FORMATRANGE消息。有很多(免费的)PDF打印机可用,但我发现只有BioPdf会让你控制输出文件的文件名。他们还有合理的授权版本费用。我曾经使用它来创建复杂的报告(多个RTF段和自定义图形的组合)作为附件发送邮件。

0
一个 RTF 文件必须由一些可以理解该格式的应用程序进行阅读和解释。您需要通过编程启动该应用程序,加载您的 RTF 文件,并将其发送到 PDF 打印机。Word 对此很好,因为它具有良好的 .NET 接口。步骤概述如下:
ApplicationClass word = new ApplicationClass();
Document doc = word.Documents.Open(ref filename, ...);
doc.PrintOut(...);

你需要使用 Microsoft.Office.Interop.Word 命名空间,并添加对 Microsoft.Office.Interop.Word.dll 程序集的引用。

我可以,但是我该怎么做呢? - Malfist
这个方法在普通环境下可以正常工作,但在服务器环境下不适用。微软不建议或支持在服务器环境中使用Office API。例如,请参考问题159744。 - Eric Pohl
很好,但问题中没有任何指向服务器环境的内容。 :-) - CesarGon

-1

实际上,这些都不是非常可靠或不能满足我的需求。解决方案很简单,安装Adobe Acrobat并使用Process类打开RTF文件。

我还发现了一个更合理的方法。我将文件保存为RTF格式,然后在Word中打开它,并将其另存为PDF(必须安装Word的Print As PDF插件)。

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Personal Document File (*.pdf)|*.pdf";
            if (sfd.ShowDialog() == DialogResult.OK) {
                String filename = Path.GetTempFileName() + ".rtf";
                using (StreamWriter sw = new StreamWriter(filename)) {
                    sw.Write(previous);
                }


                Object oMissing = System.Reflection.Missing.Value;    //null for VB
                Object oTrue = true;
                Object oFalse = false;

                Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

                oWord.Visible = false;
                Object rtfFile = filename;
                Object saveLoc = sfd.FileName;
                Object wdFormatPDF = 17;    //WdSaveFormat Enumeration
                oWordDoc = oWord.Documents.Add(ref rtfFile, ref oMissing, ref oMissing, ref oMissing);
                oWordDoc.SaveAs(ref saveLoc, ref wdFormatPDF, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
                oWord.Quit(ref oFalse, ref oMissing, ref oMissing);

                //Get the MD5 hash and save it with it
                FileStream file = new FileStream(sfd.FileName, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                using (StreamWriter sw = new StreamWriter(sfd.FileName + ".md5")) {
                    sw.WriteLine(sfd.FileName + " - " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + " md5: " + BinaryToHexConverter.To64CharChunks(retVal)[0]);
                }
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接