Error displaying image in MSG

Hi team,

When using Aspose.Email to open an MSG file and save it as a Word or PDF document, the attached image appears incomplete. The image is oversized and only partially visible, with the rest of the image extending beyond the margins.

I am attaching the MSG file and how it appears when saved as a PDF.

MsgSample.zip (166.9 KB)

MSG-to-PDF.pdf (226.3 KB)

Thank you in advance for your assistance.

Hello @gabriel.vega,

Could you please provide the code example you’re using to save the message as a PDF? This will help us better understand the issue and provide more specific assistance.

Thank you.

Hi @margarita.samodurova,

I am using the same code example from Aspose documentation.

string dataDir = RunExamples.GetDataDir_KnowledgeBase();
MailMessage mailMsg = MailMessage.Load(dataDir + "message3.msg");
MemoryStream ms = new MemoryStream();
mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);

var loadOptions = new Aspose.Words.Loading.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;

var document = new Aspose.Words.Document(ms, loadOptions);

var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save(dataDir + "SaveEmailAsPDF_out.pdf", pdfSaveOptions);

var docSaveOptions = new Aspose.Words.Saving.DocSaveOptions();
document.Save(dataDir + "SaveEmailAsDOC_out.doc", docSaveOptions);

The same problem with the image being oversized and only partially visible happens when saving in Doc and PDF format.

Hello @gabriel.vega,

The issue you’re experiencing is not related to Aspose.Email, as the MHTML displays correctly. This indicates that the problem lies in the conversion process handled by Aspose.Words.

Therefore, we direct your query to the Aspose.Words forum, where our colleagues can provide you with the appropriate guidance and solution.

@gabriel.vega The problem occurs because the image is too wide and does not fit the default page size. You can adjust page size to avoid image cropping. For example see the following code:

Aspose.Email.MailMessage msg = Aspose.Email.MailMessage.Load("C:\\Temp\\in.msg");
msg.Save("C:\\Temp\\tmp.mhtml", Aspose.Email.SaveOptions.DefaultMhtml);

Document doc = new Document("C:\\Temp\\tmp.mhtml");

// Calculate maximum image width and height.
double maxShapeWidth = 0;
double maxShapeHeight = 0;
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
    if (s.IsTopLevel)
    {
        maxShapeWidth = Math.Max(maxShapeWidth, s.Width);
        maxShapeHeight = Math.Max(maxShapeHeight, s.Height);
    }
}

PageSetup ps = doc.FirstSection.PageSetup;
double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
double pageHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

if (pageWidth < maxShapeWidth)
    ps.PageWidth = maxShapeWidth + ps.LeftMargin + ps.RightMargin;
if (pageHeight < maxShapeHeight)
    ps.PageWidth = maxShapeHeight + ps.TopMargin + ps.BottomMargin;

doc.Save("C:\\Temp\\out.pdf");