Lazy loading of DOCX pages in WinForms/WPF viewer

Blog category: ImagingOffice.NET

April 19, 2024

DOCX document is a Microsoft Word Open XML format document that contains text, images, graphics and more. The advantage of DOCX document is a simple and intuitive editing of its content. The disadvantage of DOCX document is that it requires to layout the document content to break it into pages. In other words, for a 1000-page DOCX document, you require to render all pages of document, even if you only need to view the last page.

If you open a DOCX document with a large number of pages (1000 or more pages) in Microsoft Office Word it will be noticeable that Word opens the DOCX document almost instantly and displays only several pages (3-5 pages). Later, in the background, Word loads all other pages of the document. This is done so that the user don't have to wait until Word layouts all pages of the DOCX document, which can take quite a long time. Background loading of DOCX pages is very convenient, because the user can quickly start viewing and editing the beginning of the document. If the user needs to view or edit the last page of document it is necessary to wait until Word loads all pages of the document.

VintaSoft Imaging .NET SDK allows to view DOCX document and by default the SDK retrieves information about all pages of the DOCX document and only after that the pages of DOCX document can be viewed in document viewer. For large DOCX documents (1000 or more pages) this is not convenient, because it is necessary to wait until information about all pages is retrieved and only then the pages can be viewed.

Also VintaSoft Imaging .NET SDK allows to reproduce the behavior of Microsoft Office Word, i.e. retrieve information about pages of DOCX document in the background. In order to enable background retrieval of information about pages/images of image collection it is necessary to use the [ImageViewerImagesManager] class and specify that class should:

Here is C# code that allows to display DOCX document pages in the document viewer while loading information about DOCX document pages in the background:
/// <summary>
/// The images manager for an image viewer.
/// </summary>
Vintasoft.Imaging.UI.ImageViewerImagesManager _imagesManager;



/// <summary>
/// Initializes a new instance of the <see cref="MainForm1"/> class.
/// </summary>
public MainForm1()
{
    InitializeComponent();

    // create images manager
    _imagesManager = new Vintasoft.Imaging.UI.ImageViewerImagesManager(imageViewer1);
    // specify that manager should retrieve information about image from file during 1 second, add images to an image viewer,
    // do previous steps until information about all images will not be retrieved
    _imagesManager.IntermediateAddInterval = 1000;
    // specify that manager should work asynchronously
    _imagesManager.IsAsync = true;
    _imagesManager.ImageSourceAddException += ImagesManager_ImageSourceAddException;
}



/// <summary>
/// Opens an image file.
/// </summary>
/// <param name="filename">The filename.</param>
internal void OpenImageFile(string filename)
{
    // cancel previous file opening process
    CancelOpening();
    // clear image collection of manager
    _imagesManager.Images.ClearAndDisposeItems();
    // add image file to the manager
    _imagesManager.Add(filename);
}

/// <summary>
/// Cancels the opening of a file.
/// </summary>
private void CancelOpening()
{
    _imagesManager.Cancel();
}

private void ImagesManager_ImageSourceAddException(object sender, Vintasoft.Imaging.ImageSourceExceptionEventArgs e)
{
    string message = string.Format("Cannot open {0} : {1}", System.IO.Path.GetFileName(e.SourceFilename), e.Exception.Message);
    System.Windows.Forms.MessageBox.Show(message, "Error");
}