首页 > 解决方案 > 将 RichTextBox 中的彩色文本保存为 pdf

问题描述

我遇到了以下问题:
我在 WinForms 中有一个 RichTextBox,我在其中根据条件更改了几行的颜色。
我现在想将 RichTextBox 的内容保存在 PDF 中,使用iTextSharp.
但是每当我打开保存的文件时,我只得到黑线,颜色丢失
我的代码:

        string str = txt_result.Text;
        string path = "";

        SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "pdf files (*.pdf)|*.pdf";
        dlg.Title = "Als PDF speichern";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            path = dlg.FileName;
        }
        Document document = new Document(PageSize.A4);
        var output = new FileStream(path, FileMode.Create);
        var writer = PdfWriter.GetInstance(document, output);

        document.Open();

        document.Add(new iTextSharp.text.Paragraph(str));


        document.Close();
        writer.Close();
        output.Close();

谢谢您的帮助!

编辑:我已经尝试过txt_result.rtf,但是使用 RTF 我只能在文件中得到神秘符号

标签: c#winforms

解决方案


推荐阅读