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

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

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

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

有很多(免费)PDF打印机,但我发现只有BioPdf才能控制输出的文件名。 它们对许可版本也有合理的价格。

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

如果在生产机器上允许,可以使用虚拟打印驱动程序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将自动弹出fileSaveAsDialog框,以便将文件保存为pdf文档。

rtf怎么样 ? 似乎不支持Microsoft格式。 这篇带有演示代码的文章http://msdn.microsoft.com/en-us/library/ms996492.aspx使用RichTextBox作为起点,并使用P / Invoke利用Win32的强大function将RTF打印为WYSIWG。 控件定义了它自己的页面长度方法,取代了上面在代码片段中使用的方法,并且仍然使用PrintDocument,因此它应该易于使用。 您可以使用Rtb.rtf方法分配任何rtf。

看到这篇文章 。 看起来你可以使用它而无需很多修改。 它使用Open Office。

ITextSharp应该为你做的伎俩。

必须由能够理解该格式的应用程序读取和解释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程序集的引用。

实际上,这些都不是非常可靠或者我想要的。 解决方案很简单,安装Adobe Acrobat并让它使用Process类打开RTF文件。

我也找到了一种更合理的方法。 我将文件保存为RTF,将其打开,然后将其保存为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]); } }