I need to convert a msg file that contains a long email chain to PDF. The problem is that because of the email’s blockquotes (previous responses) the PDF format (and Word format) becomes unreadable after few pages. The body of earlier emails turns into a column of single letters. Is there a way to handle the blockquotes? I’m basically interested in removing the extra space before/after the blockquotes. We are currently using Aspose.Words 22.12 and Aspose.Email 22.11. blockquote.png (21.8 KB)
blockquote-2.png (14.5 KB)
The code we use is below:
public static byte[] OutlookMailMessageToPDF(byte[] MessageMimeContent)
{
Aspose.Email.License myLic = new License();
myLic.SetLicense(AsposeLicense);
byte[] Returnval = new byte[] { 0 };
using (MemoryStream msgMS = new MemoryStream(MessageMimeContent))
{
Aspose.Email.Mapi.MapiMessage myMsg = Aspose.Email.Mapi.MapiMessage.Load(msgMS);
using (MemoryStream mhtmlMS = new MemoryStream())
{
myMsg.Save(mhtmlMS, mhtmlSaveOps);
Returnval = mhtmlMS.ToArray();
Returnval = WordDocHelper.WordDocToPDF(mhtmlMS.ToArray(), new WordToPDFConversionOptions()
{
FromMHTMLOrHTML = true
});
}
}
return Returnval;
}
public static byte[] WordDocToPDF(byte[] WordDoc, WordToPDFConversionOptions convertOps)
{
Aspose.Words.License myLic = new License();
myLic.SetLicense(AsposeLicense);
byte[] Returnval = new byte[] { 0 };
using (MemoryStream docMS = new MemoryStream(WordDoc))
{
Document myDoc = new Document(docMS);
//Handle putting the document to landscape or portrait
foreach (Section section in myDoc.Sections)
{
//Set the papersize we selected
foreach (PaperSize paperSize in Enum.GetValues(typeof(PaperSize)).Cast<PaperSize>())
{
if (paperSize.ToString() == convertOps.PaperSize.ToString())
{
section.PageSetup.PaperSize = paperSize;
}
}
//Check the orientation desired
section.PageSetup.Orientation = (convertOps.PortraitOrientation) ? Orientation.Portrait : Orientation.Landscape;
}
//If HTML, fix the tables for width
if(convertOps.FromMHTMLOrHTML)
{
foreach (Section section in myDoc.Sections)
{
//Try to Fix any and all tables
foreach (Aspose.Words.Tables.Table table in section.GetChildNodes(NodeType.Table, true))
{
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToWindow);
}
}
}
//Processing to do if the type of message we are converting is html or mhtml
if (convertOps.FromMHTMLOrHTML)
{
Aspose.Words.Layout.LayoutCollector myLayout = new Aspose.Words.Layout.LayoutCollector(myDoc);
Aspose.Words.Layout.LayoutEnumerator myLayoutEnum = new Aspose.Words.Layout.LayoutEnumerator(myDoc);
foreach (Section section in myDoc.Sections)
{
//Try to Fix any images
foreach (Aspose.Words.Drawing.Shape shape in section.GetChildNodes(NodeType.Shape, true))
{
if (shape.IsImage)
{
double targetWidth = 0;
bool Resize = false;
object renderObject = myLayout.GetEntity(shape);
myLayoutEnum.Current = renderObject;
double availableWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
if (shape.Width > availableWidth)
{
Resize = true;
targetWidth = availableWidth;
}
float Xrightmost = myLayoutEnum.Rectangle.Right;
float Xleftmost = myLayoutEnum.Rectangle.Left;
Aspose.Words.Rendering.PageInfo pageInfo = myLayoutEnum.Document.GetPageInfo(myLayoutEnum.PageIndex - 1);
if (Xrightmost > pageInfo.WidthInPoints)
{
Resize = true;
targetWidth = shape.Width - (Xrightmost - pageInfo.WidthInPoints);
}
if (Resize)
{
shape.Height = (targetWidth / shape.Width) * shape.Height;
shape.Width = targetWidth;
}
}
}
}
}
//Setup PDF Save options
Aspose.Words.Saving.PdfSaveOptions myOps = (Aspose.Words.Saving.PdfSaveOptions)Aspose.Words.Saving.PdfSaveOptions.CreateSaveOptions(SaveFormat.Pdf);
myOps.UseAntiAliasing = convertOps.UseAntiAliasing;
myOps.UseHighQualityRendering = convertOps.UseHighQualityRendering;
myOps.ImageCompression = Aspose.Words.Saving.PdfImageCompression.Jpeg;
myOps.JpegQuality = convertOps.JpegQuality;
myOps.PrettyFormat = true;
myOps.TextCompression = (convertOps.TextCompression == PDFTextCompression.FLATE) ? Aspose.Words.Saving.PdfTextCompression.Flate :
Aspose.Words.Saving.PdfTextCompression.None;
using (MemoryStream pdfMS = new MemoryStream())
{
myDoc.Save(pdfMS, myOps);
Returnval = pdfMS.ToArray();
}
}
return Returnval;
}