diff --git a/src/clawPDF.Core/Actions/EMailClientAction.cs b/src/clawPDF.Core/Actions/EMailClientAction.cs index 24910a35..0356ebfc 100644 --- a/src/clawPDF.Core/Actions/EMailClientAction.cs +++ b/src/clawPDF.Core/Actions/EMailClientAction.cs @@ -1,7 +1,15 @@ using System; +using System.Text.RegularExpressions; +using iText.Kernel.Pdf.Canvas.Parser.Listener; using clawSoft.clawPDF.Core.Jobs; using clawSoft.clawPDF.Mail; +using iText.Kernel.Pdf; +using iText.Kernel.Pdf.Canvas.Parser; using NLog; +using System.Collections.Generic; +using PdfToSvg; +using Org.BouncyCastle.Cms; +using System.Linq; namespace clawSoft.clawPDF.Core.Actions { @@ -36,9 +44,27 @@ public ActionResult ProcessJob(IJob job) if (job.Profile.EmailClient.AddSignature) message.Body += _signatureText; - foreach (var recipient in job.Profile.EmailClient.Recipients.Replace(',', ';').Split(';')) - if (!string.IsNullOrWhiteSpace(recipient)) - message.To.Add(recipient.Trim()); + if (!job.Profile.EmailClient.Autodetect) + { + foreach (var recipient in job.Profile.EmailClient.Recipients.Replace(',', ';').Split(';')) + if (!string.IsNullOrWhiteSpace(recipient)) + message.To.Add(recipient.Trim()); + } + else + { + foreach (var file in job.OutputFiles) + { + if (file.Contains(".pdf")) + { + List emails = ExtractEmailsFromPdf(file); + List distinctemails = emails.Distinct().ToList(); + foreach (string email in distinctemails) + { + message.To.Add(email.Trim()); + } + } + } + } foreach (var file in job.OutputFiles) message.Attachments.Add(new Attachment(file)); @@ -70,6 +96,33 @@ public ActionResult ProcessJob(IJob job) } } + public static List ExtractEmailsFromPdf(string pdfPath) + { + var emails = new List(); + + using (PdfReader reader = new PdfReader(pdfPath)) + { + using (iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(reader)) + { + var pageCount = pdfDocument.GetNumberOfPages(); + var regexPattern = new Regex(@"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"); + + for (int i = 1; i <= pageCount; i++) + { + var pageContent = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(i), new SimpleTextExtractionStrategy()); + var matches = regexPattern.Matches(pageContent); + + foreach (Match match in matches) + { + emails.Add(match.Value); + } + } + } + } + + return emails; + } + public static bool CheckEmailClientInstalled() { var emailClientFactory = new EmailClientFactory(); diff --git a/src/clawPDF.Settings/EmailClient.cs b/src/clawPDF.Settings/EmailClient.cs index a9906a9a..50d39538 100644 --- a/src/clawPDF.Settings/EmailClient.cs +++ b/src/clawPDF.Settings/EmailClient.cs @@ -42,6 +42,11 @@ public EmailClient() /// public string Recipients { get; set; } + /// + /// Automatic detection of email(s) in the PDF document + /// + public bool Autodetect { get; set; } + /// /// Subject line of the E-mail /// @@ -94,6 +99,15 @@ public void ReadValues(Data data, string path) Recipients = ""; } + try + { + Autodetect = bool.Parse(data.GetValue(@"" + path + @"Autodetect")); + } + catch + { + Autodetect = true; + } + try { Subject = Data.UnescapeString(data.GetValue(@"" + path + @"Subject")); @@ -110,6 +124,7 @@ public void StoreValues(Data data, string path) data.SetValue(@"" + path + @"Content", Data.EscapeString(Content)); data.SetValue(@"" + path + @"Enabled", Enabled.ToString()); data.SetValue(@"" + path + @"Recipients", Data.EscapeString(Recipients)); + data.SetValue(@"" + path + @"Autodetect", Autodetect.ToString()); data.SetValue(@"" + path + @"Subject", Data.EscapeString(Subject)); } @@ -121,6 +136,7 @@ public EmailClient Copy() copy.Content = Content; copy.Enabled = Enabled; copy.Recipients = Recipients; + copy.Autodetect = Autodetect; copy.Subject = Subject; return copy; @@ -135,6 +151,7 @@ public override bool Equals(object o) if (!Content.Equals(v.Content)) return false; if (!Enabled.Equals(v.Enabled)) return false; if (!Recipients.Equals(v.Recipients)) return false; + if (!Autodetect.Equals(v.Autodetect)) return false; if (!Subject.Equals(v.Subject)) return false; return true; @@ -148,6 +165,7 @@ public override string ToString() sb.AppendLine("Content=" + Content); sb.AppendLine("Enabled=" + Enabled); sb.AppendLine("Recipients=" + Recipients); + sb.AppendLine("Autodetect=" + Autodetect); sb.AppendLine("Subject=" + Subject); return sb.ToString(); diff --git a/src/clawPDF/Views/ActionControls/EmailClientActionControl.xaml b/src/clawPDF/Views/ActionControls/EmailClientActionControl.xaml index c1380368..e7ef5634 100644 --- a/src/clawPDF/Views/ActionControls/EmailClientActionControl.xaml +++ b/src/clawPDF/Views/ActionControls/EmailClientActionControl.xaml @@ -10,6 +10,10 @@ + + Automatic recognition +