Table in email cut partially

Hi team,

RE Automators October Primary Release - QA Sign Off Request.zip (27.3 KB)
is the sample file. We are using the following versions -

asposeCellsVersion=22.7
asposeEmailVersion=22.5:jdk16
asposePdfVersion=22.6
asposeWordsVersion=21.10:jdk17

With the following code -

public byte[] convertEmailToPdf(ByteArrayInputStream emailInputStream) throws Exception {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    try {
        log.info("Initiating Email to pdf conversion");
        MailMessage mailMsg = MailMessage.load(emailInputStream);
        // converting mailMsg to HTML (aspose doesn't support direct eml -> pdf conversion)
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //            mailMsg.save(baos, SaveOptions.getDefaultHtml()); // Commenting because I remember there were issues with MHTML conversions the last time.
        mailMsg.save(baos, SaveOptions.getDefaultMhtml());

        // loading MHTML into Aspose.Words Document
        LoadOptions lo = new LoadOptions();
        lo.setLoadFormat(LoadFormat.MHTML);
        Document document = new Document(new ByteArrayInputStream(baos.toByteArray()), lo);

        // finally saving Document as PDF.
        document.save(result, SaveFormat.PDF);
        return result.toByteArray();


    } catch (Exception ex) {
        log.error("Exception occurred while converting Email to pdf :", ex);
        throw ex;
    } finally {
        result.close();
    }
}

converted-file.pdf (23.6 KB)
is the converted file.

The converted file doesn’t cover the entire table from the original email.

Please help!

@shaurya.chawla The problem occurs because table has explicitly set width which does not fit the page size. You can work the problem around by autofit the table to window:

com.aspose.email.MailMessage mailMsg = com.aspose.email.MailMessage.load("C:\\Temp\\in.msg");
mailMsg.save("C:\\Temp\\out.mhtml", com.aspose.email.SaveOptions.getDefaultMhtml());

LoadOptions lo = new LoadOptions();
lo.setLoadFormat(LoadFormat.MHTML);
Document document = new Document("C:\\Temp\\out.mhtml", lo);

// fit tables in the document to window.
for(Table t : document.getFirstSection().getBody().getTables())
    t.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);

document.save("C:\\Temp\\out.pdf", SaveFormat.PDF);

This code produces the following output on my side: out.pdf (78.4 KB)

Thanks for your reply! I will try this out and report back!

1 Like