PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Outlook crashing when utilising PDFSharp in a VSTO Add in
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3697
Page 1 of 1

Author:  jimf [ Fri Nov 24, 2017 11:53 am ]
Post subject:  Outlook crashing when utilising PDFSharp in a VSTO Add in

I'm developing an Add in for Outlook that processes PDF Attachments - I'm using the Library from PDFSharp to do the actual processing of the PDF. We check the number of pages and if more than one split the email into single page pdfs

Code:
public class PDF_Processing
     {
          public static void SplitPDFintoSinglePages()
          {
               // MessageBox.Show("Starting Spliting");
               DataClass.strClosePDU = "N";
               string NewName = string.Empty;
               string NewFileName = string.Empty;
               string strBody = string.Empty;
               string strSubject = string.Empty;
               string stri = string.Empty;
               int inti = 0;
               string eReceipient = DataClass.strTNeMailAddr;

               MessageBoxButtons buttons = MessageBoxButtons.YesNo;
               DialogResult result = System.Windows.Forms.DialogResult.No;

               EventLog.WriteEntry("PDU_VSTO", "PDF Processing " + "\n" + "\n" + DataClass.savedAttachment  + "\n" + "\n" + "InfoCode: ppi0001", EventLogEntryType.Information, 401, 99);

               try
               {
                    PdfDocument inputDocument = PdfReader.Open(DataClass.savedAttachment, PdfDocumentOpenMode.Import);
               
                    DataClass.iPageCount = inputDocument.PageCount;
   
                    if (DataClass.iPageCount > 1)
                    {
                         result = MessageBox.Show(Strings.PageCount + DataClass.iPageCount + System.Environment.NewLine + Strings.SplitQuestion, Strings.PDFSplitTitle, buttons);
                    }

                    if (result != System.Windows.Forms.DialogResult.Yes)
                    {
                         // Closes the parent form.
                         return;
                    }
                    else
                    {
                         // Ensure the TempDetails Thread has finished
                         while (DataClass.strSurname == string.Empty)
                         {
                              Thread.Sleep(200);
                         }

                         for (int i = 0; i <= DataClass.iPageCount - 1; i++)
                         {
                              // Correct the PDF Page index to an actual page number
                              inti = i + 1;
                              stri = inti.ToString();
                             
                              NewName = DataClass.strSurname + "_" + DataClass.strForename + "_" + DataClass.strPNR + "_" + "Page_" + stri + "_of_" + DataClass.iPageCount + ".pdf";
                              NewFileName = @"C:\Temp\" + NewName;

                              PdfDocument outputDocument = new PdfDocument();

                              outputDocument.Version = inputDocument.Version;
                              outputDocument.Info.Title = string.Format("Page {0} of {1}", i + 1, inputDocument.Info.Title);
                              outputDocument.Info.Creator = inputDocument.Info.Creator;
                              outputDocument.AddPage(inputDocument.Pages[i]);
                              outputDocument.Save(NewFileName);

                              // strBody = "PDU Split PDF" + "<BR>" + "<BR>" + "Last Name : " + DataClass.strSurname + "<BR>" + "First Name : " + DataClass.strForename + "<BR>" + "PNR : " + DataClass.strPNR + "<BR>" + "Original eMail Address : " + DataClass.senderMailAddress;
                              // strSubject = "Split PDF from : " + DataClass.senderMailAddress + "_" + NewName;

                              // eMailHandling.SendSpliteMail(strSubject, strBody, eReceipient, NewFileName);
                              DataClass.strNewPDFs.Add(NewFileName);

                              // File.Delete(NewFileName);
                         }

                         DataClass.strClosePDU = "Y";

                         strBody = "PDU Split PDF" + "<BR>" + "<BR>" + "Last Name : " + DataClass.strSurname + "<BR>" + "First Name : " + DataClass.strForename + "<BR>" + "PNR : " + ataClass.strPNR + "<BR>" + "Original eMail Address : " + DataClass.strRetEmail;
                         //// strSubject = "Split PDF from : " + DataClass.senderMailAddress;
                         strSubject = "Split PDF from : " + DataClass.strRetEmail;

                         eMailHandling.SendeMailPDFs(strSubject, strBody, eReceipient);
                    }
               }
               catch (Exception ex)
               {
                    EventLog.WriteEntry("PDU_VSTO", strings.ErrorSplitPDF + "\n" + "\n" + ex.Message + "\n" + "\n" + "ErrorCode: pp0003", EventLogEntryType.Error, 402, 99);
                    MessageBox.Show(ex.Message + "\n" + "\n" + strings.ServiceNowPP + "\n" + "\n" + "ErrorCode: pp0003", strings.ErrorSplitPDF);
                    //EventLog.WriteEntry("PDU_VSTO", "PDU Exited due to an Exception", EventLogEntryType.Error, 9, 99);
                    //// System.Windows.Forms.Application.Exit();
               }
          }


So most of the time everything is fine, but, every so often Outlook just crashes but does not leave an event or raise any errors. I have try / catch blocks round the code but don't get any exceptions. Whilst the error can be reproduced - by processing a few hundred emails - I cannot produce it at will so cannot really run via the Debugger. The other point is that it does not always crash whilst the VSTO is in use

If I remove the PDFSharp libraries and related code then the outlook crashes don't occur

When Outlook crashes there are no errors generated

Any ideas what I might be doing to cause this

Cheers

Jim

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/