Convert specific .eml result in strange inline image

Hi Aspose Team,

We have specific .eml which results in an exception when getting child nodes of the Shape type.
We do this because sometimes we need to resize inline images to fit a normal A4 size PDF page.
If we handle the exception and skip the image resize pass the resulting PDF seems fine, but one of the inline images is stretched beyond recognition.
If we open the .eml in Outlook, the inline-images seems fine there.
Is this something you could investigate on your side.

Here’s the zipped .eml file:
unnamed_attachment_1.zip (27.0 KB)

And here is sample code:

Aspose.Email.MailMessage emailDoc = Aspose.Email.MailMessage.Load(filename);

emailDoc.TimeZoneOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

MemoryStream htmlStream = new MemoryStream();

MhtSaveOptions mhtSaveOptions = new MhtSaveOptions()
{
    CheckBodyContentEncoding = true,
    PreserveOriginalBoundaries = true,
    MhtFormatOptions =
        MhtFormatOptions.WriteHeader |
        MhtFormatOptions.WriteCompleteFromEmailAddress |
        MhtFormatOptions.WriteCompleteToEmailAddress |
        MhtFormatOptions.WriteCompleteCcEmailAddress |
        MhtFormatOptions.WriteCompleteBccEmailAddress |
        MhtFormatOptions.HideExtraPrintHeader |
        MhtFormatOptions.RenderVCardInfo
};

emailDoc.Save(htmlStream, mhtSaveOptions);

Aspose.Words.Loading.LoadOptions loadOptions = new Aspose.Words.Loading.LoadOptions()
{
    LoadFormat = Aspose.Words.LoadFormat.Mhtml,
};

var wordDoc = new Aspose.Words.Document(htmlStream, loadOptions);


foreach (Aspose.Words.Drawing.Shape shape in wordDoc.GetChildNodes(NodeType.Shape, true))
{
    // Return if this shape is not an image.
    if (!shape.HasImage)
        return null;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    Aspose.Words.PageSetup ps = shape.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = shape.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = shape.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;

    Aspose.Words.Drawing.ImageSize size = shape.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight || shape.Width > freePageWidth || shape.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Set the new size.
        shape.AspectRatioLocked = true;
        shape.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
    }
}

if (File.Exists(dstFilename))
    dstFilename = Utilities.CreateUniqueFilename(dstFilename);

var saveOptions = new Aspose.Words.Saving.PdfSaveOptions() { };
wordDoc.Save(dstFilename, saveOptions);

@andersalvsaker

It seems you are encountering an issue with inline images when converting a specific .eml file to PDF using Aspose. The problem arises when trying to resize these images, leading to exceptions and distorted images in the resulting PDF.

To address the issue of inline images being stretched, it is important to ensure that you call the Document.updatePageLayout() method after modifying the image sizes but before saving the document to PDF. This method updates the layout of the document, which is crucial when changes have been made to the document structure, such as resizing images.

Here’s a modified version of your code that includes the call to updatePageLayout():

Aspose.Email.MailMessage emailDoc = Aspose.Email.MailMessage.Load(filename);
emailDoc.TimeZoneOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

MemoryStream htmlStream = new MemoryStream();

MhtSaveOptions mhtSaveOptions = new MhtSaveOptions()
{
    CheckBodyContentEncoding = true,
    PreserveOriginalBoundaries = true,
    MhtFormatOptions =
        MhtFormatOptions.WriteHeader |
        MhtFormatOptions.WriteCompleteFromEmailAddress |
        MhtFormatOptions.WriteCompleteToEmailAddress |
        MhtFormatOptions.WriteCompleteCcEmailAddress |
        MhtSaveOptions.WriteCompleteBccEmailAddress |
        MhtFormatOptions.HideExtraPrintHeader |
        MhtFormatOptions.RenderVCardInfo
};

emailDoc.Save(htmlStream, mhtSaveOptions);

Aspose.Words.Loading.LoadOptions loadOptions = new Aspose.Words.Loading.LoadOptions()
{
    LoadFormat = Aspose.Words.LoadFormat.Mhtml,
};

var wordDoc = new Aspose.Words.Document(htmlStream, loadOptions);

foreach (Aspose.Words.Drawing.Shape shape in wordDoc.GetChildNodes(NodeType.Shape, true))
{
    if (!shape.HasImage)
        continue;

    Aspose.Words.PageSetup ps = shape.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = shape.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = shape.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;

    Aspose.Words.Drawing.ImageSize size = shape.ImageData.ImageSize;
    bool exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight || shape.Width > freePageWidth || shape.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        shape.AspectRatioLocked = true;
        shape.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
    }
}

// Update the layout before saving to PDF
wordDoc.UpdatePageLayout();

if (File.Exists(dstFilename))
    dstFilename = Utilities.CreateUniqueFilename(dstFilename);

var saveOptions = new Aspose.Words.Saving.PdfSaveOptions() { };
wordDoc.Save(dstFilename, saveOptions);

By including the wordDoc.UpdatePageLayout(); line, you should see an improvement in how the inline images are rendered in the final PDF. If the issue persists, it may be beneficial to report it to Aspose’s support team for further investigation, especially if it relates to specific .eml files.

For more information on saving emails as PDFs, you can refer to the Aspose documentation on this topic here.

If you have any further questions or need additional assistance, feel free to ask!

@andersalvsaker Please try modifying your code like this:

if (exceedsMaxPageSize)
{
    // Set the new size.
    shape.AspectRatioLocked = true;
    if(shape.Width>shape.Height)
        shape.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
    else
        shape.Height = ps.PageHeight - ps.TopMargin - ps.BottomMargin;
}

The problem occurs because in your case when shapes width is set with AspectRatioLocked enabled, height if the resulting shape is greater than maximum allowed shape size 1584 points.

1 Like

Thanks !
This helps overcome the exception thing.
But the second inline-image has a very odd Height value in code. Which might seem like a bug or a problem. It could possibly be the .eml itself but Outlook can display it properly.
If you expect the result PDF and look at the last image on page 4, this is a very stretched out company logo. The same logo is presented on page 2 and has the correct aspect ratio.

If I manually force the AspectRatioLocked to false and then set an aprox width heigh = 120 or something. The image will be rendered more close to the wanted result.
So, could it be something in the Aspose interpretation of eml/html data which result in a Shape/Image which has very off height value ? Because it is really big like 650 something and with is only 120 or something.

Thanks in advance for investigating.
Kind Regards,
Anders

@andersalvsaker It looks like the problem is in the MHTML produced by Aspose.Email. My colleagues will take a look at the issue shortly.

1 Like

@sergey.vivsiuk Could you please take a look from Aspose.Email point of view.

Hello @andersalvsaker ,

Please check the original HTML in the MIME message body. The style height for the second logo has an incorrect value. It is set as “height:10in”.

-- LOGO1 height:1.0in
<img border="0" width="167" height="96" id="_x0000_i1026" src="cid:image001.png@01DA2434.41EC5E80" alt="cid:image001.png@01D8B27A.3EDEE960" style="width:1.7416in; height:1.0in">

-- LOGO2 - height:10in
<img border="0" width="167" height="96" id="Bilde_x0020_77818895" src="cid:image001.png@01DA2434.41EC5E80" style="width:1.7416in; height:10in">
1 Like

Thank you for investigating this !