'Save rich text boxes as pdf files and send an email with attachments
I want to save rich text boxes as pdf files. Each time I save a file Adobe Reader can't open it.
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);
}
}
I also made send button to send an email with attachments but the problem is I'm unable to send the email:
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);
Solution 1:[1]
May be Save PDF and MS Word File in C# can help you!!!! It uses iTextSharp
Solution 2:[2]
Using this or this library can help you. As @JleruOHeP stated in a comment simply renaming the file won't work.
Solution 3:[3]
The problem is that, using the method, you cannot save the content of a RichTextBox in the PDF format.
Here you can find the currently available stream format types the can be used: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxstreamtype.aspx.
As you see, the main supported type is RTF (Rich Text Format), a plain-text multi-platform format: this is very different from PDF. Look here and here.
EDIT: I just answer to the questioneer's comment asking for some helping code:
// 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 :(");
}
Solution 4:[4]
As everyone has said, you can't simply save RTF and change the extension to make a PDF, they are incompatible formats. Among the many commercial components available, AbcPdf allows you to read in RTF, and then save the output as a PDF: http://www.websupergoo.com/abcpdf-11.htm#note
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Niladri Biswas |
Solution 2 | t3hn00b |
Solution 3 | |
Solution 4 | Jude Fisher |