c# - 从 XPS 文档中提取单个页面

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

最佳答案

  1. Open the XpsDocument
  2. 创建目标 XpsDocument(相同方法)
  3. Get the FixedDocumentSequece from the first XpsDocument
  4. Get the first FixedDocument from the sequence.
  5. 获得第一个PageContent来自 Pages property
  6. Child property 获取 FixedPage PageContent的
  7. Get the XpsDocumentWriter from the second XpsDocument
  8. Write the FixedPage

简单。


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

关于c# - 从 XPS 文档中提取单个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328596/

相关文章:

c# - 更改文件的 ACL 以允许所有人完全访问

.net - CA1303,DoNotPassLiteralsAsLocalizedParameters,但我实际上不是

c# - 如何在xps中将多个fpages添加到固定文档?

reporting-services - 以编程方式将 LocalReport 呈现为 XPS

c# - 使用 js/jQuery 显示 'saving...' 消息,然后重定向到页面

c# - 正则表达式替换文件链接中的空格

.net - 我可以将 mono 的 AOT 功能用于 native "pre-compile".NET DLL/EXE 以使其更难进行逆向工程吗?

c# - MySQL 与另外两个表一起创建一个表并使用 C# WinForms 添加记录

c# - 一列中有多个图表区域