将 XPS 文档附加到现有文档
伙计们,这就是我被困住的地方。
我需要从一大堆微小的 XPS 文件创建一个 XPS 文件。问题是当我尝试执行此操作时,内存总是不足。
我在下面展示了代码(取自 MSDN),但本质上它所做的只是这样:
- 它读取每个微小的 XPS 文件
- 并从中提取页面。
- 将这些页面添加到固定文档序列中。
- 当所有文档完成后,它会将此序列写入合并的 XPS 文档中。
IMO,我的固定文档序列变得太大了。所以,我在想,也许我可以一点一点地做这件事——即将微小的 XPS 文档一一附加到合并的 XPS 文档中。
现在,我不知道该怎么做。有什么指点吗?
代码:
//Create new xps package
Package combinedXPS = Package.Open(filename, FileMode.Create);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(new XpsDocument(combinedXPS));
FixedDocumentSequence combinedSequence = new FixedDocumentSequence();
//Go through each file given
foreach (string file in filenames)
{
//Load Xps Package
XpsDocument singleXPS = new XpsDocument(file, FileAccess.Read);
FixedDocumentSequence singleSequence = singleXPS.GetFixedDocumentSequence();
//Go through each document in the file
foreach (DocumentReference docRef in singleSequence.References)
{
FixedDocument oldDoc = docRef.GetDocument(false);
FixedDocument newDoc = new FixedDocument();
DocumentReference newDocReference = new DocumentReference();
newDocReference.SetDocument(newDoc);
//Go through each page
foreach (PageContent page in oldDoc.Pages)
{
PageContent newPage = new PageContent();
newPage.Source = page.Source;
(newPage as IUriContext).BaseUri = ((IUriContext)page).BaseUri;
newPage.GetPageRoot(true);
newDoc.Pages.Add(newPage);
}
//Add the document to package
combinedSequence.References.Add(newDocReference);
}
singleXPS.Close();
}
xpsWriter.Write(combinedSequence);
combinedXPS.Close();
Guys, here's where I am stuck.
I have a need to create a single XPS file from a huge bunch of tiny XPS files. The problem is that I keep running out of memory when I try to do this.
I present below the code (taken from MSDN), but essentially all it does is this:
- It reads each tiny XPS file
- Extracts the pages from it.
- Adds these pages to a FixedDocumentSequence.
- When all docs are done, it writes this sequence out to the combined XPS doc.
IMO, my FixedDocumentSequence is getting too big. So, I am thinking, that maybe I can do this piece by piece - i.e. append the tiny XPS docs to the combined XPS docs one by one.
Now, I don't know how to do that. Any pointers?
Code:
//Create new xps package
Package combinedXPS = Package.Open(filename, FileMode.Create);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(new XpsDocument(combinedXPS));
FixedDocumentSequence combinedSequence = new FixedDocumentSequence();
//Go through each file given
foreach (string file in filenames)
{
//Load Xps Package
XpsDocument singleXPS = new XpsDocument(file, FileAccess.Read);
FixedDocumentSequence singleSequence = singleXPS.GetFixedDocumentSequence();
//Go through each document in the file
foreach (DocumentReference docRef in singleSequence.References)
{
FixedDocument oldDoc = docRef.GetDocument(false);
FixedDocument newDoc = new FixedDocument();
DocumentReference newDocReference = new DocumentReference();
newDocReference.SetDocument(newDoc);
//Go through each page
foreach (PageContent page in oldDoc.Pages)
{
PageContent newPage = new PageContent();
newPage.Source = page.Source;
(newPage as IUriContext).BaseUri = ((IUriContext)page).BaseUri;
newPage.GetPageRoot(true);
newDoc.Pages.Add(newPage);
}
//Add the document to package
combinedSequence.References.Add(newDocReference);
}
singleXPS.Close();
}
xpsWriter.Write(combinedSequence);
combinedXPS.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XPS 文档只是压缩 XAML 文件,我敢打赌您可以直接从所有内容中写出 XML源文件而不将其全部保留在内存中,然后将其压缩。甚至还有 .NET API 可以帮助您直接处理此类文件。
XPS documents are just zipped up XAML files, I bet you could just directly write out an XML from all of the source files without keeping it all in memory, then zip it up. There's even .NET APIs that help you deal with these kinds of files directly.