Adding TextBlocks to a FixedPage (of a FixedDocumentSequence) is exponentially slower the more I add RRS feed

  • Question

  • My requirements:

    • Open a pre-created XPS document which resembles a blank form that requires filling in. It may have multiple pages.
    • "Fill it in" dynamically with code
    • Display it to the user
    • Print it

    I've got this all working, but it's starting to run extremely slow.

    I am using XpsDocument to get the XPS file, followed by a call to GetFixedDocumentSequence() and subsequent calls to DocumentPaginator.GetPage(int) to retrieve the various pages. These are provided as FixedPage objects. I then add various TextBlock objects in the right location on each page, like this:

    TextBlock tb = new TextBlock { Text = "..." };
    FixedPage.SetLeft(tb, leftValue);
    FixedPage.SetTop(tb, topValue);

    Simple, right? It was working no problem for me until I start to add more and more TextBlocks (I have maybe 100 in total), when it started to crawl in speed. It's exponential - the more Textblocks I add, the slower and slower it gets. Regardless of the TextBlock properties or how they are positioned, or whether the XPS has multiple pages or not, it still crawls.

    If I replace the TextBlock with Rectangle, the speed issue is gone. So the problem is with TextBlock.

    I have been battling with this for hours. I've tried adding all the text to a canvas and then adding the canvas to the Fixed page - same issues. I've tried many workarounds - no joy.

    In one of my tests, I instantiated a new FixedPage object (i.e. one not retrieved from the FixedDocumentSequence of the XpsDocument) and added hundreds of TextBlocks to it. In this test, there was NO speed issue - it worked fine. So my hunch is that in the multi-page scenario, there is a load of overhead - maybe it thinks it has to recalculate the entire multi page flow with each TextBlock I add (even though I am adding simple absolutely positioned one-line TextBlocks). I could be wrong.

    Any help with this would be massively appreciated.

    Friday, November 19, 2010 7:41 PM


  • I eventually got around this issue, although I never figured out just why I was having the speed issues.

    My hunch about it being applicable to a multi-page FixedDocument was wrong - if I create one from scratch, it works fine. The problem was with XPS. Opening an XPS and adding TextBlocks to the FixedPages is just plain slow. It actually adds them all quickly enough. But then randomly, in no particular place in my code, a several second delay/freeze just occurs. The XPS is doing something in the background - I still don't know what, or why one textblock is fast, but 100 are slow.

    I got around it by unzipping the XPS, taking out the documents/pages/resources into resource Xaml files, and loading the FixedDocument directly from them using XamlReader. I can then add all I want to this in the exact same way and it works great. Happy now.

    Now, if only the WPF DocumentReader wasn't so half-baked...

    • Marked as answer by rizzlwizzl Saturday, November 20, 2010 9:43 AM
    Saturday, November 20, 2010 9:43 AM