将富文本框保存为pdf文件,并发送包含附件的电子邮件

时间:2012-08-28 06:39:05

标签: c# winforms pdf-generation richtextbox

我想将富文本框保存为pdf文件。每次保存文件时,Adobe Reader都无法打开它。

private void button3_Click(object sender, EventArgs e)
{
   SaveFileDialog MyFiles = new SaveFileDialog();
   MyFiles.Filter = "PDF Files|*.pdf";
   MyFiles.Title = "Save As...";
   MyFiles.DefaultExt = "*.pdf";

   if (MyFiles.ShowDialog() == DialogResult.OK) 
   {
      richTextBox1.SaveFile(MyFiles.FileName, RichTextBoxStreamType.PlainText);
      richTextBox3.SaveFile(MyFiles.FileName, RichTextBoxStreamType.PlainText);
      richTextBox4.SaveFile(MyFiles.FileName, RichTextBoxStreamType.PlainText);
      richTextBox5.SaveFile(MyFiles.FileName, RichTextBoxStreamType.PlainText);
   }
}

我还发了一个发送按钮来发送带附件的电子邮件,但问题是我无法发送电子邮件:

        MailMessage MyMail = new MailMessage(richTextBox1.Text, richTextBox4.Text);
        MyMail.To.Add(new MailAddress(richTextBox4.Text));
        MailAddress mail = new MailAddress(richTextBox1.Text);
        MyMail.From = mail;
        MyMail.Subject = richTextBox5.Text;
        MyMail.Body = richTextBox3.Text;
        MyMail.Attachments.Add(new Attachment(richTextBox2.Text));
        SmtpClient MySmtp = new SmtpClient(TheServer.Text);
        MySmtp.UseDefaultCredentials = true;
        MySmtp.EnableSsl = true;
        MySmtp.Port = Convert.ToInt32(ThePort.Text);
        MySmtp.Send(MyMail);            

4 个答案:

答案 0 :(得分:0)

问题是,使用该方法,您无法以PDF格式保存RichTextBox的内容。

您可以在此处找到可以使用的当前可用的流格式类型:http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxstreamtype.aspx

如您所见,主要支持的类型是RTF(富文本格式),一种纯文本多平台格式:这与PDF非常不同。看herehere

编辑:我只是回答了提问者的评论,要求提供一些帮助代码:

// This method opens a dialog and save the content of the passed RichTextBox
private bool ShowRichTextBoxSaveDialog(RichTextBox richTextBox)
{
    SaveFileDialog newFileDialog = new SaveFileDialog();
    newFileDialog.Filter = "PDF Files|*.pdf";
    newFileDialog.Title = "Save As...";
    newFileDialog.Filter = "*.pdf";

    // If the user confirm the dialog window...
    if (newFileDialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            richTextBox.SaveFile(newFileDialog.FileName, RichTextBoxStreamType.PlainText);

            // Success!
            return true;
        }
        catch(Exception e)
        {
            // Error during saving!
            MessageBox.Show(String.Concat("Error during saving: ", e.Message));

            return false;
        }
    }
    else
            // Aborted by the user!
            return false;
}

private void button3_Click(object sender, EventArgs e)
{
   // NEXT WILL SHOW UP 4 DIALOGS, FOR ASKING THE USER 4 FILES TO SAVE!
   this.ShowRichTextBoxSaveDialog(richTextBox1);
   this.ShowRichTextBoxSaveDialog(richTextBox3);
   this.ShowRichTextBoxSaveDialog(richTextBox4);

   // HERE I ALSO CHECK IF THE SAVING IS SUCCESSFUL..
   if (this.ShowRichTextBoxSaveDialog(richTextBox5))
       MessageBox.Show("Success in saving :)");
   else
       MessageBox.Show("Failure in saving :(");
}

答案 1 :(得分:0)

可能Save PDF and MS Word File in C#可以帮助你!!!!它使用iTextSharp

答案 2 :(得分:0)

使用thisthis库可以为您提供帮助。正如@JleruOHeP在评论中所述,只是重命名该文件将无法正常工作。

答案 3 :(得分:0)

正如大家所说,你不能简单地保存RTF并更改扩展名来制作PDF,它们是不兼容的格式。在众多可用的商业组件中,AbcPdf允许您读取RTF,然后将输出保存为PDF:http://www.websupergoo.com/abcpdf-11.htm#note