In my WPF app, I have several FixedDocument objects, which I want to display together in a DocumentViewer, as if they were a single document.
It's easy to display a single FixedDocument in a document viewer:
myDocumentViewer.Document = myFixedDocument;
My first idea was to create a FixedDocumentSequence of all my FixedDocuments (which can also be set as th开发者_如何学运维e DocumentViewer.Document value as it implements IDocumentPaginatorSource). But when building a FixedDocumentSequence, one can only add to a collection of DocumentReference objects. There is no straightforward way to add a FixedDocument directly, and I can't figure out how to create and add a DocumentReference which is somehow linked to one of my FixedDocuments.
My second idea was to create a master FixedDocument, and then loop through all the FixedDocuments, and move their pages into the master FixedDocument. But I run into all sort of issues when trying to do this (to do with pages already belonging to another document (with no ability to remove them from that document! etc. etc.)
My third idea was to create and build a temporary XpsDocument, and retrieve the FixedDocumentSequence that it automatically seems to create. I've looked at this a bit, but it's getting to be what feels needlessly complex, so I thought I'd post here first to see if there are any other ideas, or help with my first two attempts!
Figured it out.
You can in fact, dynamically create DocumentReference objects linked to FixedDocuments:
var documentReference = new DocumentReference();
documentReference.SetDocument(document);
fixedDocumentSequence.References.Add(documentReference);
I'm not quite sure how I missed the SetDocument
method before. I think I must have been looking for a DocumentReference.Document
property with getter/setter. It is a little inconsistent in the .NET framework to instead have GetDocument and SetDocument methods, in my opinion.
Have you tried doing this, i have tried implementing your 1st approach. I hope this will do for you -
FixedDocument destFixedDoc = new FixedDocument();
foreach (FixedDocument sourceFixedDoc in YourCollection)
{
if ((sourceFixedDoc != null) && (sourceFixedDoc.Pages != null))
{
foreach (PageContent sourcePage in sourceFixedDoc.Pages)
{
PageContent newPage = new PageContent();
newPage.Source = sourcePage.Source;
(newPage as IUriContext).BaseUri = ((IUriContext)sourcePage).BaseUri;
newPage.GetPageRoot(true);
destFixedDoc.Pages.Add(newPage);
}
}
}
精彩评论