我有一个带有fixedDocument的DocumentViewer (用XAML构建),然后我用代码将内容添加到fixedDocument中,它在屏幕上显示完美。
我的问题是,当我试图从fixedDocument创建一个XPS文件时,我得到一个‘它已经是另一个元素的子元素’的错误。
我找不到DocumentViewer.Children.Clear方法,如何移除/分离fixedDocument以便使用它来创建文件?
为了完整起见,下面是我得到错误的代码:
public void CreateXPSFile()
{
// 1 - create xps_file
string OutputPath = baseDir + pathAdjust + "test.xps";
using (FileStream fs = File.Create(OutputPath))
{
ConvertToXps(fixedDocument, fs);
}
// open the document using the system default xps viewer
Process.Start(OutputPath);
}
public static void ConvertToXps(FixedDocument fd, Stream outputStream)
{
var package = Package.Open(outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
// xps documents are built using fixed document sequences
var fixedDocSeq = new FixedDocumentSequence();
var docRef = new DocumentReference();
docRef.BeginInit();
docRef.SetDocument(fd);
docRef.EndInit();
((IAddChild)fixedDocSeq).AddChild(docRef); <<<<<----- Error occurs here
// write out our fixed document to xps
xpsWriter.Write(fixedDocSeq.DocumentPaginator);
xpsDoc.Close();
package.Close();
}谢谢
发布于 2011-07-27 23:23:44
您应该能够将文档设置为null。
DocumentViewer dv;
dv.Document = null;发布于 2011-07-28 04:17:23
由于您没有将XPS加载到dv中,因此设置dv.Document =null可能不会起到作用。而不是Process.Start(OutputPath);将xps加载到dv。或者,您可以为该进程指定一个名称,以便可以将其关闭。但我会显式地加载到dv中。
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// ...
myProcess.Close();https://stackoverflow.com/questions/6846778
复制相似问题