บันทึกไฟล์ MSG เป็น PDF

แปลงข้อความอีเมลเป็น PDF

โค้ดต่อไปนี้แสดงการแปลงข้อความอีเมลเป็น PDF ด้วยการใช้ Aspose.Email ร่วมกับ Aspose.Words for 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);
}