1

I want to save complete mail as PDF.

I found code below in stackoverflow 1. It saves the mailitem body and not the header (such as sender, recipient, subject).

I tried to manipulate the Word.Document to add the header info manually (in the code below I just use minimal changes for testing purposes) but it seems to be readonly. I also thought of "Print as PDF" using the Outlook print functionality, but found no way to get it triggered from my Outlook VSTO solution.

using Word = Microsoft.Office.Interop.Word;

private void SaveMailAsPDF(Outlook.MailItem _mailitem)
        {
            //source: https://stackoverflow.com/questions/26421252/save-outlook-mailitem-body-as-pdf
            Outlook.MailItem mi = _mailitem;
            mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string datetimeReceived = mi.ReceivedTime.ToString("yyyyMMdd-Hmmss");
            string fullPath = @"C:\Users\al\Documents\OutlookMailsTest\" + datetimeReceived + "Test.pdf";

            Word.Document doc = mi.GetInspector.WordEditor;
            //doc.Paragraphs.Add();
            //doc.Paragraphs.Add();
            //Word.Range rng = doc.Range(0, 0);
            //rng.Text = "New Text";
            doc.SaveAs2(fullPath, FileFormat: Word.WdSaveFormat.wdFormatPDF);
        }
Community
  • 1
  • 1
Alexander
  • 23
  • 3

2 Answers2

1

Try to save in the MHTML format (it preserves the embedded pictures and includes the headers) using MailItem.SaveAs, then open the MHTML file using Word object model and save it as a PDF file.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks for your reply. Since it is not possible to select a printer other than the default printer when using Mailitem.Printout() (see post [link](https://stackoverflow.com/questions/49299267/how-to-change-print-preferences-when-using-mailitem-printout-in-vsto) ) I will make use of your proposal. I have also tried to use a free version of itextsharp PDF library to add header information of email (sender, recipient, subject ..) at the top of PDF - but this is hard to implement. Your proposal is a little bit slow since Word needs to opened but it works! – Alexander Sep 06 '21 at 05:13
  • If the post answers your question, please mark it as such. Thanks! – Dmitry Streblechenko Sep 06 '21 at 21:42
1

The Outlook object model doesn't provide any print as pdf methods. The MailItem.PrintOut method prints the Outlook item using all default settings.The PrintOut method is the only Outlook method that can be used for printing.

You can use the Word object model for saving the message body in the format you need. For example, you may save the document which represents the message body using the open XML document (*.docx) and then open it for editing using Open XML SDK for adding message headers (if required). See Welcome to the Open XML SDK 2.5 for Office for more information.

You are free to use third-party components to save the document with required customizations using the PDF file format.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45