Question

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();
            }
        }
    }
Was it helpful?

Solution

  1. Open the XpsDocument
  2. Create the destination XpsDocument (same method)
  3. Get the FixedDocumentSequece from the first XpsDocument
  4. Get the first FixedDocument from the sequence.
  5. Get the first PageContent from the Pages property
  6. Get the FixedPage from the Child property of the PageContent
  7. Get the XpsDocumentWriter from the second XpsDocument
  8. Write the FixedPage

Easy.


As noted by Christopher Currens, it may be necessary to use PageContent.GetPageRoot instead of Child in step 6.

OTHER TIPS

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://connect.microsoft.com/VisualStudio/feedback/details/529120/printqueue-addjob-ignores-printtickets-in-xps-documents

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top