Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm trying to convert a outlook message file i.e. .msg file to a .pdf file using PDFsharp library. Now, my goal is to make the pdf file looking identical to the msg file even if has tables, images or colored/formatted text. But my code is not doing that properly.

What I have tried:

C#
public static void ConvertMsgToPdf(string msgFilePath, string pdfFilePath)
		{
			// Load the Outlook email
			Application outlookApp = new Application();
			MailItem mailItem = (MailItem)outlookApp.CreateItemFromTemplate(msgFilePath);

			// Create a new PDF document
			PdfDocument document = new PdfDocument();

			// Create a new PDF page
			PdfPage page = document.AddPage();

			// Create a new PDF graphics object
			XGraphics gfx = XGraphics.FromPdfPage(page);

			// Handle tables in the email body
			Microsoft.Office.Interop.Outlook.Inspector inspector = mailItem.GetInspector as Microsoft.Office.Interop.Outlook.Inspector;
			if (inspector != null)
			{
				Microsoft.Office.Interop.Word.Document document2 = inspector.WordEditor as Microsoft.Office.Interop.Word.Document;
				if (document2 != null)
				{
					foreach (Microsoft.Office.Interop.Word.Table table in document2.Tables)
					{
						for (int row = 1; row <= table.Rows.Count; row++)
						{
							for (int col = 1; col <= table.Columns.Count; col++)
							{
								string cellText = table.Cell(row, col).Range.Text;
								gfx.DrawString(cellText, new XFont("Arial", 12), XBrushes.Black, new XRect(col * 100, row * 20, 100, 20), XStringFormats.TopLeft);
							}
						}
					}
				}
			}

			// Handle text in the email body
			string[] lines = mailItem.Body.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
			for (int i = 0; i < lines.Length; i++)
			{
				gfx.DrawString(lines[i], new XFont("Arial", 12), XBrushes.Black, new XRect(0, i * 20, page.Width, 20), XStringFormats.TopLeft);
			}

			// Handle images in the email body
			foreach (Microsoft.Office.Interop.Outlook.Attachment attachment in mailItem.Attachments)
			{
				if (attachment.Type == Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
				{
					string fileName = Path.Combine(Path.GetTempPath(), attachment.FileName);
					attachment.SaveAsFile(fileName);

					XImage image = XImage.FromFile(fileName);
					gfx.DrawImage(image, new XRect(0, 0, page.Width, page.Height));
				}
			}

			// Save the document
			document.Save(pdfFilePath);
		}


Can anyone help me with this ?
Note: If there is any other open source library that can get this done I'm open to it as well. Also, I want to show the sender info and subject of the email at the top as well in the pdf.
Posted
Comments
Richard MacCutchan 13-May-23 12:46pm    
Why not just use the built-in "Microsoft Print to PDF"?
Member 12692000 13-May-23 12:54pm    
I want to add this feature in my app.
mtoha 17-May-23 6:38am    
I am using PDFsharp and this lib generates error. For free library, as far as I know, there are few library for PDF you can search at nugget. For free library, usually for GPU lisence, not for business or propietary application.

1 solution

As far as i know there's no exact method to write mail message to *.pdf via PDFSharp library. But! there's a workaround...

To able to achieve that you have to:
- convert/save mail message as rtf (see: MailItem.SaveAs method (Outlook) | Microsoft Learn[^]), then
- open that *.rtf in RichTextBox and
- write the lines of RichTextBox to pdf.

Done!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900