none
How to display a txt file in flowdocumentReader?

    Question

  • i wan to display a txt file in a flowdocumentreader. and want to capture the page changed event of the flowdocumentreader. i don't how to do.
    Saturday, April 05, 2008 3:21 PM

Answers

  • The PageCount is not the number of pages in the document but rather the number of pages into which the FlowDocument Readerhas partitioned the document.  If you change the size of the FlowDocumentReader control then the PageCount may change.  Most likely the reason that you are seeing a PageCount of 0 is because the FlowDocumentReader's container has not yet arranged the FlowDocumentReader.  If you sink the SizeChangedEvent and trace the PageCount you will notice that it changes as the size of the FlowDocumentReader changes.

     

    In order to capture the commands shown in the FlowDocumentReader's toolbar you will have to create a CommandBinding for each command that you want to capture, so for example your XAML might look like:

     

    Code Snippet

    <Window.CommandBindings>

      <CommandBinding PreviewExecuted="CommandBinding_PreviewExecuted"

         Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/>

    </Window.CommandBindings>

    <Grid>

      <FlowDocumentReader x:Name="documentReader"/>

    </Grid>

     

     

    The only set of buttons on FlowDocumentReader that doesn't seem to correlate to a command is are the ones that affect the ViewingMode but ViewingMode is a dependency property so you could control it by binding it to a property.

     

    DocumentViewer is different from FlowDocumentReader in that it displays FixedDocuments, not FlowDocuments.  I think this fact alone will make DocumentViewer significantly more difficult to work with.

     

    Hope this helps,

    Wells

     


    Wells Caughey | Magenic Technologies
    Saturday, April 05, 2008 10:33 PM
  • you could just as well call textange.text =filestring (txt from file) where text range encompasses (document.contentstart,document.contentend)

    Sunday, April 06, 2008 5:08 PM

All replies

  • WPF seperates out the concepts of a Control Hierarchy from a Document Content Hierarchy; controls are derived from UIElement whereas content is derived from ContentElement.  In your situation the FlowDocumentReader is a control, but it in turn contains a FlowDocument which represents the document's content.

     

    In order to load a FlowDocumentReader with text you will need to create a FlowDocument, to it add a Paragraph (FlowDocuments can only contain Block-level content) to which you can add your text.  Finally you will set the Document property of your FlowDocumentReader to the new FlowDocument:

     

    Code Snippet

    Paragraph paragraph = new Paragraph();

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

    FlowDocument document = new FlowDocument(paragraph);

    documentReader.Document = document;

     

     

    There is one thing to note: Flow Documents can contain far more than just text so if your needs ever change so that you want to incorporate this richer content into your documents you may want to look at the Document Serialization library: http://msdn2.microsoft.com/en-us/library/ms745778.aspx

     

    Hope this helps,

    Wells

     

     


    Wells Caughey | Magenic Technologies
    Saturday, April 05, 2008 4:45 PM
  • thans very much for answering my question. now , this question seems to be sovled,but actually, there is a problem.
    after loading the file content into the flowdocumentreader, if we watch the PageCount Property of it, it is 0. i don't know why.
      Additionally, i have two more questions:
    1. we know that the flowdocumentreader has navigation buttons, how can i capture the corresponding event and to add something i want to do.
    2. can we add Annotations to the flowdocumentreader as we do to documentviwer displaying xps files.
    3. how to display a txt file in a documentviewer..


    ^_^ the questions are so many .that's because i am a freshman in wpf, and want to do a bookreader using wpf.
    if you can help me, i will be greatly appreciate.
    Saturday, April 05, 2008 5:31 PM
  • The PageCount is not the number of pages in the document but rather the number of pages into which the FlowDocument Readerhas partitioned the document.  If you change the size of the FlowDocumentReader control then the PageCount may change.  Most likely the reason that you are seeing a PageCount of 0 is because the FlowDocumentReader's container has not yet arranged the FlowDocumentReader.  If you sink the SizeChangedEvent and trace the PageCount you will notice that it changes as the size of the FlowDocumentReader changes.

     

    In order to capture the commands shown in the FlowDocumentReader's toolbar you will have to create a CommandBinding for each command that you want to capture, so for example your XAML might look like:

     

    Code Snippet

    <Window.CommandBindings>

      <CommandBinding PreviewExecuted="CommandBinding_PreviewExecuted"

         Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/>

    </Window.CommandBindings>

    <Grid>

      <FlowDocumentReader x:Name="documentReader"/>

    </Grid>

     

     

    The only set of buttons on FlowDocumentReader that doesn't seem to correlate to a command is are the ones that affect the ViewingMode but ViewingMode is a dependency property so you could control it by binding it to a property.

     

    DocumentViewer is different from FlowDocumentReader in that it displays FixedDocuments, not FlowDocuments.  I think this fact alone will make DocumentViewer significantly more difficult to work with.

     

    Hope this helps,

    Wells

     


    Wells Caughey | Magenic Technologies
    Saturday, April 05, 2008 10:33 PM
  • you could just as well call textange.text =filestring (txt from file) where text range encompasses (document.contentstart,document.contentend)

    Sunday, April 06, 2008 5:08 PM