PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:06 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Nov 24, 2017 11:53 am 
Offline

Joined: Thu Nov 23, 2017 3:48 pm
Posts: 1
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 127 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group