我需要拆分现有的 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();
}
}
}
最佳答案
- Open the XpsDocument
- 创建目标 XpsDocument(相同方法)
- Get the FixedDocumentSequece from the first XpsDocument
- Get the first FixedDocument from the sequence.
- 获得第一个PageContent来自 Pages property
- 从 Child property 获取 FixedPage PageContent的
- Get the XpsDocumentWriter from the second XpsDocument
- Write the FixedPage
简单。
如 Christopher Currens 所述,可能需要在步骤 6 中使用 PageContent.GetPageRoot
而不是 Child
。
关于c# - 从 XPS 文档中提取单个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328596/