从XPS文档中提取单个页面

时间:2011-03-16 16:40:43

标签: c# .net xps xps-generation

我需要拆分现有的XPS文档并创建一个新的XPS文档,其中只有一页原始页面。我试图复制文档并从复制的文档中删除页面,但这很慢。有没有更有效的方法来做到这一点?在C#请。

感谢。

解决:

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

2 个答案:

答案 0 :(得分:8)

  1. Open the XpsDocument
  2. 创建目标XpsDocument(方法相同)
  3. Get the FixedDocumentSequece from the first XpsDocument
  4. Get the first FixedDocument from the sequence.
  5. PageContent
  6. 获取第一个Pages property
  7. 从PageContent的Child property获取固定页面
  8. Get the XpsDocumentWriter from the second XpsDocument
  9. Write the FixedPage
  10. 易。


    正如Christopher Currens所述,在步骤6中可能需要使用PageContent.GetPageRoot代替Child

答案 1 :(得分:1)

谢谢,它可以帮助很多人寻找解决Xps打印限制的解决方法,忽略在页面级别定义的PrintTicket。

https://connect.microsoft.com/VisualStudio/feedback/details/529120/printqueue-addjob-ignores-printtickets-in-xps-documents