Збереження MSG як PDF

Конвертувати лист у PDF

Наведений код показує конвертацію листа у PDF за допомогою Aspose.Email у поєднанні з Aspose.Words для Java. Це включає наступні кроки:

  1. Завантажити лист за допомогою MailMessage
  2. Зберегти лист у MemoryStream у форматі MHTML
  3. Завантажити потік за допомогою Aspose.Words
  4. Зберегти лист у PDF

Вихідний лист можна бачити наступним чином:

|todo:image_alt_text| | :- | |Фігура: Вихідний MSG файл |

|todo:image_alt_text| | :- | |Фігура: Конвертований PDF-файл | Java

static void emailToPdf(String emailPath, String pdfPath) throws Exception {
    MailMessage eml = MailMessage.load(emailPath);

    // Save the message to an output stream in MHTML format
    ByteArrayOutputStream emlStream = new ByteArrayOutputStream();
    eml.save(emlStream, SaveOptions.getDefaultMhtml());

    // Load the stream into a Word document; the Aspose.Words types are qualified
    // because com.aspose.email has a LoadOptions class of its own
    com.aspose.words.LoadOptions lo = new com.aspose.words.LoadOptions();
    lo.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
    com.aspose.words.Document doc = new com.aspose.words.Document(
            new ByteArrayInputStream(emlStream.toByteArray()), lo);

    // Save to disk
    doc.save(pdfPath, com.aspose.words.SaveFormat.PDF);

    // or save to a stream
    ByteArrayOutputStream foStream = new ByteArrayOutputStream();
    doc.save(foStream, com.aspose.words.SaveFormat.PDF);
}