从XPS文档中提取单个页面
我需要拆分现有的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(); } } }
- 打开XpsDocument
- 创建目标XpsDocument(相同方法)
- 从第一个XpsDocument获取FixedDocumentSequece
- 从序列中获取第一个FixedDocument。
- 从Pages属性获取第一个PageContent
- 从PageContent的Child属性中获取FixedPage
- 从第二个XpsDocument获取XpsDocumentWriter
- 写下FixedPage
简单。
正如Christopher Currens所指出的,在步骤6中可能需要使用PageContent.GetPageRoot
而不是Child
。
谢谢,它可以帮助很多人寻找解决Xps打印限制的解决方法,忽略在页面级别定义的PrintTicket。