开发者

How to remove FixedDocument from DocumentViewer?

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          
        开发者_Go百科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


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

DocumentViewer dv;
dv.Document = null;


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜