6

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

4

4 回答 4

2

如果在生产机器上允许,您可以使用虚拟打印驱动程序 doPdf http://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 会自动弹出一个 fileSaveAs 对话框,以便将文件保存为 pdf 文档。

但是 rtf呢?一种 Microsoft 格式并没有得到很好的支持,所以看起来。这篇带有演示代码的文章http://msdn.microsoft.com/en-us/library/ms996492.aspx使用 RichTextBox 作为起点,并通过使用 P/Invoke 利用 Win32 的强大功能将 RTF 打印为 WYSIWG。该控件定义了它自己的页面长度方法,替换了上面代码片段中使用的方法,并且仍然使用 PrintDocument,因此它应该易于使用。您可以使用 Rtb.rtf 方法分配任何 rtf。

于 2009-12-05T21:03:57.117 回答
2

您可以使用 PDF 打印机,但仍有一些问题需要解决。

为了处理跨多个页面的文本,您需要本文创建处理 EM_FORMATRANGE 消息的 RichTextbox 的后代。

那里有很多(免费)PDF 打印机,但我发现只有BioPdf可以让您控制输出的文件名。他们还为许可版本提供合理的价格。

我用它来创建复杂的报告(多个 RTF 段和自定义图形的组合)作为电子邮件的附件。

于 2009-12-05T22:44:54.693 回答
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程序集的引用。

于 2009-12-05T20:17:07.063 回答
-1

实际上,这些都不是非常可靠或做我想要的。解决方案很简单,安装 Adob​​e 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]);
                }
            }
于 2009-12-07T21:31:04.640 回答