Extract a single page from an XPS document

落爺英雄遲暮 提交于 2020-02-01 05:46:47

问题


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();
            }
        }
    }

回答1:


  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.




回答2:


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



来源:https://stackoverflow.com/questions/5328596/extract-a-single-page-from-an-xps-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!