如何从 DocumentViewer 中删除固定文档?

发布于 2024-11-26 18:05:28 字数 1478 浏览 3 评论 0原文

我有一个带有固定文档(用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2024-12-03 18:05:28

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

DocumentViewer dv;
dv.Document = null;

You should just be able to set the Document to null.

DocumentViewer dv;
dv.Document = null;
手长情犹 2024-12-03 18:05:28

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

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.

        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.Start();
        // ... 
        myProcess.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文