winforms 将格式文本框保存为pdf文件并发送带有附件的电子邮件

brgchamk  于 2023-03-13  发布在  其他
关注(0)|答案(4)|浏览(96)

我想将格式文本框另存为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);
dojqjjoe

dojqjjoe2#

使用thisthis库可以帮助你。正如@JleruOHeP在评论中所说,简单地重命名文件是行不通的。

z9ju0rcb

z9ju0rcb3#

问题在于,使用方法时,无法将RichTextBox的内容保存为PDF格式。
您可以在此处找到当前可用的流格式类型: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 :(");
}
umuewwlo

umuewwlo4#

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

相关问题