We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table and images are rendered outside the page after MHTML to PDF conversion using Java

Hi. I am converting msg files to a pdfs (in Java). Example code below.
But some images & tables are cut off at the right-hand edge.
How do I change the code below so that images & tables are not cut off in the PDF?

String sourceFile = “C:\TestFiles\15.msg”;
String outFile = “C:\TestFiles\Aspose\15.pdf”;
FileInputStream fstream = new FileInputStream(sourceFile);
MailMessage msg = MailMessage.load(fstream);
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
msg.save(msgStream, SaveOptions.getDefaultMhtml());
LoadOptions lo = new LoadOptions();
com.aspose.words.Document doc = new com.aspose.words.Document(new ByteArrayInputStream(msgStream.toByteArray()),lo);
doc.save(outFile, SaveFormat.PDF);



Can you please first try using latest Aspose.Email for Java 20.8 on your end along with latest version of Aspose.Words too. Can you please export the MSG to MHTML and see if there is issue in MHTML export by comparing it with actual MSG file. If the MHTML is rendered fine but output PDF has issue then its most likely Aspose.Words issue. However, if the MHTML output has issues then its an issue related to Aspose.Email. Please provide us with source MSG file, MHTML and exported PDF files generated on your end.

Tests 15 and 21.zip (615.3 KB)

Hi. The mhtml files are okay, so issue is with conversion to PDF.
I have attached files for 2 examples (msg, mhtml and pdf for each test).
One the image is cut off. The other a table is cut off.
Thanks for your help.

(Also - I am using the latest versions of Aspose)


With MHTML having no issues, then issue is likely in Aspose.Words API. I have moved this thread to concerned forum where our respective support team will assist you further in this regard.


Please use the following code example to get the desired output. Hope this helps you.

var doc = new Document(MyDir + @"21.mhtml");
PdfSaveOptions saveOptions = new PdfSaveOptions { OptimizeOutput = false };
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
doc.Save(MyDir + @"20.9.pdf"); 

public static void ResizebigImage(Shape image)
    // Return if this shape is not an image.
    if (!image.HasImage)

    // 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)
        // Set the new size.
        image.AspectRatioLocked = true;
        image.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

I translated the code above to Java and it’s working okay now. Thanks for help


Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.