diff --git a/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs b/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs index c3998d3b9..1354373dc 100644 --- a/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs +++ b/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using Syncfusion.Pdf.HtmlToPdf; using System.ComponentModel.DataAnnotations; +using System; namespace MCS.FOI.MSGToPDF { @@ -102,6 +103,7 @@ public MSGFileProcessor(Stream sourceStream) fileNameHash.Add(filename, true); attachmentInfo.Add("filename", _attachment.FileName); attachmentInfo.Add("s3filename", filename); + attachmentInfo.Add("cid", _attachment.ContentId); attachmentInfo.Add("size", _attachment.Data.Length.ToString()); attachmentInfo.Add("lastmodified", _attachment.LastModificationTime.ToString()); attachmentInfo.Add("created", _attachment.CreationTime.ToString()); @@ -109,32 +111,12 @@ public MSGFileProcessor(Stream sourceStream) } } } - - //msg.CharsetDetectionEncodingConfidenceLevel = 0.7f; if (msg.BodyRtf != null) { - //using var fs0 = new FileStream("C:\\folder\\log.txt", FileMode.Create, FileAccess.Write); var msgReader = new Reader(); var inputStream = new MemoryStream(); - //SourceStream.CopyTo(inputStream); - //List result = msgReader.ExtractToStream(inputStream, true); - - - //using var htmlfs = new FileStream("C:\\folder\\test.txt", FileMode.Create, FileAccess.Write); - //result[0].WriteTo(htmlfs); - //string bin = BitConverter.ToString(((Storage.Attachment)msg.Attachments[0]).Data).Replace("-", string.Empty); - //Console.WriteLine(bin); string body = msgReader.ExtractMsgEmailBody(SourceStream, ReaderHyperLinks.Both, "text/html; charset=utf-8", false); - //var ms = new MemoryStream(); - //var success = false; - //(ms, success) = ConvertHTMLtoPDF(body, ms); - - //using var fs1 = new FileStream("C:\\folder\\blinkconverted.pdf", FileMode.Create, FileAccess.Write); - using var fs2 = new FileStream("C:\\folder\\syncfusionconverted.pdf", FileMode.Create, FileAccess.Write); - //using var fs3 = new FileStream("C:\\folder\\syncfusionconverted.docx", FileMode.Create, FileAccess.Write); - //using var fs4 = new FileStream("C:\\folder\\test.txt", FileMode.Create, FileAccess.Write); - //ms.WriteTo(fs1); var bodyreplaced = Regex.Replace(Regex.Replace(body.Replace("
", "
").Replace("", "").Replace("", ""), "=(?(?!utf-8)[\\w|-]+)", "=\"${tagname}\""), "", ""); const string rtfInlineObject = "[*[RTFINLINEOBJECT]*]"; const string imgString = "", ""); + foreach (KeyValuePair> attachment in attachmentsObj) + { + if (attachment.Value["cid"] == inlineAttachment.ContentId) + { + attachmentsObj.Remove(attachment.Key); + } + } } else if (rtfInline) { - //using var fs5 = new FileStream("C:\\folder\\image.png", FileMode.Create, FileAccess.Write); - //fs5.Write(inlineAttachment.Data, 0, inlineAttachment.Data.Length); - bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, ""); - //var string1 = Convert.ToBase64String(inlineAttachment.Data); - //var string2 = BitConverter.ToString(inlineAttachment.Data).Replace("-", string.Empty); + if (inlineAttachment.OleAttachment) + { + bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, ""); + foreach (KeyValuePair> attachment in attachmentsObj) + { + if (attachment.Value["filename"] == inlineAttachment.FileName) + { + attachmentsObj.Remove(attachment.Key); + } + } + } + else + { + bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, " [**Inline Attachment - " + inlineAttachment.FileName + "**]"); + } } - } + } } - + byte[] byteArray = Encoding.ASCII.GetBytes(bodyreplaced); using (MemoryStream messageStream = new MemoryStream(byteArray)) { @@ -210,6 +206,26 @@ public MSGFileProcessor(Stream sourceStream) var regex = new Regex(@"(\r)*(\n)*(\t)+", RegexOptions.Multiline); var occurences = rtfDoc.Replace(regex, "\r\n"); + List pictures = rtfDoc.FindAllItemsByProperty(EntityType.Picture, "", ""); + + foreach (WPicture picture in pictures) + { + picture.LockAspectRatio = true; + const float maxSize = 500; + if (picture.Height > maxSize && picture.Height >= picture.Width) + { + var scale = (maxSize / picture.Height) * 100; + picture.HeightScale = scale; + picture.WidthScale = scale; + } + if (picture.Width > maxSize) + { + var scale = (maxSize / picture.Width) * 100; + picture.HeightScale = scale; + picture.WidthScale = scale; + } + } + //Gets all the hyperlink fields in the document List fields = rtfDoc.FindAllItemsByProperty(EntityType.Field, "FieldType", FieldType.FieldHyperlink.ToString()); @@ -311,6 +327,8 @@ public MSGFileProcessor(Stream sourceStream) //bool isConverted; //(output, isConverted) = ConvertHTMLtoPDF(htmlString, output); + + break; }