Generate document page preview

GroupDocs.Annotation allows you to generate document page previews. To do this, call the GeneratePreview method of the Document class.

Use the PreviewOptions class to manage the generation process: specify desired page numbers, image format, etc.

To generate a document preview, follow these steps:

  1. Create an instance of the Annotator class. Specify the source document path as a parameter. The Document property of the Annotator class provides access to source document and implements GeneratePreview method.
  2. Instantiate the PreviewOptions class. Follow these steps: a. for each page, delegate stream creation (see the CreatePageStream event handler);  b. specify the image preview format - PNG / JPG / BMP, c. specify page numbers to process; d. set the custom size of preview images (if needed).
    Note
    Stream created by the CreatePageStream delegate are disposed automatically once after generation of preview image. To implement the custom image preview stream disposing, you have to specify an additional argument ReleasePageStream to clean up resources.
  3. Call the GeneratePreview method of the Annotator.Document class. Specify PreviewOptions.

The PreviewOptions class main properties are as follows:

  • CreatePageStream is a delegate which defines the method to create output page preview stream.
  • ReleasePageStream is a delegate which defines the method to remove output page preview stream. Use it for advanced control for resources handling.
  • Width specifies the preview image width.
  • Height specifies the preview image height.
  • PageNumbers - Page numbers that will be previewed;
  • PreviewFormat gets or sets the preview image format. Use the BMP format should be used for the best image quality. Use the JPG format in case of strict requirements to image size, but with lower quality than BMP. By default GroupDocs.Annotation uses the PNG format which is a golden mean between image quality and size.
  • RenderComments is an option to show or hide comments. By default it is true. Set it to false if you do not need to show comments. This property affects only on WordProcessing documents. The RenderComments value impacts any document comments.
  • RenderAnnotations is an option to show or hide annotations.By default it is true. Set it to false if you do not need to show annotations.
  • WorksheetColumns is a list of the WorksheetColumnsRange objects that indicates which columns to generate on specified worksheet.

The following code snippet demonstrates how to generate document previews:

Get document page previews 

using (Annotator annotator = new Annotator("input.pdf"))
{
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = $"D:/result_{pageNumber}.png";
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2, 3, 4 };
    annotator.Document.GeneratePreview(previewOptions);
}

Set specific size for preview images

You can set a specific size for preview images. For example, you can create small-sized thumbnails and full-sized previews.

The following code snippet shows how to set specific size for preview images:

using (Annotator annotator = new Annotator("input.pdf"))
{
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = $"D:/result_{pageNumber}.png";
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2, 3, 4 };
	previewOptions.Height = 100;
    previewOptions.Width = 80;
    annotator.Document.GeneratePreview(previewOptions);   
}

Get page previews with manual resource cleaning

By default, GroupDocs.Annotation deletes the image stream immediately after creating and rendering the document page. If necessary, you can implement a custom method to perform this operation.

// Method should match with ReleasePageStream delegate signature
private void UserReleaseStreamMethod(int pageNumber, Stream stream)
{
	Console.WriteLine($"Releasing memory for page: {pageNumber}");
    stream.Close();
}
 
using (Annotator annotator = new Annotator("input.pdf"))
{
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = $"D:/result_{pageNumber}.png";
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2, 3, 4 };
	previewOptions.Height = 100;
    previewOptions.Width = 80;
 
    // here we set delegate target method
    previewOptions.ReleasePageStream = UserReleaseStreamMethod;
    annotator.Document.GeneratePreview(previewOptions);   
}

Show and hide replies for WordsProcessing documents

Use the boolean RenderComments property to show comments when rendering image preview.

Note
This feature is supported for WordProcessign documents only.

The default value of the property is true. To hide comments, set it to false. This property applies to all comments.

The following code snippet shows how to render an image preview without comments:

Annotator annotator = new Annotator(File.OpenRead(MakeStoragePath(inputPath)));
           PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
           {
               var pagePath = MakeStoragePath(inputPath.Replace("input.doc", $"result{pageNumber}.png"));
               return File.Create(pagePath);
           });
previewOptions.PreviewFormat = PreviewFormats.PNG;
previewOptions.RenderComments = false;
annotator.Document.GeneratePreview(previewOptions);

Generate cells preview with specified columns

GroupDocs.Annotation v20.11 and later allows you to specify the columns you want to generate on the sheet you selected. To do this, use the WorksheetColumns property.

Note
This feature is supported for CellsProcessign documents only.

The following code snippet shows how to set the list of columns to render:

PreviewOptions previewOptions =
    new PreviewOptions(pageNumber => new FileStream($"preview_pages/page{pageNumber}.png", FileMode.Create),
        (number, stream) => stream.Dispose());
previewOptions.WorksheetColumns.Add(new WorksheetColumnsRange("Sheet1", 2, 3));
previewOptions.WorksheetColumns.Add(new WorksheetColumnsRange("Sheet1", 1, 1));

using (Annotator annotator = new Annotator("input.xlsx"))
{
    annotator.Document.GeneratePreview(previewOptions);
}

Original worksheet

Generated image file