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

Free Support Forum - aspose.com

Conversion from MSG to PDF causes text to be cut off

We recently upgraded from aspose-email 18.4 to 20.5 and aspose-words from 18.5 to 20.6. We are using the java api for converting email .msg files to PDF using the aspose email and aspose words.
We found that when we convert MSG files to PDF the final PDF output has the text and images cut-off from the right and left side. The same emails are being correctly converted using the earlier versions of aspose email and aspose words.
Could you please suggest any solution.

@akstech.ghawate,
Welcome to our community! Thank you for the issue description. Could you please share a code snippet with Aspose.Email and the MSG file that can be used to investigate the problem? Please also share the PDF output containing the text and image cut-off.

Hi Andrey,

The code snippet used to convert MSG file to PDF is exactly the same as provided in the Aspose documentation https://docs.aspose.com/email/java/save-email-message-as-pdf/

Unfortunately I cannot share the MSG file due to the data sensitivity and organizational constraints. However, below are the steps to reproduce the issue.

  1. Create a new Outlook email and in the email body create a table structure with an outer table containing two inner tables, the first one with an image and the second one with text. You can refer to the table layout in the attached file. Email_layout.pdf (181.2 KB)
  2. Use the above code snippet to convert the MSG file as PDF

@akstech.ghawate,
To investigate the issue correctly, we have to ask you your own MSG file sample and PDF output.

Hi Andrey,

I was able to reproduce the problem with few test emails I created. Please find the MSG file and PDF output files below.
Test_Files.zip (670.7 KB)

@akstech.ghawate

I have tried your MSG files. One of them is corrupt and cannot even be opened in MS Outlook. However, for other one I have exported to MHTML and there is no issue. The cropping of right hand side happens when you export MHTML to PDF using Aspose.Words and and this issue is related to Aspose.Words. I suggest you to please visit the following thread as well where similar issue was reported same days ago and solution was proposed in Aspose.Words for resolution.

public static void TestMSGtoPDF()
{
    String path="/Users/mudassirkhan/Downloads/Test_Files/";
    MailMessage mailMessage = MailMessage.load(path+"Test_mail2.msg", new MsgLoadOptions());
    MhtSaveOptions saveOptions=new MhtSaveOptions();
 
    saveOptions = SaveOptions.getDefaultMhtml();
    mailMessage.save(path+"Saved.mhtml",saveOptions);
    
     
}

Saved.zip (177.4 KB)

@ mudassir.fayyaz
I have tried testing with the below code snippet to resize the image but it still does not resolve the issue with the text being cut off the edges.
Output: image_resize_output.zip (378.1 KB)

Code snippet:
//Resize images in document
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : (Iterable) shapes) {
if (shape != null) {
resizeBigImage(shape);
}
}

private static void resizeBigImage(Shape shape) throws Exception {
// Return if this shape is not an image.
if (!shape.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 = shape.ParentParagraph.ParentSection.PageSetup;
    PageSetup ps = shape.getParentParagraph().getParentSection().getPageSetup();
    double freePageWidth = shape.isInline() ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin() : ps.getPageWidth();
    double freePageHeight = shape.isInline() ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin() : ps.getPageHeight();
    
    ImageSize size = shape.getImageData().getImageSize();
    boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight
            || shape.getWidth() > freePageWidth || shape.getHeight() > freePageHeight;
            
    if(exceedsMaxPageSize){
    	// Set the new size.
        shape.setAspectRatioLocked(true);
        shape.setWidth(ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin());
    }
}

@akstech.ghawate

I am moving this thread to Aspose.Words forum where our concerned team will assist you further in this regard.

@akstech.ghawate,

I have converted the MHTML file “Saved.mhtml” to PDF format by using “Save As” command of MS Word 2016 (on Windows 10) and attached the PDF file here for your reference:

You can observe similar issues in this above PDF. Aspose.Words is designed to mimic the behavior of MS Word. In your case, you need to adjust content (reduce font size of content, reduce Table/Image sizes etc) so that it fits within the default Page size. Or you can adjust the width/height of Page using PageSetup.PageWidth, PageSetup.PageHeight properties (or use PageSetup.Orientation property). For this case, you can also try following option:

Document doc = new Document("C:\\temp\\Saved\\Saved.mhtml");

for (Table table : (Iterable<Table>) doc.getChildNodes(NodeType.TABLE, true))
    table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);

// resize images etc

doc.save("C:\\Temp\\Saved\\awjava-21.2.pdf");

A post was split to a new topic: Converting MSG file to (MHTML to ) PDF, text / table are cut off on right side