MSG 파일을 PDF로 저장
Contents
[
Hide
]
이 문서는 Aspose.Email을 사용해 이메일 메시지를 PDF로 변환하는 방법을 보여줍니다. Aspose.Email for Java는 Microsoft Outlook 기능을 다루며 PDF로 직접 변환할 수 없습니다. 이를 해결하기 위해 이 샘플은 Aspose.Email을 사용해 이메일 메시지를 MHTML 스트림으로 변환한 다음 Aspose.Words for Java로 해당 스트림을 로드해 PDF로 저장합니다.
이메일 메시지는 첨부 파일을 포함할 수도 있습니다. 첨부 파일마다 미디어 유형이 다를 수 있기 때문에 Aspose.Email은 MHTML로 변환할 때 이러한 첨부 파일을 무시합니다. 즉, 메시지의 인라인 이미지만 MHTML에 포함되고 일반 첨부 파일은 무시됩니다.
이메일 메시지를 PDF로 변환
다음 코드는 Aspose.Email과 Java용 Aspose.Words를 결합하여 이메일 메시지를 PDF로 변환하는 방법을 보여줍니다. 다음 단계가 포함됩니다:
- MailMessage를 사용해 이메일 메시지 로드
- 이메일 메시지를 MemoryStream에 MHTML로 저장
- Aspose.Words를 사용해 스트림 로드
- 메시지를 PDF로 저장
원본 이메일 메시지는 다음과 같이 볼 수 있습니다:
|
| | :- | |그림: 원본 MSG 파일 |
|
| | :- | |그림: 변환된 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);
}