【问题标题】:How to remove FixedDocument from DocumentViewer?如何从 DocumentViewer 中删除 FixedDocument?
【发布时间】:2011-07-28 04:28:39
【问题描述】:

我有一个带有 fixedDocument(在 XAML 中构建)的 DocumentViewer,然后我将内容添加到代码中的 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();
    }

谢谢

【问题讨论】:

    标签: c# wpf xps documentviewer


    【解决方案1】:

    您应该能够将 Document 设置为 null。

    DocumentViewer dv;
    dv.Document = null;
    

    【讨论】:

      【解决方案2】:

      由于您没有将 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多