首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从DocumentViewer中删除FixedDocument?

如何从DocumentViewer中删除FixedDocument?
EN

Stack Overflow用户
提问于 2011-07-27 23:15:28
回答 2查看 2K关注 0票数 0

我有一个带有fixedDocument的DocumentViewer (用XAML构建),然后我用代码将内容添加到fixedDocument中,它在屏幕上显示完美。

我的问题是,当我试图从fixedDocument创建一个XPS文件时,我得到一个‘它已经是另一个元素的子元素’的错误。

我找不到DocumentViewer.Children.Clear方法,如何移除/分离fixedDocument以便使用它来创建文件?

为了完整起见,下面是我得到错误的代码:

代码语言:javascript
复制
    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();
    }

谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-07-27 23:23:44

您应该能够将文档设置为null。

代码语言:javascript
复制
DocumentViewer dv;
dv.Document = null;
票数 2
EN

Stack Overflow用户

发布于 2011-07-28 04:17:23

由于您没有将XPS加载到dv中,因此设置dv.Document =null可能不会起到作用。而不是Process.Start(OutputPath);将xps加载到dv。或者,您可以为该进程指定一个名称,以便可以将其关闭。但我会显式地加载到dv中。

代码语言:javascript
复制
        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.Start();
        // ... 
        myProcess.Close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6846778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档