代码之家  ›  专栏  ›  技术社区  ›  Malfist

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

  •  4
  • Malfist  · 技术社区  · 15 年前

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

    6 回复  |  直到 14 年前
        1
  •  2
  •   Henk Holterman    15 年前

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

    为了处理跨越多个页面的文本,需要 this article 创建richtextbox的子代以处理em_formatrange消息。

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

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

        2
  •  2
  •   Andrew    15 年前

    您可以使用虚拟打印驱动程序dopdf http://www.dopdf.com/ 如果生产机器允许这样做。这将或多或少地将任何文件类型转换为PDF格式,而不仅仅是RTF。安装后,它将显示为“打印管理器”中的另一台打印机。

    为了在say 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”;

    可能有一种打印机枚举方法,它会更优雅、更健壮,针对这种方法,可以测试打印驱动程序是否存在,或者针对配置文件设置。

    更新: 此方法处理多个页面:此.pd_printpage根据msdn示例。 PrintDocument支持从和到页面的打印。 dopdf将自动弹出一个文件保存对话框,以便将文件保存为PDF文档。

    但是RTF呢 ? Microsoft格式不受很好的支持,所以看起来是这样。本文 http://msdn.microsoft.com/en-us/library/ms996492.aspx 演示代码使用richtextbox作为起点,使用p/invoke利用win32的功能将rtf打印为wysiwg。控件定义了它自己的页面长度方法,替换了上面在代码段中使用的方法,并且仍然使用printdocument,因此应该易于使用。您可以使用rtb.rtf方法分配任何rtf。

        3
  •  1
  •   Roman Boiko    15 年前

    this article . 看起来你不用做很多修改就可以使用它。它使用开放式办公室。

        4
  •  0
  •   Kevin LaBranche    15 年前

    ITextSharp 应该为你表演这个把戏。

        5
  •  0
  •   CesarGon    15 年前

    RTF文档必须由一些能够理解该格式的应用程序读取和解释。您需要以编程方式启动该应用程序,加载RTF文件,然后将其发送到PDF打印机。因为它有一个很好的.NET接口,所以Word很适合这样做。步骤概述如下:

    ApplicationClass word = new ApplicationClass();
    Document doc = word.Documents.Open(ref filename, ...);
    doc.PrintOut(...);
    

    您需要使用 Microsoft.Office.Interop.Word 命名空间并添加对 Microsoft.Office.Interop.Word.dll 装配。

        6
  •  -1
  •   Malfist    15 年前

    实际上,这些都不是非常可靠的,也不是我想做的。解决方案很简单,安装AdobeAcrobat,让它使用process类打开rtf文件。

    我也找到了一个更合理的方法。我将文件保存为RTF,在Word中打开它,并将其保存为PDF(必须安装Word的打印为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]);
                    }
                }