How to load a FlowDocument for viewing in PowerShell

29 views Asked by At

I think this should be simple, but I can't get it to work - what an I doing wrong? The error is on the $Range.Load line... If someone can give me an example, it does not need to be a RichEditBox and can be one of the other FlowDocument viewers.

Add-Type -AssemblyName PresentationFramework

if ([System.Threading.Thread]::CurrentThread.GetApartmentState() -eq "STA") {
    $Report = "$PSScriptRoot\Test.xaml"

    $Viewer = [System.Windows.Controls.RichTextBox]::new()
    $Viewer.IsReadOnly = $true
    $Viewer.Document = [System.Windows.Documents.FlowDocument]::new()
    $Range = [System.Windows.Documents.TextRange]::new($Viewer.Document.ContentStart, $Viewer.Document.ContentEnd)

    $DocItem = Get-Item $Report
    $Stream = $DocItem.OpenRead()
    <#
        I get a "Cannot create unknown type 'FlowDocument'." on the below line...  Any ideas?
    #>
    $Range.Load($Stream, "Xaml")
    $Stream.Close()

    $window = New-Object System.Windows.Window
    $window.FontFamily = "Lucida Console"
    $window.FontSize = 12
    $window.Title = "FlowDocument Test Viewer"
    $window.Content = $Viewer
    $window.ShowDialog() | Out-Null
}
0

There are 0 answers