I need to split an existing XPS Document and create a new XPS Document with only one page of the original one. I tried to copy the document and delete pages from the copied document, but that's very slow. Is there a more efficient way to do this? In C# please.
Thanks.
Resolved:
public void Split(string originalDocument, string detinationDocument)
{
using (Package package = Package.Open(originalDocument, FileMode.Open, FileAccess.Read))
{
using (Package packageDest = Package.Open(detinationDocument))
{
string inMemoryPackageName = "memorystream://miXps.xps";
Uri packageUri = new Uri(inMemoryPackageName);
PackageStore.AddPackage(packageUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName);
XpsDocument xpsDocumentDest = new XpsDocument(packageDest, CompressionOption.Normal, detinationDocument);
var fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
DocumentReference docReference = xpsDocument.GetFixedDocumentSequence().References.First();
FixedDocument doc = docReference.GetDocument(false);
var content = doc.Pages[2];
var fixedPage = content.GetPageRoot(false);
var writter = XpsDocument.CreateXpsDocumentWriter(xpsDocumentDest);
writter.Write(fixedPage);
xpsDocumentDest.Close();
xpsDocument.Close();
}
}
}
- Open the XpsDocument
- Create the destination XpsDocument (same method)
- Get the FixedDocumentSequece from the first XpsDocument
- Get the first FixedDocument from the sequence.
- Get the first PageContent from the Pages property
- Get the FixedPage from the Child property of the PageContent
- Get the XpsDocumentWriter from the second XpsDocument
- Write the FixedPage
Easy.
As noted by Christopher Currens, it may be necessary to use PageContent.GetPageRoot
instead of Child
in step 6.
Thank you, it can help a lot people looking for a workaround against limitation of Xps printing which ignores PrintTicket defined at page level.
来源:https://stackoverflow.com/questions/5328596/extract-a-single-page-from-an-xps-document