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);
myFixedPage.Children.Add(tb);
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.