Free Support Forum - groupdocs.com

Specify output filename/directory

Hello, I’m trying to extract all attachments from a zip and store previews under file actual names (e.g. test.pdf becomes test_p_1.html, test_p_2.html etc.), how can I do this? Also, might be useful to specify output directory. I can’t find output path parameter in viewer.View method:

using (Viewer viewer = new Viewer("sample.zip"))
{
    var options = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(options);

    var attachments = viewer.GetAttachments();
    Console.WriteLine("\nAttachments:");
    foreach (Attachment attachment in attachments)
    {
        var ms = new MemoryStream();
        viewer.SaveAttachment(attachment, ms);

        if (attachment.FileType == FileType.Unknown)
            continue;

        var lo = new LoadOptions() { FileType = attachment.FileType };
        using (Viewer viewer1 = new Viewer(ms, lo))
        {
            viewer1.View(options); //How can I specify output filename here? It just writes everythign in p_1.html
        }

        Console.WriteLine(attachment);
    }
}

@eugenekr

The output directory can be specified in the ViewOptions as shown in the following code snippet

using (Viewer viewer = new Viewer("sample.zip"))
{
    var options = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(options);

    var attachments = viewer.GetAttachments();
    Console.WriteLine("\nAttachments:");
    foreach (Attachment attachment in attachments)
    {
        var ms = new MemoryStream();
        viewer.SaveAttachment(attachment, ms);

        if (attachment.FileType == FileType.Unknown)
            continue;

        var lo = new LoadOptions() { FileType = attachment.FileType };
        using (Viewer viewer1 = new Viewer(ms, lo))
        {
            var attachmentViewOptions = 
                HtmlViewOptions.ForEmbeddedResources($"{attachment.FileName}/p_{{0}}.html");

            viewer1.View(attachmentViewOptions);
        }

        Console.WriteLine(attachment);
    }
}

You can use attachment filename $"{attachment.FileName}/p_{{0}}.html" or attachment ID $"{attachment.Id}/p_{{0}}.html" when specifying output path template. The Attachment ID is typically relative path to the file e.g. foloder/attachmnt.ext.

1 Like

@eugenekr

To simplify attachment processing the new field called FilePath will be added to the Attachment class in the upcoming v21.3. This new field can be used to retrieve an attachment relative path e.g. folder/sample.docx and filename when the file is in archive root.

@eugenekr

The FilePath parameter has been added to Attachmnet class in GroupDocs.Viewer for v21.3 that just has been released. The new version can be found at

Have a nice day!