Render attachments
Leave feedback
Render attachments in the same way as any other documents.
To view attachments, follow these steps:
- Instantiate the first Viewer object. Specify a file that contains attachments.
- Call the saveAttachment method to save the attachment (to local disk, memory stream, etc).
- Instantiate the second Viewer object. Specify the previously saved attachment.
- Specify the view options depending on the output format - HtmlViewOptions/PngViewOptions/JpgViewOptions/PdfViewOptions.
- Call the View.view() method.
The following code snippet shows how to render attachments from the MSG file:
NoteNOTE: provided code snippet suits all format families that support attachments: emails, Outlook data files, archives, and PDF documents.
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
import com.groupdocs.viewer.options.LoadOptions;
import com.groupdocs.viewer.results.Attachment;
// ...
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Attachment firstAttachment;
// Save attachment
try (Viewer viewer = new Viewer("sample.msg")) {
firstAttachment = viewer.getAttachments().get(0);
viewer.saveAttachment(firstAttachment, outputStream);
}
LoadOptions loadOptions = new LoadOptions(firstAttachment.getFileType());
// Render attachment
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
Viewer viewer = new Viewer(inputStream, loadOptions)) {
HtmlViewOptions options = HtmlViewOptions.forEmbeddedResources();
viewer.view(options);
}
}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.