Articles in this section
Category / Section

How to Create WinForms PDF Dynamically and Email as Attachment?

2 mins read

The Winforms PDF can be created dynamically and send email with an attachment, please find the code example and sample below for the same.

// Create a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
// Add a page to the document.
PdfPage page = document.Pages.Add();
// Create PDF graphics for the page.
PdfGraphics g = page.Graphics;
// Create a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f);
// Draw the text.
g.DrawString("Hello world!", font, brush, new PointF(20, 20));
MemoryStream ms = new MemoryStream();
// Save and close the document.
document.Save(ms);
document.Close(true);
//Reset the memory stream position.  
ms.Position = 0;
//Attach the file
Attachment file = new Attachment(ms ,"Attachment.pdf", "application/pdf");
using (SmtpClient smtp = new SmtpClient("xxxx.co.in"))
{
MailMessage message = new MailMessage();
// end-user customization
message.From = new MailAddress("yyyy@gmail.com");
message.To.Add("zzzz@gmail.com");
message.Subject = "message";
message.Attachments.Add(file);
message.IsBodyHtml = false;
smtp.Send(message);
}

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion:

I hope you enjoyed learning about how to create WinForms PDF dynamically and Email as attachment.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied