Generate document pages preview

GroupDocs.Merger provides PreviewOptions class to specify different options to manage preview of document pages.

Here are the steps to generate document preview with GroupDocs.Merger:

  • Create new instance of Merger class and pass source document path as a constructor parameter.
  • Instantiate the PreviewOptions object with:
    • delegate for each page stream creation (see event handler CreatePageStream); 
    • image preview format - PNG / JPG / BMP;
    • page numbers to process;
    • custom size of preview images (if needed). 
  • Call GeneratePreview method of Merger class instance and pass PreviewOptions to it.

CreatePageStream delegate implementation

GroupDocs.Merger expects CreatePageStream delegate to obtain each page stream for image preview generation process

private static Stream CreatePageStream(int pageNumber)
{
    string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
    var folder = Path.GetDirectoryName(imageFilePath);
    if(!Directory.Exists(folder))
    {
        Directory.CreateDirectory(folder);
    }
    return new System.IO.FileStream(imageFilePath, FileMode.Create);
}

ReleasePageStream delegate implementation

private static void ReleasePageStream(int pageNumber, Stream pageStream)
{
     pageStream.Dispose();
     string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
     Console.WriteLine("Image file {0} is ready for preview", imageFilePath);
}

Generate document preview from file on local disk

public static void GetPreview()
{   
    string filePath = @"c:\sample.xlsx";
    using (Merger merger = new Merger(filePath))
    {
        IPreviewOptions previewOption = new PreviewOptions(CreatePageStream, PreviewMode.JPEG);
        merger.GeneratePreview(previewOption);
    }
}
 
private static Stream CreatePageStream(int pageNumber)
{
    string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
    var folder = Path.GetDirectoryName(imageFilePath);
    if(!Directory.Exists(folder))
    {
        Directory.CreateDirectory(folder);
    }
    return new System.IO.FileStream(imageFilePath, FileMode.Create);
}

Generate document preview from stream with custom stream releasing delegate

public static void GetPreview()
{
    string filePath = @"c:\sample.xlsx";
    using (Merger merger = new Merger(filePath))
    {
        IPreviewOptions previewOption = new PreviewOptions(CreatePageStream, ReleasePageStream, PreviewMode.JPEG);
        merger.GeneratePreview(previewOption);
    }
}
 
private static Stream CreatePageStream(int pageNumber)
{
    string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
    var folder = Path.GetDirectoryName(imageFilePath);
    if(!Directory.Exists(folder))
    {
        Directory.CreateDirectory(folder);
    }
    return new System.IO.FileStream(imageFilePath, FileMode.Create);
}

private static void ReleasePageStream(int pageNumber, Stream pageStream)
{
     pageStream.Dispose();
     string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
     Console.WriteLine("Image file {0} is ready for preview", imageFilePath);
}