我正在尝试从磁盘加载XPS文件,并将其打印为我创建的内存文档中的FixedDocument或FixedDocumentSequence的一部分。它们需要作为一个序列打印,因为它们是重复的。
到目前为止,我最好的尝试是:
// create my memory FixedDocument (a packing slip)
DocumentReference mainDocRef = GetMainDoc(); // created in memory
// load XPS document from file (to print on the back)
XpsDocument xpsDoc = new XpsDocument("flyer.xps", FileAccess.Read);
var docSequenceFromFile = xpsDoc.GetFixedDocumentSequence();
var xpsDocRef = docSequenceFromFile.References.First();
// try to combine together
FixedDocumentSequence documentSequence = new FixedDocumentSequence();
documentSequence.References.Add(mainDocRef);
documentSequence.References.Add(xpsDocRef); // THROWS EXCEPTION
// print
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(printQueue);
xps.Write(documentSequence, ticket);我总是有一个例外:
InvalidOperationException:附加信息:指定的元素已经是另一个元素的逻辑子元素。先切断它。
我已经尝试过几种方法来做这件事,但是最后还是会出现这样的错误
如何加载XpsDocument并将其打印为内存中创建的FixedDocumentSequence中的第二个页面?
发布于 2014-01-29 05:30:44
Simon_Weaver,
由于我不知道GetMainDoc()方法返回的是什么,我已经排除了这一点,只是注释掉了mainDocRef的参考值,但是这似乎很小,因为您的问题是将加载的documentSequence文件添加到documentSequence中,而mainDocRef与问题有点无关(如果我错了,请纠正我)。
现在的问题是,xpsDoc被加载到另一个元素(文档)中,我们必须将它作为异常状态来分离。但是,这样做的能力受到internal方法的保护。这就是说,最简单的方法就是枚举文档的所有页面,并从被枚举的页面源创建一个新页面到一个新文档。
最后的代码看起来像是..。(代码注释)
//DocumentReference mainDocRef = GetMainDoc(); // created in memory. commented as I dont have a reference to what this object contains.
//create our new document reference to add the pages to
DocumentReference newDocReference = new DocumentReference();
// load XPS document from file (to print on the back)
using (XpsDocument xpsDoc = new XpsDocument(@"flyer.xps", FileAccess.Read))
{
var docSequenceFromFile = xpsDoc.GetFixedDocumentSequence();
var xpsDocRef = docSequenceFromFile.References.First();
//get the fixed document to enumerate
FixedDocument xpsFixedDoc = xpsDocRef.GetDocument(false);
//get the fixed document to add to
FixedDocument newFixedDoc = new FixedDocument();
//set the new document reference
newDocReference.SetDocument(newFixedDoc);
//enumerate each page of the fixed document
foreach (PageContent page in xpsFixedDoc.Pages)
{
PageContent newPageContent = new PageContent();
newPageContent.Source = page.Source;
((IUriContext)newPageContent).BaseUri = ((IUriContext)page).BaseUri;
newPageContent.GetPageRoot(true);
newFixedDoc.Pages.Add(newPageContent);
}
}
// try to combine together.
FixedDocumentSequence documentSequence = new FixedDocumentSequence();
//documentSequence.References.Add(mainDocRef); <<-- commented out, re-add after tests
//add the new document reference
documentSequence.References.Add(newDocReference);
// print
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(printQueue);
xps.Write(documentSequence, ticket);
mainDocRef = null;
newDocReference = null;现在要注意的一点是,XpsDocument和PrintQueue继承了IDisposable。(不需要告诉你在那里做什么)。
现在,GetMainDoc()可能仍然存在问题,但是由于对该方法的引用没有发布,所以我不能在这里测试任何错误。告诉我事情进展如何。
干杯。尼科
https://stackoverflow.com/questions/21345895
复制相似问题