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

发布于 2024-08-13 04:44:39 字数 71 浏览 1 评论 0原文

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

How can I convert an RTF file to a PDF one? I have the adobe PDF printer, should I use it? If so, how can I programmatically access it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯沦落人 2024-08-20 04:44:39

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

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

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

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

You can use a PDF printer, but then you still have a few problems to solve.

In order to handle text that spans multiple pages, you need this article to create a descendant of RichTextbox that handles the EM_FORMATRANGE Message.

There are a lot of (free) PDF printer out there, but I found that only BioPdf will let you control the filename of the output. They also have reasonable rates for licensed versions.

I have used this to create complex reports (combinations of multiple RTF segments and custom graphics) as attachments for emailing.

毁梦 2024-08-20 04:44:39

如果允许,您可以使用虚拟打印驱动程序 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";

可能有一种打印机枚举方法,该方法会更优雅、更健壮,并且可以针对此方法进行测试,看看打印驱动程序是否存在,也许针对配置文件设置。

更新:
此方法负责处理多个页面:根据 msdn 示例,this.pd_PrintPage。
PrintDocument 支持页面打印和页面打印。
DoPdf 会自动弹出文件另存为对话框,以便将文件另存为 pdf 文档。

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

You could use the virtual print Driver doPdf http://www.dopdf.com/ if this is permitted on the production machine. This will convert more or less any file type to a pdf format not just rtf. It just appears as another printer within Print Manager once installed.

To use it in say winforms code I adapted the code found on the msdn printing example 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);
        }

    }

The only part of the code I needed to add was that marked above e.g. pd.PrinterSettings.PrinterName = "doPDF v6";

There may be a printer enumeration method which would be more elegant and robust and against this one could test to see if the print driver existed perhaps against a config file setting.

Update:
Handling multiple pages is taken care of in this method : this.pd_PrintPage as per the msdn sample.
PrintDocument supports from and to page printing.
DoPdf will pops up a fileSaveAsDialog box automatically so the files can be saved as a pdf document.

What about rtf though?
A Microsoft format not supported very well so it would seem. This article http://msdn.microsoft.com/en-us/library/ms996492.aspx with demo code uses the RichTextBox as a starting point and by using P/Invoke leverages the power of Win32 to print RTF as WYSIWG. The control defines it's own page length method replacing the one used above in the code snippet and still uses PrintDocument so it should be easy to use. You can assign any rtf using Rtb.rtf method.

原谅过去的我 2024-08-20 04:44:39

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 的引用> 组装。

An RTF document has to be read and interpreted by some app that can understand that format. You would need to programmatically launch that app, load your RTF file, and send it to the PDF printer. Word would be good for that, since it has a nice .NET interface. An overview of the steps would be:

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

You will need to use the Microsoft.Office.Interop.Word namespace and add a reference to the Microsoft.Office.Interop.Word.dll assembly.

雪落纷纷 2024-08-20 04:44:39

事实上,这些都不是非常可靠,也不是我想要的。解决方案很简单,安装 Adob​​e Acrobat 并让它使用 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]);
                }
            }

Actually, none of these are terribly reliable or do what I want. The solution is simple, install Adobe Acrobat and just have it open the RTF file using the Process class.

I also found a more reasonable approach. I save the file as an RTF, the open it in word, and save it as PDF (Word's Print As PDF plugin must be installed)

            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]);
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文