detecting blank xps documents/pages?

May 11, 2012 at 4:07 PM

Hello -

Do you know of a handy way to detect if an XPS is essentially blank (no pages)? Blank documents are purely the result of user error, but I'd rather detect them and tell the user than have them stare at a blank space and think it's the code's fault. :)

I looked at the DocumentDataSource and DocumentViewer events for possible candidates, but couldn't find one that was guaranteed to fire even when there weren't any pages to load. Setting the package reader to treat warnings as errors did not have any impact.

For now I am simply showing a message along the lines of "if this message doesn't disappear in two seconds, double-check your settings," but it feels a little sloppy so I'd like to improve it if I can.

Thanks!

Coordinator
May 14, 2012 at 7:47 AM
Edited May 14, 2012 at 7:48 AM

The Document property on the DocumentDataSource control has a property PageCount that you can use for this purpose. Documents are loaded asynchronously, so you need to wait until the Document property is set. That's where the DocumentDataSource.PropertyChanged event comes into play.

The following snippet handles the PropertyChanged event and displays the page count once a document is available.

void DataSource_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
  if (e.PropertyName == "Document") {
    var document = this.DataSource.Document;
    if (document != null) {
      MessageBox.Show("Document page count: " + document.PageCount);
    }
  }
}

- Koen