Generate document pages preview

GroupDocs.Annotation allows to generate document page previews using GeneratePreview method of a Document class.
PreviewOptions class is used to manage preview generation process - specify desired page numbers, image format etc.

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

  • Create a new instance of Annotator class and pass source document path as a constructor parameter.
  • Document property of Annotator object allows to access source document and provides GeneratePreview method.
  • 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 Annotator.Document object and pass PreviewOptions to it.

Here a PreviewOptions class main properties:

  • CreatePageStream - Delegate which defines method to create output page preview stream;
  • ReleasePageStream - Delegate which defines method to remove output page preview stream. This is can be used when need advanced control for resources handling.
  • Width - Preview image width. This property used when need customize output image width;
  • Height - Preview image height. This property used when need customize output image height;
  • PageNumbers - Page numbers that will be previewed;
  • PreviewFormat - Gets or sets the preview image format which provides ability to choose between image quality and size. BMP format should be used for the best image quality. JPG format will be useful in case of strict requirements to image size - it produces smallest image size (and faster loading image previews), but with lower quality than BMP. By default PNG format is selected - which is a golden mean between image quality and size.
  • RenderComments - Default value is true. If it is not needed to display replies and comments at the page preview - set RenderComments property to false (replies and comments still will be stored inside document).
    Please notice, that RenderComments value will impact any document comments (doesn’t matter if they were added by GroupDocs.Annotation or  some other application). This property affects only on WordProcessing documents

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

In some cases it may be useful to set specific image size during document pages preview generation. For example, to generate document pages thumbnails - small images that is a compressed preview image of the original image that is used as a placeholder. The main advantage of such thumbnail images is their reduced file size compared to the original page image.

The following code snippet demonstrates 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, after generating and rendering document page preview  image stream will be immediately disposed. However there is an ability to implement custom method for handling 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\hide replies for WordsProcessing documents

If you need not to generate comments on image preview you may use RenderComments property

Default State is true. If needed not to generate replies and comments - set it as false. Please notice, that it concerns any comments it doesn’t matter if they were added by GroupDocs.Annotation or somewhere else, they won’t be on the preview. But that still be present on the document.

Here the code that demonstrates how render 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);

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to annotate your PDF, DOC or DOCX, XLS or XLSX, PPT or PPTX, PNG and other documents with free to use online GroupDocs Annotation App.