Unable to shrink images in document converted from email

Hi, we are converting emails to pdf via aspose.email > aspose.words > aspose.pdf. This works well and we are able to get most of the content displaying very well. However, inline images which are wider than the page are being cut off. The accepted way of shrinking images to fit the page width in aspose.words does not work for us. We can verify that everything seems to work as required, but the resultant pdf does not show the image in the width we specify. Even when shrinking it to a “hardcoded” number like 100 points it still has no effect.

Is there a way to shrink the images to fit width wise?

See code (bold lines have no effect on the resultant pdf):

private static Byte[] PdfEnhancedByteArray(Email email, TimeZoneInfo workTimeZoneInfo, ExchangeListenerCollection.EmailAttachmentOptions attachmentOption)
{
    // Uses Aspose.Email, Aspose.Words and Aspose.Pdf 

    // Convert MSG to MHTML - Use UTF8
    Aspose.Email.Mail.MailMessage mailMsg = Aspose.Email.Mail.MailMessage.Load(email.OriginalEmail, new Aspose.Email.Mail.MhtmlLoadOptions { PrefferedTextEncoding = Encoding.UTF8, PreserveTnefAttachments = true });
    // Use the Offset to display the correct time
    mailMsg.TimeZoneOffset = workTimeZoneInfo.GetUtcOffset(DateTime.Now);
    using (var ms = new MemoryStream())
    {
        try
        {
            mailMsg.Save(ms, Aspose.Email.Mail.SaveOptions.DefaultMhtml);
        }
        catch (SystemException ex)
        {
            Logging.Error("Could not convert email", ex);
            throw ex;
        }

        // Convert MHTML to PDF

        // create an instance of Document and load the MTHML from MemoryStream
        Aspose.Words.Document document = new Aspose.Words.Document(ms, new Aspose.Words.LoadOptions { LoadFormat = Aspose.Words.LoadFormat.Mhtml });

        // Format the document - smaller margins
        foreach (Aspose.Words.Section section in document)
        {
            section.PageSetup.LeftMargin = 20;
            section.PageSetup.RightMargin = 20;
            section.PageSetup.TopMargin = 20;
            section.PageSetup.BottomMargin = 20;
            section.PageSetup.HeaderDistance = 10;
            section.PageSetup.FooterDistance = 10;
        }

        // Prevent Tables from being cut off if the are too wide to fit to the page 
        foreach (Aspose.Words.Tables.Table tableNode in document.GetChildNodes(Aspose.Words.NodeType.Table, true))
        {
            tableNode.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPercent(100);
        }

        // TODO: Note, resizing does not work, so we are adding the inline images that are too wide to the pdf - revisit…
        // Prevent embedded images from being cut off if the are too wide to fit to the page
        foreach (Aspose.Words.Drawing.Shape shape in document.GetChildNodes(Aspose.Words.NodeType.Shape, true))
        {
            if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.Image)
            {
                // If images needs to be shrunk then scale to fit (-40 for margins)
                if (document.GetPageInfo(0).WidthInPoints < shape.Width)
                {

                    double scale = (document.GetPageInfo(0).WidthInPoints - 40) /
                    shape.Width;
                    **shape.Width = document.GetPageInfo(0).WidthInPoints - 40; **
                        **shape.Height = shape.Height * scale; **
                }
            }
        }

        using (var myPdfStream = new MemoryStream())
        {
            byte[] returnArray;
            // save the document to Pdf Stream
            document.Save(myPdfStream, new Aspose.Words.Saving.PdfSaveOptions());

            // Add Attachments to final PDF
            if (attachmentOption == ExchangeListenerCollection.EmailAttachmentOptions.AddToEmailBody && email.Attachments.Count > 0)
            {
                // Create pdf
                var objPdf = new Aspose.Pdf.Document(myPdfStream);

                foreach (var attachment in email.Attachments)
                {
                    if (!attachment.IsInlineContent)
                    {
                        var fileSpec = new Aspose.Pdf.FileSpecification(attachment.GetStream(), attachment.Name);
                        objPdf.EmbeddedFiles.Add(fileSpec);
                    }
                    else
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromStream(attachment.GetStream());
                        if (img.Width > document.GetPageInfo(0).WidthInPoints - 40)
                        {
                            // Images aren’t getting scaled properly - so embed them to the pdf if they are too large
                            var fileSpec = new Aspose.Pdf.FileSpecification(attachment.GetStream(), attachment.Name);
                            objPdf.EmbeddedFiles.Add(fileSpec);
                        }

                    }
                }

                objPdf.Save(myPdfStream);
            }

            return myPdfStream.ToArray();

        }
    }
}

Hi Chris,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input email file
  • The intermediate Aspose.Email generated MHTML file
  • Aspose.Words 16.1.0 generated output PDF file showing the undesired behavior

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi

Appreciate your response - Here you go:
Original Email: RE Return to Queue Report.msg
EmailMessage: AsposeMailMessage.bin
Intermediary Docx: AsposeDocument.docx (this shows the images fit to width properly)
Pdf: Email _ RE_ Return to Queue Report.pdf (This shows 2 of the images being cut off)

So, it looks like it does fit the images to the correct width in the word doc, but then between the word doc and the pdf it goes back to the original width.

Thank you.

Hi Chruzi,

I had the same problem and I discovered that document.GetPageInfo(0).WidthInPoints causes the problem.

I’ve replaced it with “document.Sections[0].PageSetup.PageWidth”

Please let me know if it worked for you too.

Best regards,

Dragos

Brilliant! That has sorted it out for me.

Thanks so much for helping - really appreciate it.

Hi Chris,

Thanks for your inquiry. It appears that the problem occurs because you’r using a very old version of Aspose.Words for .NET i.e. 13.1.0 on your end. Do you see the same problem with Aspose.Words for .NET 16.1.0 when converting your “AsposeDocument.docx” to PDF format using the code from here?

Best regards,