从XPS文档中提取单个页面

Jav*_*ier 3 .net c# 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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 8

  1. 打开XpsDocument
  2. 创建目标XpsDocument(相同方法)
  3. 从第一个XpsDocument获取FixedDocumentSequece
  4. 从序列中获取第一个FixedDocument.
  5. Pages属性获取第一个PageContent
  6. 从PageContent 的Child属性中获取FixedPage
  7. 从第二个XpsDocument获取XpsDocumentWriter
  8. 写下FixedPage

简单.


Christopher Currens所述,可能有必要使用PageContent.GetPageRoot而不是Child在步骤6中.

  • 我在使用几个XPS文件从`PageContent`的`Child`属性获取任何固定页面时遇到了麻烦.当Source属性设置固定页面时,你需要使用`PageContent.GetPageRoot`而不是'Child`来执行第6步.它在MSDN文档中,但我一直在忽略它. (2认同)