Generate document pages preview

GroupDocs.Comparison allows you to generate page previews for source, target and output document(s) using the GeneratePreview method of the Document class.

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

To generate the document preview using GroupDocs.Comparison API, follow these steps:

  1. Create an instance of the Comparer class. Specify the source document path as a constructor parameter.
  2. Add the target document to comparison using the Add method.
  3. The Source and Targets properties of the Comparer object allows you to access source and target documents and provides the GeneratePreview method;
  4. Instantiate the PreviewOptions object. Specify the following parameters:
    • delegate for each page stream creation (see the CreatePageStream event handler)
    • image preview format - PNG / JPG / BMP
    • page numbers to process
    • custom size of preview images (if needed)
      Note
      Stream created by the CreatePageStream delegate is disposed automatically when preview image is generated. If you need to implement the custom disposing of the image preview stream, specify the ReleasePageStream argument to clean up resources.
  5. Call the GeneratePreview method of the Source and Targets documents. Specify the PreviewOptions.

The PreviewOptions class main properties are as follows:

  • CreatePageStream is a delegate which defines method to create output page preview stream
  • ReleasePageStream is a delegate which defines method to remove output page preview stream. This is can be used when need advanced control for resources handling
  • Width is preview image width. Use this property to customize output image width
  • Height is preview image height. Use this property to customize output image height
  • PageNumbers defines numbers of pages to be previewed;
  • PreviewFormat gets or sets the preview image format which provides ability to choose between image quality and size. Use the BMP format for the best image quality. Use the JPG format to produce smallest image size (and faster loading image previews), but with lower quality than BMP. By default GroupDocs.Comparison uses the PNG format to provide appropriate image quality and size.

The following code snippet shows how to generate document previews:

Get page previews for source document

using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
using System.IO;
// ...

using (Comparer comparer = new Comparer("source.docx"))
{
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(@"D:\Temp\", $"result_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    comparer.Source.GeneratePreview(previewOptions);
}

The result is as follows:

Get page previews for target document

using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
using System.IO;
// ...

using (Comparer comparer = new Comparer("source.docx"))
{
    comparer.Add("target.docx");
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(@"C:\", $"result_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    comparer.Targets[0].GeneratePreview(previewOptions);
}

Get page previews for output document

using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
using System.IO;
// ...

using (Comparer comparer = new Comparer("source.docx"))
{
    comparer.Add("target.docx");
    comparer.Compare("result.docx");
    Document document = new Document(File.OpenRead("result.docx"));
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(@"C:\", $"result_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    document.GeneratePreview(previewOptions);
}

Set specific size for preview images

using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
using System.IO;
// ...

using (Comparer comparer = new Comparer("source.pptx"))
{
    comparer.Add("target.pptx");
    comparer.Compare("result.pptx");
    Document document = new Document(File.OpenRead("result.pptx"));
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(@"C:\", $"result_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    previewOptions.Height = 1000;
    previewOptions.Width = 1000;
    document.GeneratePreview(previewOptions);
}
Note
This feature is not supported for WordProcessing documents.

Get page previews and clean resources manually

using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
using System.IO;
// ...

// Method should match with ReleasePageStream delegate signature
private void UserReleaseStreamMethod(int pageNumber, Stream stream)
{
    Console.WriteLine($"Releasing memory for page: {pageNumber}");
    stream.Close();
}

using (Comparer comparer = new Comparer("source.docx"))
{
    comparer.Add("target.docx");
    comparer.Compare("result.docx");
    Document document = new Document(File.OpenRead("result.docx"));
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(@"C:\", $"result_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    // here we set delegate target method
    previewOptions.ReleasePageStream = UserReleaseStreamMethod;
    document.GeneratePreview(previewOptions);
}