Email to PDF: Images are missing (C# .NET)

aspose truncates images in emails when converting to pdf.
included code :-
using (var docStream = ConvertStreamToMemoryStream(sm))
{
docStream.Position = 0;
var message = MailMessage.Load(docStream);

            var msgStream = new MemoryStream();
            message.Save(msgStream, SaveOptions.DefaultMhtml);
            msgStream.Position = 0;

            var loadOptions = new LoadOptions
            {
                LoadFormat = LoadFormat.Auto, 
                PreserveIncludePictureField = true
            };

            var doc = new Document(msgStream, loadOptions);
            var pdfStream = new MemoryStream();
            doc.Save(pdfStream, SaveFormat.Pdf);
            pdfStream.Position = 0;

            var output = new MemoryStream();
            var docPdf = new Aspose.Pdf.Document(pdfStream);
            docPdf.Convert(new MemoryStream(), PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
            docPdf.Info.ModDate = DateTime.Now;
            docPdf.Save(output, Aspose.Pdf.SaveFormat.Pdf);
            output.Position = 0;

            return output.ToArray();
        }

@mikesoffice,

I have observed the issue shared by you and request you to please first try using latest versions of Aspose.Email, Aspose.Words and Aspose.PDF on your end first. In case there is still an issue then please provide the source file and generated output PDF files with us on all stages. From Aspose.Email perspective, if you save message to MHTML file and it is rendered same as MSG file then the issue is most likely on Aspose.Words or Aspose.PDF end. However, if the MHTML has issue then it is Aspose.Email related issue and we will be able to log this on provision of requested information.

I’m currently using the latest version of all aspose products. I enclose said email and code snippet

RE Mikeys test1.1.zip (236.7 KB)

private byte[] ConvertEmailToPdf(Stream sm)
{
if (!CheckEmailFormat.Run(sm)) return null;

        _extension = "pdf";

        using (var docStream = ConvertStreamToMemoryStream(sm))
        {
            docStream.Position = 0;
            var message = MailMessage.Load(docStream);

            var msgStream = new MemoryStream();
            message.Save(msgStream, SaveOptions.DefaultMhtml);
            msgStream.Position = 0;

            var doc = new Document(msgStream, new LoadOptions { LoadFormat = LoadFormat.Mhtml });

            var pdfStream = new MemoryStream();
            doc.Save(pdfStream, SaveFormat.Pdf);
            pdfStream.Position = 0;

            var docPdf = new Aspose.Pdf.Document(pdfStream);
            docPdf.Convert(new MemoryStream(), PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
            var output = new MemoryStream();
            docPdf.Info.ModDate = DateTime.Now;
            docPdf.Save(output);
            output.Position = 0;

            return output.ToArray();
        }
    }

@mikesoffice,

I have worked with source file shared by you. For further investigation can you please share Mhtml file and generated pdf so that we may check if it is issue in Aspose.Email or any other API you are using.

The MHtml file is an in memory translation as per the code supplied not file to disk.RE Mikeys test1.pdf (223.7 KB)

I have attached the converted file

@mikesoffice,

I have tried saving the MSG to MHTML file using Aspose.Email for .NET and there is no issue in output. This MHTML serve as input for Aspose.Words to export to PDF further. I am moving this thread to Aspose.Words forum for further verification on Aspose.Words end since the output from Aspose.Email does not have an issue.

RE Mikeys test1.1.zip (243.2 KB)

@mikesoffice,

In your case, we suggest you please use the following code example to resize the big size images. We have attached the output PDF with this posts for your kind reference. 19.6.pdf (403.3 KB)

Document doc = new Document(MyDir + "RE Mikeys test1.1.mhtml");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
        ResizeLargeImage(shape);
doc.Save(MyDir + "19.6.pdf");

public static void ResizeLargeImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

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


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        Boolean widthLonger = (size.WidthPoints > size.HeightPoints);
        double ratio = widthLonger ? freePageWidth / size.WidthPoints : freePageHeight / size.HeightPoints;

        if (ratio > .90)
            ratio = ratio - .10;
        // Set the new size.
        image.Width = size.WidthPoints * ratio;
        image.Height = size.HeightPoints * ratio;
    }
}