how to add FixedPage to DocumentViewer

essamce 621 Reputation points
2020-06-16T16:38:01.34+00:00

how to add FixedPage to DocumentViewer

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-06-18T06:11:53.447+00:00

    Hi, in .NET Core 3.1. it work fine:

    <Window x:Class="WpfApp1.Window02"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp02"  
            mc:Ignorable="d"  
            Title="Window02" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <DocumentViewer Document="{Binding Doc}"/>  
      </Grid>  
    </Window>  
    

    -----------------------------------------------

    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Documents;  
      
    namespace WpfApp02  
    {  
      public class ViewModel  
      {  
        public ViewModel()  
        {  
          Doc = RenderDocument();  
        }  
      
        public IDocumentPaginatorSource Doc { get; set; }  
      
        public FixedDocument RenderDocument()  
        {  
          FixedDocument fd = new FixedDocument();  
          PageContent pc = new PageContent();  
          FixedPage fp = new FixedPage();  
          TextBlock tb = new TextBlock();  
          // add some text to a TextBox object  
          tb.Text = "This is some test text";  
          // add the text box to the FixedPage  
          fp.Children.Add(tb);  
          // ,add the FixedPage to the PageContent   
          pc.Child = fp;  
          // add the PageContent to the FixedDocument  
          fd.Pages.Add(pc);  
          return fd;  
        }  
      }  
    }  
    

    10127-x.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-06-16T18:52:48.56+00:00

    Hi, try following demo:

    <Window x:Class="Window031"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1.WpfApp031"
            mc:Ignorable="d"
            Title="Window031" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
      <Grid>
        <DocumentViewer Document="{Binding Doc}"/>
      </Grid>
    </Window>
    

    Namespace WpfApp031
      Public Class ViewModel
    
        Public Sub New()
          Doc = RenderDocument()
        End Sub
    
        Public Property Doc As IDocumentPaginatorSource
    
        Public Function RenderDocument() As FixedDocument
          Dim fd As New FixedDocument()
          Dim pc As New PageContent()
          Dim fp As New FixedPage()
          Dim tb As New TextBlock()
    
          ' add some text to a TextBox object
          tb.Text = "This is some test text"
          ' add the text box to the FixedPage
          fp.Children.Add(tb)
          ',add the FixedPage to the PageContent 
          pc.Child = fp
          ' add the PageContent to the FixedDocument
          fd.Pages.Add(pc)
          Return fd
        End Function
    
      End Class
    
    End Namespace
    
    1 person found this answer helpful.