如何从 DocumentViewer 中删除固定文档?
我有一个带有固定文档(用 XAML 构造)的 DocumentViewer,然后我在代码中将内容添加到固定文档,它在屏幕上完美显示。
我的问题是,当我尝试从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();
}
谢谢
I have a DocumentViewer with a fixedDocument (constructed in XAML) I then add content to the fixedDocument in code and it displays perfecty on screen.
My problem is when I try to create an XPS file from the fixedDocument, im getting an 'its already a child of another element' error.
I cant find a DocumentViewer.Children.Clear method, How can I remove/detach the fixedDocument so I can use it to create the file?
for completeness, here's the code where im getting the error:
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();
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够将文档设置为空。
You should just be able to set the Document to null.
由于您没有将 XPS 加载到 dv 中,因此设置 dv.Document = null;可能不会成功。而不是 Process.Start(OutputPath);将 xps 加载到 dv 中。或者您可以为该进程指定一个名称,以便可以将其关闭。但我会明确加载到 dv 中。
Since you are not loading the XPS into dv then setting the dv.Document = null; might not do the trick. Rather than Process.Start(OutputPath); load the xps into dv. Or you can assign the process to a name so you can close it. But I would explicitly load into dv.