SIGN UP MEMBER LOGIN:    
ARTICLE

Loading a text file in a WPF FlowDocumentReader

Posted by Mahesh Chand Articles | WPF with C# August 12, 2009
In this article, I demonstrate how to load a text file in a FlowDocumentReader available in WPF using C#.
Reader Level:

The attached project is a WPF application written in C# and XAML. It uses OpenFileDialog to browse text files and once text file is selected, it is loaded in a FlowDocumentReader that allows you to view, paging, and find options.

First, let's create a UI with a TextBox, a Button, and a FlowDocumentViewer control.

The UI looks like Figure 1.

 

fdrImg1.jpg
Figure 1

 

The XAML code for the UI looks like this.

 

<Window x:Class="LoadTextFileInFlowDocumentViewerSample.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="290" Width="600">

    <Canvas>

        <TextBox Height="32" HorizontalAlignment="Left" Margin="6,10,0,0" Name="FileNameTextBox"

                 VerticalAlignment="Top" Width="393" />

        <Button Content="Browse" Height="32" HorizontalAlignment="Left" Margin="405,10,0,0"

                Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click" />

     

            <FlowDocumentReader Name="FlowDocReader" Background="LightBlue"

                                Canvas.Top="50" Canvas.Left="5" Width="560"

                                Height="210">

        </FlowDocumentReader>

    </Canvas>

</Window>  


On Button control, we will browse a text file and set TextBox.Text property to the selected file. As you can see from code below, we create a Paragraph and add the selected text file using Paragraph.Inlines.Add method. After that, we create a FlowDocument by passing this Paragraph and in the end; we set FlowDocumentReader.Document property to the FlowDocument.

 

Code sample in C#:

 

private void button1_Click(object sender, RoutedEventArgs e)

{

    // Create OpenFileDialog

    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();          

  

    // Set filter for file extension and default file extension

    dlg.DefaultExt = ".txt";

    dlg.Filter = "Text documents (.txt)|*.txt";

 

    // Display OpenFileDialog by calling ShowDialog method

    Nullable<bool> result = dlg.ShowDialog();

 

    // Get the selected file name and display in a TextBox

    if (result == true)

    {

        // Open document

        string filename = dlg.FileName;

        FileNameTextBox.Text = filename;

 

        Paragraph paragraph = new Paragraph();

        paragraph.Inlines.Add(System.IO.File.ReadAllText(filename));

        FlowDocument document = new FlowDocument(paragraph);

        FlowDocReader.Document = document;

    }

}

 

Code sample in VB.NET:

 

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        'Create OpenFileDialog

        Dim dlg As Microsoft.Win32.OpenFileDialog = New Microsoft.Win32.OpenFileDialog()

 

        ' Set filter for file extension and default file extension

        dlg.DefaultExt = ".txt"

        dlg.Filter = "Text documents (.txt)|*.txt"

 

        ' Display OpenFileDialog by calling ShowDialog method

        If (dlg.ShowDialog() = True) Then

 

            '; Open document

            Dim filename As String = dlg.FileName

            FileNameTextBox.Text = filename

 

            Dim paragraph As Paragraph = New Paragraph()

            paragraph.Inlines.Add(System.IO.File.ReadAllText(filename))

            Dim document As FlowDocument = New FlowDocument(paragraph)

            FlowDocReader.Document = document

        End If

 

    End Sub

 

Now if you run the application and browse a text file, the output looks like Figure 2.

 

fdrImg2.jpg
Figure 2

 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, Can i know how to import textfile (tab delimited text file) into wpf application and from wpf how can i analysis it into a 3d chart? Hope to heard from you soon. thank you regards, Adeline

Posted by Adeline Jan 09, 2011

i want to display the detail of selected file on file browser in window os format like vista and window7
it is working but not showing the detail

private void button1_Click(object sender, RoutedEventArgs e)

{

System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();

dlg.Title = "select file manuuaaly";

dlg.ShowDialog();

}



help out
amit

Posted by Amit Kumar Thakur Sep 08, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor