Render attachments

Render attachments in the same way as any other documents.

To view attachments, follow these steps:

  1. Instantiate the first Viewer object. Specify a file that contains attachments.
  2. Call the saveAttachment method to save the attachment (to local disk, memory stream, etc).
  3. Instantiate the second Viewer object. Specify the previously saved attachment.
  4. Specify the view options depending on the output format - HtmlViewOptions/PngViewOptions/JpgViewOptions/PdfViewOptions.
  5. Call the View.view() method.

The following code snippet shows how to render attachments from the MSG file:

Note
NOTE: 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);
    }
}