PageRenderTime 46ms CodeModel.GetById 2ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 1ms

/DocuVision.EDD.EFD/BaseJob.cs

https://bitbucket.org/docuvision/docuvision.edd.efd
C# | 1292 lines | 1086 code | 118 blank | 88 comment | 81 complexity | 96efddb32eaf15d90125c657b36910c4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1using iTextSharp.text;
   2using iTextSharp.text.pdf;
   3using iTextSharp.text.pdf.parser;
   4using Newtonsoft.Json;
   5using Quartz;
   6using System;
   7using System.Collections;
   8using System.Collections.Generic;
   9using System.Configuration;
  10using System.Data.SqlClient;
  11using System.IO;
  12using System.Linq;
  13using System.Net;
  14using System.Net.Mail;
  15using System.Net.Mime;
  16using System.Net.NetworkInformation;
  17using System.Net.Security;
  18using System.Reflection;
  19using System.Security.Cryptography;
  20using System.Security.Cryptography.X509Certificates;
  21using System.Text;
  22using System.Threading;
  23using System.Threading.Tasks;
  24using System.Xml.Linq;
  25using System.Xml.XPath;
  26
  27namespace DocuVision.EDD.EFD
  28{
  29    [DisallowConcurrentExecution]
  30    class BaseJob : IJob
  31    {
  32        #region Declarations
  33
  34        static ServiceProperties serviceProperties = new ServiceProperties();
  35
  36        private static string inputPath;
  37        private static string outputPath;
  38        private static string auditPath;
  39        private static string smtpHost;
  40        private static int smtpPort;
  41        private static bool archive;
  42        private static bool ssl;
  43        private static string embeddedImagePath;
  44        private static string BCC;
  45        private static string smtpUserName;
  46        private static string smtpPassword;
  47        private static string smtpFrom;
  48        private static string Body;
  49        private static string Subject;
  50        private static string CC;
  51        private static string DocumentCategory;
  52        private static string XMLField;
  53        private static int smtpTimeOut;
  54        private static string replyToAddress;
  55
  56        private static string toAddress = string.Empty;
  57        private static string fromAddress = string.Empty;
  58        private static string DocumentFolder = string.Empty;
  59        private static string TemplateName = string.Empty;
  60        private static string DocNo = string.Empty;
  61
  62        private static string waterMarkText;
  63        private static string table;
  64        private static string efdFolder;
  65
  66        private static Dictionary<string, string> dicParser = new Dictionary<string, string>();
  67
  68        private static log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  69        #endregion
  70
  71        #region Constructor
  72        public BaseJob()
  73        {
  74            ExeConfigurationFileMap map = new ExeConfigurationFileMap()
  75            {
  76                ExeConfigFilename = string.Concat(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "\\serviceApp.config")
  77            };
  78            Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
  79            if (string.IsNullOrEmpty(cfg.AppSettings.Settings["ServiceProperties"].Value)) return;
  80
  81            serviceProperties = JsonConvert.DeserializeObject<ServiceProperties>(cfg.AppSettings.Settings["ServiceProperties"].Value);
  82            //Config
  83            inputPath = serviceProperties.Inputpath;
  84            outputPath = serviceProperties.OutputPath;
  85            auditPath = serviceProperties.AuditPath ?? System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  86            smtpHost = serviceProperties.SmtpHost;
  87            smtpPort = serviceProperties.SmtpPort;
  88            archive = serviceProperties.ArchiveToEdms;
  89            ssl = serviceProperties.SSLEnabled;
  90            smtpTimeOut = serviceProperties.SmtpTimeout;
  91        } 
  92        #endregion
  93
  94        #region Events
  95        public void Execute(IJobExecutionContext context)
  96        {
  97            try
  98            {
  99                #region Licensing
 100                //LicenseModule lm = new LicenseModule();
 101
 102                //if (lm.checkLicense(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "eMailer") == false)
 103                //{
 104                //    logger.Info("License file invalid");
 105                //    return;
 106                //}
 107
 108                //Config cfg = new Config();
 109                //if (cfg.checkConfig() == false)
 110                //{
 111                //    log.Info("Configuration invalid");
 112                //    return;
 113                //}
 114                #endregion
 115
 116                #region Check Directories
 117                if (!Directory.Exists(inputPath)) Directory.CreateDirectory(inputPath);
 118                if (!Directory.Exists(inputPath + "\\Processed\\")) Directory.CreateDirectory(inputPath + "\\Processed\\");
 119
 120                if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath);
 121                if (!Directory.Exists(outputPath + "\\Rejected")) Directory.CreateDirectory(outputPath + "\\Rejected");
 122                //if (!Directory.Exists(outputPath + "\\Rejected\\Transaction Logs")) Directory.CreateDirectory(outputPath + "\\Rejected\\Transaction Logs");
 123                if (!Directory.Exists(outputPath + "\\Processing\\")) Directory.CreateDirectory(outputPath + "\\Processing\\");
 124
 125                if (!Directory.Exists(serviceProperties.AuditPath)) Directory.CreateDirectory(serviceProperties.AuditPath);
 126
 127                if (!Directory.Exists(serviceProperties.EFDExportPath)) Directory.CreateDirectory(serviceProperties.EFDExportPath);
 128                if (!Directory.Exists(serviceProperties.EFDImportPath)) Directory.CreateDirectory(serviceProperties.EFDImportPath);
 129                #endregion
 130
 131                #region Split&Save
 132                log.Info(String.Format("Retrieving all file info in '{0}'", inputPath));
 133                FileInfo[] files = new DirectoryInfo(inputPath).GetFiles("*.pdf", SearchOption.TopDirectoryOnly);
 134                foreach (var ftp in files)
 135                {
 136                    try
 137                    {
 138                        log.Info(string.Format("File received : {0}", ftp.Name));
 139                        SplitAndSave(ftp.FullName, outputPath);
 140                        Thread.Sleep(100);
 141                        //move file
 142                        ftp.MoveTo(System.IO.Path.GetDirectoryName(ftp.FullName) + "\\Processed\\" + System.IO.Path.GetFileNameWithoutExtension(ftp.Name) + "_" + Guid.NewGuid() + ftp.Extension);
 143                    }
 144                    catch (Exception ex)
 145                    {
 146                        log.Error(ex.Message, ex);
 147                    }
 148                }
 149                #endregion
 150
 151                #region Merge
 152                log.Info("Perparing for merge.");
 153                DirectoryInfo[] directories = new DirectoryInfo(outputPath + "\\Processing").GetDirectories("*", SearchOption.AllDirectories);
 154
 155                foreach (var directory in directories)
 156                {
 157                    try
 158                    {
 159                        log.Info(String.Format("Processing folder {0}", directory.Name));
 160
 161                        FileInfo[] dirFiles = new DirectoryInfo(directory.FullName).GetFiles("*.pdf", SearchOption.TopDirectoryOnly);
 162                        string[] arrMerge = new string[dirFiles.Length];
 163                        int count = 0;
 164
 165                        log.Info(String.Format("{0} file found to process.", dirFiles.Length));
 166
 167                        if (dirFiles.Length != 0)
 168                        {
 169                            foreach (var fileToMerge in dirFiles.OrderBy(x => x.CreationTime.Ticks).ToList())
 170                            {
 171                                log.Info(String.Format("Listing {0} for merging.", fileToMerge.FullName));
 172                                arrMerge[count] = fileToMerge.FullName;
 173                                count++;
 174                            }
 175                            string filename = string.Empty;
 176                            try
 177                            {
 178                                filename = System.IO.Path.Combine(directory.FullName, dirFiles[0].Name);
 179                                log.Info(String.Format("New filename set to '{0}'. Merging to start", filename));
 180                                Thread.Sleep(100);
 181                                filename = MergePDF(arrMerge, filename);
 182                                log.Info("Merge complete, File created : " + filename);
 183                                foreach (var item in arrMerge)
 184                                {
 185                                    Thread.Sleep(100);
 186                                    GC.Collect();
 187                                    File.Delete(item);
 188                                }
 189                                File.Move(filename, filename.Substring(0, filename.LastIndexOf("-")));
 190                                filename = filename.Substring(0, filename.LastIndexOf("-"));
 191                                //foreach (var item in arrMerge)
 192                                //{
 193                                //    if (item.ToString().Substring(item.LastIndexOf(@"\") + 1) != filename.Substring(filename.LastIndexOf(@"\") + 1))
 194                                //    {
 195                                //        File.Delete(outputPath + "\\Transaction Logs\\" + item.ToString().Substring(item.LastIndexOf(@"\") + 1).Replace(".pdf", ".txt")); 
 196                                //    }
 197                                //}
 198                            }
 199                            catch (Exception ex)
 200                            {
 201                                log.Error(ex.Message, ex);
 202                            }
 203                        }
 204                        else
 205                        {
 206                            try
 207                            {
 208                                directory.Delete();
 209                            }
 210                            catch (Exception ex)
 211                            {
 212                                log.Error(ex.Message, ex);
 213                            }
 214                        }
 215
 216                    }
 217                    catch (Exception ex)
 218                    {
 219                        log.Error(ex.Message, ex);
 220                    }
 221                }
 222                #endregion
 223
 224                #region Exporting Items
 225                DirectoryInfo[] mailDirs = new DirectoryInfo(outputPath + "\\Processing").GetDirectories("*");
 226                foreach (var mailDir in mailDirs)
 227                {
 228                    files = new DirectoryInfo(mailDir.FullName).GetFiles("*.pdf", SearchOption.AllDirectories);
 229                    foreach (var file in files)
 230                    {
 231                        string folder = string.Empty;
 232                        string mailAddress = string.Empty;
 233                        string response = string.Empty;
 234
 235                        string retVal = string.Empty;
 236
 237                        Thread.Sleep(100);
 238
 239                        try
 240                        {
 241                            #region Get Info
 242                            var info = file.Name.Split('#');
 243
 244                            DocNo = info.Length > 0 ? info[0] : DocNo;
 245                            toAddress = info.Length > 1 ? info[1] : toAddress;
 246                            TemplateName = info.Length > 2 ? info[2] : DocumentFolder;
 247
 248                            //Get Template Data
 249                            if (!GetTemplateValues(TemplateName))
 250                            {
 251                                log.Error("A template has not been setup for the folder " + TemplateName + " or the template is incorrect");
 252                            } 
 253                            #endregion
 254
 255                            #region Database Authentication
 256                            if (serviceProperties.EnableDatabase)
 257                            {
 258                                try
 259                                {
 260                                    //Build SQL ConnectionString
 261                                    System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
 262                                    builder.DataSource = serviceProperties.DataSource;
 263                                    builder.UserID = serviceProperties.UserID;
 264                                    builder.Password = serviceProperties.Password;
 265                                    builder.InitialCatalog = serviceProperties.Database;
 266
 267                                    using (SqlConnection conn = new SqlConnection(builder.ConnectionString))
 268                                    {
 269                                        conn.Open();
 270
 271                                        SqlCommand cmd = new SqlCommand("IF EXISTS(SELECT DocumentID FROM " + table + " WHERE " + XMLField + "  = '" + DocNo + "') "
 272                                        + "BEGIN "
 273                                        + "  SELECT FILEID FROM XTDocuments WHERE DocumentID in (SELECT DocumentID FROM " + table + " WHERE " + XMLField + "  = '" + DocNo + "')"
 274                                        + "END "
 275                                        + "ELSE "
 276                                        + "BEGIN "
 277                                        + "  SELECT 1 "
 278                                        + "END", conn);
 279
 280                                        retVal = (cmd.ExecuteScalar() ?? "1").ToString();
 281                                        if (string.IsNullOrEmpty(retVal) || retVal == "1")
 282                                        {
 283                                            //Copy file to EFD Location
 284                                            file.MoveTo(System.IO.Path.Combine(serviceProperties.EFDExportPath, file.Name));
 285                                            //write log
 286                                            AuditLog(DocNo, DocumentFolder, DocumentCategory, mailAddress, fromAddress, "EFD Export");
 287                                            continue;
 288                                        }
 289                                    }
 290                                }
 291                                catch (Exception ex)
 292                                {
 293                                    log.Error("There was an error in the sql connection process.", ex);
 294                                }
 295                            }
 296                            #endregion
 297
 298                            #region Add WaterMark
 299                            if (!string.IsNullOrEmpty(waterMarkText)) //Check flag
 300                            {
 301                                string timeStamp = (DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + "_" + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond).ToString();
 302                                AddWatermarkText(TemplateName, System.IO.Path.Combine(serviceProperties.RepoPath, retVal), System.IO.Path.Combine(serviceProperties.EFDImportPath, $"{DocNo}#{toAddress}#{TemplateName}#{timeStamp}#false.pdf"), waterMarkText, null, 55f, BaseColor.RED, 0.3f, 45f);
 303                                file.Delete();
 304                            }
 305                            #endregion
 306                        }
 307                        catch (Exception ex)
 308                        {
 309                            log.Error("An error occurred. Moving files to rejected folder");
 310
 311                            file.MoveTo(outputPath + "\\Rejected\\" + file.Name);
 312                            //File.Move(outputPath + "\\Transaction Logs\\" + file.Name.Replace("pdf", "txt"), outputPath + "\\Rejected\\Transaction Logs\\" + file.Name.Replace("pdf", "txt"));
 313                            log.Error(ex.Message, ex);
 314                            AuditLog(DocNo, DocumentFolder, DocumentCategory, mailAddress, fromAddress, "REJECTED");
 315                        }
 316                    }
 317                }
 318
 319                #region EFD Import
 320                files = new DirectoryInfo(serviceProperties.EFDImportPath).GetFiles("*.pdf", SearchOption.TopDirectoryOnly);
 321                foreach (var file in files)
 322                {
 323                    try
 324                    {
 325                        #region Copy to Processed
 326                        if (!Directory.Exists(serviceProperties.EFDImportPath + "\\Processed\\")) Directory.CreateDirectory(serviceProperties.EFDImportPath + "\\Processed\\");
 327                        file.CopyTo(serviceProperties.EFDImportPath + "\\Processed\\" + file.Name, true);
 328                        #endregion
 329
 330                        #region Get Info
 331                        toAddress = string.Empty;
 332                        DocNo = string.Empty;
 333
 334                        var info = file.Name.Split('#');
 335
 336                        DocNo = info.Length > 0 ? info[0] : DocNo;
 337                        toAddress = info.Length > 1 ? info[1] : toAddress;
 338                        TemplateName = info.Length > 2 ? info[2] : DocumentFolder;
 339                        archive = info.Length > 4 ? bool.Parse(info[4].Replace(".pdf", "")) : archive;
 340
 341                        //Get Template Data
 342                        if (!GetTemplateValues(TemplateName))
 343                        {
 344                            log.Error("A template has not been setup for the folder " + TemplateName + " or the template is incorrect");
 345                        }
 346                        #endregion
 347
 348                        #region Send Via Email
 349                        try
 350                        {
 351                            if (!string.IsNullOrEmpty(toAddress) && !string.IsNullOrEmpty(serviceProperties.SmtpHost) && ValidateAddress(toAddress))
 352                            {
 353                                if (!Directory.Exists(outputPath + "\\Rejected\\")) Directory.CreateDirectory(outputPath + "\\Rejected\\");
 354
 355                                //Encrypt
 356                                log.Info("Preparing to encrypt files");
 357
 358                                log.Info(String.Format("Encrypting file '{0}'", file.FullName));
 359                                EncryptPDF(file.FullName);
 360
 361                                File.Delete(file.FullName);
 362                                File.Move(file.FullName.Replace(".pdf", ".nef"), file.FullName);
 363
 364                                //Send Encrypted file via Email
 365                                SendMail(toAddress, file.FullName, TemplateName);
 366                            }
 367                            else
 368                            {
 369                                log.Warn("A valid email address could not be retrieved from the file " + file.FullName);
 370                            }
 371                        }
 372                        catch (Exception ex)
 373                        {
 374                            log.Error("Email Failure, Skipping file due to the following exception: " + ex.Message, ex);
 375                            //Replace encrypted file with backed up file
 376                            File.Copy(serviceProperties.EFDImportPath + "\\Processed\\" + file.Name, file.FullName,  true);
 377                            continue;
 378                        }
 379                        #endregion
 380
 381                        #region Archive To Folder
 382                        //Archive item to Folder
 383                        try
 384                        {
 385                            string newDir = outputPath + "\\Success\\" + TemplateName;
 386                            if (!Directory.Exists(newDir)) Directory.CreateDirectory(newDir);
 387
 388                            string guid = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper();
 389                            if (archive)
 390                            {
 391                                if (!Directory.Exists(newDir + "\\Images")) Directory.CreateDirectory(newDir + "\\Images");
 392
 393                                if (!Directory.Exists(newDir + "\\xml")) Directory.CreateDirectory(newDir + "\\xml");
 394
 395                                if (string.IsNullOrEmpty(DocumentCategory)) DocumentCategory = TemplateName;
 396
 397                                if (string.IsNullOrEmpty(XMLField)) XMLField = "DocumentNo";
 398
 399                                using (StreamWriter xml = new StreamWriter(newDir + "\\xml\\" + file.Name.Replace(".pdf", ".xml")))
 400                                {
 401                                    xml.WriteLine("<BaseDocument>");
 402                                    xml.WriteLine("<" + XMLField + ">" + DocNo + "</" + XMLField + ">");
 403                                    xml.WriteLine("<SentTo>" + toAddress + "</SentTo>");
 404                                    xml.WriteLine("<DocumentCategory>" + DocumentCategory + "</DocumentCategory>");
 405                                    if (!string.IsNullOrEmpty(toAddress))
 406                                    {
 407                                        dicParser.Add("DocNo", DocNo);
 408                                        dicParser.Add("DocumentCategory", DocumentCategory);
 409                                        Subject = ExpressionParser(Subject ?? "", dicParser);
 410                                        xml.WriteLine("<Subject>" + Subject + "</Subject>");
 411                                        dicParser.Clear();
 412                                    }
 413                                    xml.WriteLine("</BaseDocument>");
 414                                }
 415                                newDir = newDir + "\\Images";
 416
 417                                string path = System.IO.Path.GetDirectoryName(file.FullName);
 418                                file.MoveTo(newDir + "\\" + file.Name);
 419                                GC.Collect();
 420                                //log.Debug($"TODO DELETE ME : BEFORE PATH {path}");
 421                                //Directory.Delete(path);
 422
 423                                //write log
 424                                AuditLog(DocNo, DocumentFolder, DocumentCategory, toAddress, fromAddress, "ARCHIVE");
 425                            }
 426                            else
 427                            {
 428                                file.Delete();
 429                            }
 430                        }
 431                        catch (Exception ex)
 432                        {
 433                            log.Error(ex.Message, ex);
 434                        }
 435                        #endregion Archive To Folder
 436                    }
 437                    catch (Exception ex)
 438                    {
 439                        log.Error(ex.Message, ex);
 440                    }
 441                }
 442                #endregion
 443
 444
 445                #endregion
 446
 447                #region TidyUp
 448                Thread.Sleep(100);
 449                directories = new DirectoryInfo(outputPath + "\\Processing").GetDirectories("*", SearchOption.TopDirectoryOnly);
 450                foreach (var dir in directories)
 451                {
 452                    try
 453                    {
 454                        if (dir.GetFileSystemInfos().Length == 0) dir.Delete();
 455                    }
 456                    catch (Exception ex)
 457                    {
 458                        log.Error(ex.Message, ex);
 459
 460                        files = new DirectoryInfo(dir.FullName).GetFiles(".nef");
 461                        foreach (var file in files)
 462                        {
 463                            try
 464                            {
 465                                file.Delete(); 
 466                                dir.Delete(); 
 467                            }
 468                            catch (Exception exc)
 469                            {
 470                                log.Error(exc.Message, exc);
 471                            }
 472                        }
 473                    }
 474                }
 475
 476                //files = new DirectoryInfo(outputPath + "\\Rejected").GetFiles("*", SearchOption.AllDirectories);
 477                //TODO: Remove Decrypting for now untill its fixed
 478                //foreach (var file in files)
 479                //{
 480                //    DecryptPDF(file.FullName);
 481                //    File.Delete(file.FullName);
 482                //    File.Move(file.FullName + "_Decyrpt", file.FullName);
 483                //}
 484                #endregion
 485            }
 486            catch (Exception ex)
 487            {
 488                log.Error(ex.Message, ex);
 489            }
 490        }
 491        #endregion
 492
 493        #region Methods
 494        public static bool ValidateAddress(string eMAilAddress)
 495        {
 496            if (!string.IsNullOrEmpty(eMAilAddress))
 497            {
 498                string[] arrAddresses = eMAilAddress.Split(';');
 499                foreach (var item in arrAddresses)
 500                {
 501                    if (!string.IsNullOrEmpty(item))
 502                    {
 503                        try
 504                        {
 505                            MailAddress m = new MailAddress(item);
 506                        }
 507                        catch (Exception ex)
 508                        {
 509                            log.Error(item + " is not a valid email address, error: " + ex.Message);
 510                            return false;
 511                        }
 512                    }
 513                    else
 514                    {
 515                        return false;
 516                    }
 517                }
 518                return true;
 519            }
 520
 521            return false;
 522        }
 523
 524        public static string ExtractTextFromPdf(string path, int pageNo)
 525        {
 526            try
 527            {
 528                PdfReader.unethicalreading = true;
 529                using (PdfReader reader = new PdfReader(path))
 530                {
 531                    PdfReader.unethicalreading = true;
 532
 533                    ITextExtractionStrategy Strategy = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
 534                    string pdftext = PdfTextExtractor.GetTextFromPage(reader, pageNo, Strategy);
 535
 536                    StringBuilder text = new StringBuilder();
 537                    //text.Append(PdfTextExtractor.GetTextFromPage(reader, pageNo).Replace('\n',' '));
 538                    foreach (string row in pdftext.Split('\n'))
 539                    {
 540                        if (row.Contains(serviceProperties.SplitChar))
 541                        {
 542                            text.Append(row.Replace('\n', ' '));
 543                        }
 544                    }
 545
 546                    return text.ToString();
 547                }
 548            }
 549            catch (Exception ex)
 550            {
 551                log.Error(ex.Message, ex);
 552                throw;
 553            }
 554        }
 555
 556        public static void SplitAndSave(string inputPath, string outputPath)
 557        {
 558            try
 559            {
 560                FileInfo file = new FileInfo(inputPath);
 561                //StringBuilder sbLog = new StringBuilder();
 562                //string errorTimeStamp = "[" + DateTime.Now + "]";
 563                PdfReader.unethicalreading = true;
 564                using (PdfReader reader = new PdfReader(inputPath))
 565                {
 566                    PdfReader.unethicalreading = true;
 567                    int pagenumber = 1;
 568                    //errorTimeStamp = "[" + DateTime.Now + "]";
 569                    for (pagenumber = 1; pagenumber <= reader.NumberOfPages; pagenumber++)
 570                    {
 571                        string timeStamp = (DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + "_" + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond).ToString();
 572                        try
 573                        {
 574                            GC.Collect();
 575                            Thread.Sleep(100);
 576                            string text = ExtractTextFromPdf(file.FullName, pagenumber);
 577                            if (string.IsNullOrEmpty(text))
 578                            {
 579                                log.Error("The file " + file.Name + " was not in the in a readable format, please ensure that the file is an image over text PDF file.");
 580                            }
 581                            if (!text.Contains(serviceProperties.SplitChar))
 582                            {
 583                                log.Error("The file " + file.Name + " did not contain the necessary start and end tags," + " please ensure that the file has the necessary start and end tags and try again.");
 584                            }
 585
 586                            string[] split = text.Split(new string[] { serviceProperties.SplitChar }, StringSplitOptions.None);
 587
 588                            //if (split.Length > 9)
 589                            //{
 590                            DocNo = split.Length > 1 ? split[1] : DocNo;
 591                            toAddress = split.Length > 3 ? split[3] : toAddress;
 592                            fromAddress = split.Length > 5 ? split[5] : fromAddress;
 593                            DocumentFolder = split.Length > 7 ? split[7] : DocumentFolder;
 594                            DocumentCategory = split.Length > 9 ? split[9] : DocumentCategory;
 595
 596                            //TODO check if any blank, so we can populate them with Defaults
 597
 598                            string filename = DocNo + "#" + toAddress + "#" + DocumentFolder + "#" + timeStamp + ".pdf";
 599
 600                            if (!string.IsNullOrEmpty(DocNo))
 601                            {
 602                                if (!Directory.Exists(outputPath + "\\Processing\\" + DocumentFolder + "-" + DocNo)) Directory.CreateDirectory(outputPath + "\\Processing\\" + DocumentFolder + "-" + DocNo);
 603
 604                                filename = System.IO.Path.Combine(outputPath, "Processing", DocumentFolder + "-" + DocNo, filename);
 605                            }
 606                            else
 607                            {
 608                                filename = outputPath + "\\rejected\\" + filename;
 609                            }
 610                            using (Document document = new Document())
 611                            {
 612                                if (!Directory.Exists(filename)) Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
 613                                PdfCopy copy = new PdfCopy(document, new FileStream(filename, FileMode.Create));
 614                                document.Open();
 615                                copy.AddPage(copy.GetImportedPage(reader, pagenumber));
 616                            }
 617                            if (string.IsNullOrEmpty(DocNo))
 618                            {
 619                                log.Error("A document number could not be retrieved from page " + pagenumber.ToString() + " of " + reader.NumberOfPages.ToString() + " of file " + file.Name);
 620                            }
 621                            else if ((string.IsNullOrEmpty(toAddress) || !ValidateAddress(toAddress)))
 622                            {
 623                                log.Warn("One or more eMail addresses on page " + pagenumber.ToString() + " of " + reader.NumberOfPages.ToString() + " of file " + file.Name + " is invalid");
 624                            }
 625
 626                            log.Info("Completed page extraction of page " + pagenumber.ToString() + " of " + reader.NumberOfPages.ToString() + " of file " + filename);
 627
 628                            //if (!Directory.Exists(outputPath + "\\Transaction Logs\\")) Directory.CreateDirectory(outputPath + "\\Transaction Logs\\");
 629
 630                            ////Write Tansactions Logs
 631                            //using (StreamWriter sw = new StreamWriter(outputPath + "\\Transaction Logs\\" + System.IO.Path.GetFileName(filename.Replace(".pdf", ".txt"))))
 632                            //{
 633                            //    sw.WriteLine(toAddress);
 634                            //    sw.WriteLine(DocNo);
 635                            //    sw.WriteLine(DocumentFolder);
 636                            //    sw.WriteLine(DocumentCategory);
 637                            //    sw.WriteLine(fromAddress);
 638                            //}
 639
 640                            //write log
 641                            AuditLog(DocNo, DocumentFolder, DocumentCategory, toAddress, fromAddress, "SPLIT - " + pagenumber + " of " + reader.NumberOfPages);
 642                            //}
 643                            //else
 644                            //{
 645                            //    log.Error("Page " + pagenumber.ToString() + " of " + reader.NumberOfPages.ToString() + " of file " + file.Name + " did not contain the correct formula");
 646                            //}
 647                        }
 648                        catch (Exception ex)
 649                        {
 650                            log.Error("Failed page extraction of page " + pagenumber.ToString() + " of " + reader.NumberOfPages.ToString() + " of file " + file.Name + " with error - " + ex.Message);
 651                        }
 652                    }
 653                }
 654            }
 655            catch (Exception ex)
 656            {
 657                log.Error(ex.Message, ex);
 658                throw;
 659            }
 660            //return sbLog.ToString();
 661        }
 662
 663        public static bool GetTemplateValues(string folderProcessed)
 664        {
 665            try
 666            {
 667                foreach (Template template in serviceProperties.Templates)
 668                {
 669                    if (template.TemplateName.Trim().ToLower() == folderProcessed.Trim().ToLower())
 670                    {
 671                        log.Info("Matching Template found, Populating values");
 672                        smtpUserName = template.SmtpUserName;
 673                        smtpPassword = template.SmtpPassword;
 674                        smtpFrom = template.SmtpFrom;
 675                        Body = template.Body;
 676                        Subject = template.Subject;
 677                        CC = template.CC;
 678                        DocumentCategory = string.IsNullOrWhiteSpace(DocumentCategory) ? template.DocumentCategory : DocumentCategory;
 679                        XMLField = template.XMLField;
 680                        embeddedImagePath = template.EmbeddedImagePath;
 681                        BCC = template.BCC;
 682                        replyToAddress = template.SmtpReplyTo;
 683                        waterMarkText = template.WatermarkPrint;
 684                        table = template.Table;
 685                        toAddress = string.IsNullOrWhiteSpace(toAddress) ? template.DefaultToEmail : toAddress;
 686
 687                        return true;
 688                    }
 689                }
 690            }
 691            catch (Exception ex)
 692            {
 693                log.Error(ex.Message, ex);
 694                return false;
 695            }
 696
 697            log.Error("No matching Template was found");
 698            return false;
 699        }
 700
 701        static byte[] GetBytes(string str)
 702        {
 703            try
 704            {
 705                byte[] bytes = new byte[str.Length * sizeof(char)];
 706                System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
 707                return bytes;
 708            }
 709            catch (Exception ex)
 710            {
 711                log.Error(ex.Message, ex);
 712                throw;
 713            }
 714        }
 715
 716        public static void DecryptPDF(string inputFile)
 717        {
 718            try
 719            {
 720                PdfReader.unethicalreading = true;
 721                using (PdfReader reader = new PdfReader(inputFile, GetBytes("D0cuV1s10n")))
 722                {
 723                    PdfReader.unethicalreading = true;
 724                    using (MemoryStream memoryStream = new MemoryStream())
 725                    {
 726                        using (PdfStamper stamper = new PdfStamper(reader, memoryStream))
 727                        {
 728                            stamper.SetFullCompression();
 729                            //File.Delete(inputFile);
 730                            File.WriteAllBytes(inputFile + "_decrypt", memoryStream.ToArray());
 731                        }
 732                    }
 733                }
 734            }
 735            catch (Exception ex)
 736            {
 737                log.Error(ex.Message, ex);
 738                throw;
 739            }
 740        }
 741
 742        public static void EncryptPDF(string input)
 743        {
 744            try
 745            {
 746                PdfReader.unethicalreading = true;
 747                using (PdfReader reader = new PdfReader(input))
 748                {
 749                    PdfReader.unethicalreading = true;
 750
 751                    if (File.Exists(input.Replace(".pdf", ".nef"))) File.Delete(input.Replace(".pdf", ".nef"));
 752                    using (FileStream fs = new FileStream(input.Replace(".pdf", ".nef"), FileMode.Create))
 753                    {
 754                        using (PdfStamper stamper = new PdfStamper(reader, fs))
 755                        {
 756                            try
 757                            {
 758                                stamper.SetEncryption(null, GetBytes("D0cuV1s10n"), PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_256);
 759                                stamper.SetFullCompression();
 760                                stamper.Reader.RemoveUnusedObjects();
 761                                stamper.FormFlattening = true;
 762                            }
 763                            catch (Exception ex)
 764                            {
 765                                //if (ex.Message == "PdfReader not opened with owner password")
 766                                //{
 767                                //    File.Delete(input.Replace(".pdf", ".nef"));
 768                                //    return;
 769                                //}
 770                                //File.Copy(input, input.Replace(".pdf", ".nef"));
 771                                log.Error(ex.Message, ex);
 772                            }
 773
 774                            //File.Delete(input);
 775                            //File.Move(input.Replace(".pdf", ".nef"), input);
 776                        }
 777                    }
 778                }
 779            }
 780            catch (Exception ex)
 781            {
 782                log.Error(ex.Message, ex);
 783                throw;
 784            }
 785        }
 786
 787        public static string MergePDF(string[] InFiles, string OutFile)
 788        {
 789            try
 790            {
 791                OutFile = OutFile + "-" + Guid.NewGuid().ToString().ToUpper().Replace("-", string.Empty);
 792                string response = string.Empty;
 793
 794                if (File.Exists(OutFile)) return OutFile; //+ " merged successfully 0x00001";
 795
 796                using (FileStream fileStream = new FileStream(OutFile, FileMode.Create))
 797                {
 798                    using (Document document = new Document())
 799                    {
 800                        using (PdfCopy pdfCopy = new PdfCopy(document, fileStream))
 801                        {
 802                            document.Open();
 803
 804                            for (int i = 0; i < InFiles.Length; i++)
 805                            {
 806                                try
 807                                {
 808                                    PdfReader.unethicalreading = true;
 809                                    using (PdfReader pdfReader = new PdfReader(InFiles[i]))
 810                                    {
 811                                        PdfReader.unethicalreading = true;
 812                                        for (int j = 0; j < pdfReader.NumberOfPages; j++)
 813                                        {
 814                                            pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, j + 1));
 815                                        }
 816                                        pdfCopy.FreeReader(pdfReader);
 817                                    }
 818                                }
 819                                catch (Exception ex)
 820                                {
 821                                    log.Error(string.Format("Failed to merge file : {0}, with error : {1}", InFiles[i], ex.Message));
 822                                }
 823                            }
 824                        }
 825                    }
 826                }
 827            }
 828            catch (Exception ex)
 829            {
 830                log.Error(ex.Message, ex);
 831            }
 832            return OutFile;
 833        }
 834
 835        public static void SendMail(string eMailAddress, string FileName, string templateCode)
 836        {
 837            FileInfo file = new FileInfo(FileName);
 838            using (SmtpClient smtp = new SmtpClient())
 839            {
 840                try
 841                {
 842                    string strName = Guid.NewGuid().ToString().ToUpper().Replace("-", string.Empty);
 843                    //if (!setMailValues(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\eMailer Templates.xml", templateCode))
 844                    //if (!GetTemplateValues(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\eMailer Templates.xml", templateCode))
 845                    //{
 846                    //    log.Error("A template has not been setup fo the folder " + templateCode + " or the template is incorrect");
 847                    //    return;
 848                    //}
 849
 850                    #region Normal
 851                    smtp.Port = smtpPort;
 852                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //TODO: Check if this should be parameterized
 853                    smtp.EnableSsl = ssl;
 854                    smtp.Timeout = smtpTimeOut;
 855                    smtp.Host = smtpHost;
 856
 857                    if (!string.IsNullOrEmpty(smtpPassword) & !string.IsNullOrEmpty(smtpUserName))
 858                    {
 859                        smtp.UseDefaultCredentials = false;
 860                        smtp.Credentials = new NetworkCredential(smtpUserName.TrimStart().TrimEnd(), smtpPassword.TrimStart().TrimEnd());
 861                    }
 862                    else
 863                    {
 864                        smtp.UseDefaultCredentials = true;
 865                    }
 866                    #endregion
 867
 868                    //if (!string.IsNullOrEmpty(config.AppSettings.Settings["TestMail"].Value))
 869                    if (!string.IsNullOrEmpty(serviceProperties.TestEmail))
 870                    {
 871                        //eMailAddress = config.AppSettings.Settings["TestMail"].Value;
 872                        eMailAddress = serviceProperties.TestEmail;
 873                    }
 874                    using (MailMessage mail = new MailMessage())
 875                    {
 876                        string[] arrAddresses = eMailAddress.Split(';');
 877                        if (arrAddresses.Count() != 0)
 878                        {
 879                            foreach (var item in arrAddresses)
 880                            {
 881                                if (!string.IsNullOrEmpty(item.ToString()) & !string.IsNullOrWhiteSpace(item.ToString()))
 882                                {
 883                                    mail.To.Add(new MailAddress(item.ToString()));
 884                                }
 885                            }
 886                        }
 887                        else
 888                        {
 889                            mail.To.Add(new MailAddress(eMailAddress));
 890                        }
 891
 892                        if (!string.IsNullOrEmpty(replyToAddress))
 893                        {
 894                            mail.ReplyToList.Add(new MailAddress(replyToAddress));
 895                        }
 896
 897
 898                        if (!string.IsNullOrEmpty(fromAddress))
 899                        {
 900                            //mail.Sender = new MailAddress(fromAddress);
 901                            try
 902                            {
 903                                mail.From = new MailAddress(fromAddress);
 904                                log.Info("From address : " + fromAddress);
 905                            }
 906                            catch (Exception ex)
 907                            {
 908                                log.Error("Invalid email address in From field found, " + fromAddress + "\n" + ex.Message);
 909                            }
 910                        }
 911                        else if (!string.IsNullOrEmpty(smtpFrom))
 912                        {
 913                            log.Warn("No From Address found, using the Template Default: " + smtpFrom);
 914                            mail.From = new MailAddress(smtpFrom);
 915                        }
 916
 917                        mail.IsBodyHtml = true; //TODO make this configurable
 918                        if (!string.IsNullOrEmpty(CC) & CC != " ")
 919                        {
 920                            string[] arrAddressesCC = CC.Split(';');
 921                            if (arrAddressesCC.Count() != 0)
 922                            {
 923                                foreach (var item in arrAddressesCC)
 924                                {
 925                                    if (!string.IsNullOrEmpty(item.ToString()) & !string.IsNullOrWhiteSpace(item.ToString()))
 926                                    {
 927                                        mail.CC.Add(new MailAddress(item.ToString()));
 928                                    }
 929                                }
 930                            }
 931                            else
 932                            {
 933                                mail.CC.Add(new MailAddress(CC));
 934                            }
 935                        }
 936
 937                        if (!string.IsNullOrEmpty(BCC) & BCC != " ")
 938                        {
 939                            string[] arrAddressesBCC = BCC.Split(';');
 940                            if (arrAddressesBCC.Count() != 0)
 941                            {
 942                                foreach (var item in arrAddressesBCC)
 943                                {
 944                                    if (!string.IsNullOrEmpty(item.ToString()) & !string.IsNullOrWhiteSpace(item.ToString()))
 945                                    {
 946                                        mail.Bcc.Add(new MailAddress(item.ToString()));
 947                                    }
 948                                }
 949                            }
 950                            else
 951                            {
 952                                mail.Bcc.Add(new MailAddress(BCC));
 953                            }
 954                        }
 955
 956                        mail.Attachments.Add(new Attachment(file.FullName));
 957                        //mail.Subject = Subject; //TODO: Create Text Parser, where we substitude values <##>
 958                        dicParser.Add("DocNo", DocNo);
 959                        dicParser.Add("DocumentCategory", DocumentCategory);
 960                        Subject = ExpressionParser(Subject ?? "", dicParser);
 961                        mail.Subject = Subject;
 962                        mail.Body = ExpressionParser(Body ?? "", dicParser);
 963
 964                        dicParser.Clear(); //TODO : resolve this better
 965
 966                        //if (File.Exists(Body))
 967                        if (!string.IsNullOrEmpty(embeddedImagePath) & embeddedImagePath != " ")
 968                        {
 969                            if (File.Exists(embeddedImagePath))
 970                            {
 971                                log.Info("Embedded Image found.");
 972
 973                                AlternateView avHtml = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);
 974                                // Create a LinkedResource object for the embedded image
 975                                LinkedResource pic1 = new LinkedResource(embeddedImagePath, MediaTypeNames.Image.Jpeg);
 976                                pic1.ContentId = "Pic1";
 977                                avHtml.LinkedResources.Add(pic1);
 978
 979                                // Add the alternate views instead of using MailMessage.Body
 980                                mail.AlternateViews.Add(avHtml);
 981                            }
 982                        }
 983
 984                        ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; //TODO: WTF is this???
 985
 986                        smtp.Send(mail);
 987                        log.Info(file.Name + " successfully sent to " + eMailAddress);
 988                        //sbLog.Append("[" + DateTime.Now + "] " + file.Name + " successfully sent to " + eMailAddress);
 989
 990                        //log.Debug($"TODO DELETE, BEFORE DELETE (EMAIL) {file.FullName.Replace(file.Name, strName)}");
 991                        //File.Delete(file.FullName.Replace(file.Name, strName)); 
 992
 993                        string To_Address = "";
 994                        foreach (var address in mail.To)
 995                        {
 996                            To_Address += address.Address + ";";
 997                        }
 998
 999                        AuditLog(DocNo, DocumentFolder, DocumentCategory, To_Address, mail.From.Address, "EMAIL");
1000                    }
1001                }
1002                catch (SmtpException ex)
1003                {
1004                    dicParser.Clear();
1005                    log.Error(file.Name + " failed to send to " + eMailAddress + " with the error - " + ex.Message, ex);
1006                    throw;
1007                    //sbLog.Append("[" + DateTime.Now + "] " + file.Name + " failed to send to " + eMailAddress + " with the error - " + ex.Message + "-" + ex.InnerException);
1008                }
1009                catch (Exception ex)
1010                {
1011                    dicParser.Clear();
1012                    log.Error(ex.Message, ex);
1013                    throw;
1014                    //sbLog.Append("[" + DateTime.Now + "] " + file.Name.Substring(file.Name.LastIndexOf("~") + 1).Replace(file.Extension, string.Empty) + " failed to send to " + eMailAddress + " with the error - " + ex.Message + "-" + ex.InnerException);
1015                }
1016            }
1017        }
1018
1019        public static string ExpressionParser(string inputString, Dictionary<string, string> arrValues)
1020        {
1021            string retVal = inputString;
1022
1023            try
1024            {
1025                //loop through keys 
1026                foreach (string item in arrValues.Keys)
1027                {
1028                    string replaceKey = string.Concat("<#", item, "#>");
1029                    string replaceVal = string.Empty;
1030                    if (inputString.Contains(replaceKey))
1031                    {
1032                        //Retrieve Values from Keys
1033                        arrValues.TryGetValue(item, out replaceVal);
1034                        retVal = retVal.Replace(replaceKey, replaceVal

Large files files are truncated, but you can click here to view the full file