Render attachments

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

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);
    }
}