User Tools

Site Tools


notes:wpf:documents

Documents in WPF

FlowDocument

FlowDocument elements are automatically wrapped in FlowDocumentPageViewer which provides pagination. Non-paginated display can be achieved by wrapping FlowDocument in FlowDocumentScrollViewer which provides scrollbars.

Example: FlowDocument can contain block elements such as a Paragraph. Paragraph defines the logical structure of the text. It supports all the same font properties as TextBlock:

<Window x:Class="QuickTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="370">
 
    <Window.Resources>
        <!-- Disable scrollbars -->
        <Style TargetType="{x:Type FlowDocumentScrollViewer}">
            <Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
            <Setter Property="VerticalScrollBarVisibility" Value="Disabled" />
        </Style>
    </Window.Resources>
 
    <FlowDocumentScrollViewer>
        <FlowDocument FontSize="12">
            <Paragraph>
                Windows Presentation Foundation (WPF) provides a great set of features. 
                So many features, in fact, that even some very significant ones don't 
                get nearly the attention they deserve. A perfect example is the Flow 
                Documents feature, which allows developers to create documents natively 
                in WPF. Flow Documents are aimed at screen reading and work for almost 
                everything related to text content, from product descriptions to entire books.
            </Paragraph>
            <Paragraph FontSize="10" FontStyle="Italic" >
                Source: http://msdn.microsoft.com/en-us/magazine/cc163371.aspx
            </Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
 
</Window>

Example: Disabling scrollbars in FlowDocumentScrollViewer:

<!-- Method #1 - using a style -->
<Style TargetType="{x:Type FlowDocumentScrollViewer}">
    <Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
    <Setter Property="VerticalScrollBarVisibility" Value="Disabled" />
</Style>
 
<!-- Method #2 - setting properties directly -->
<FlowDocumentScrollViewer HorizontalScrollBarVisibility="Disabled"
                          VerticalScrollBarVisibility="Disabled">
...
</FlowDocumentScrollViewer>

Floater

An example of a Floater element:

<Window x:Class="QuickTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="700">
 
    <FlowDocumentScrollViewer>
        <FlowDocument FontSize="12">
            <Paragraph FontWeight="Bold">Quaternions (source: Wikipedia)</Paragraph>
 
            <!-- 1st paragraph -->
            <Paragraph>
                In mathematics, the quaternions are a number system that extends the complex numbers. 
                They were first described by Irish mathematician Sir William Rowan Hamilton in 1843 
                and applied to mechanics in three-dimensional space. Hamilton defined a quaternion 
                as the quotient of two directed lines in a three-dimensional space or equivalently 
                as the quotient of two vectors.
            </Paragraph>
 
            <!-- Floater -->
            <Paragraph>
                <Floater Background="AntiqueWhite" BorderBrush="Black" 
                         BorderThickness="1" Margin="10" Width="280">
                    <Paragraph FontWeight="Bold">Quaternion Formula</Paragraph>
                    <Paragraph>
                        The great breakthrough in quaternions came on Monday 16 October 1843 in Dublin, 
                        when Hamilton was on his way to the Royal Irish Academy where he was going to 
                        preside at a council meeting. While walking along the towpath of the Royal Canal 
                        with his wife, the concepts behind quaternions were taking shape in his mind. 
                        When the answer dawned on him, Hamilton could not resist the urge to carve the 
                        formula for the quaternions into the stone of Brougham Bridge as he paused on it.
                    </Paragraph>
                </Floater>
            </Paragraph>
 
            <!-- 2st paragraph -->
            <Paragraph>
                Quaternions find uses in both theoretical and applied mathematics, in particular for
                calculations involving three-dimensional rotations such as in three-dimensional computer
                graphics and computer vision. They can be used alongside other methods, such as Euler 
                angles and rotation matrices, or as an alternative to them depending on the application.
            </Paragraph>
 
            <!-- 3st paragraph -->
            <Paragraph>
                In modern language, quaternions form a four-dimensional associative normed division 
                algebra over the real numbers, and thus also form a domain. In fact, the quaternions 
                were the first noncommutative division algebra to be discovered.
            </Paragraph>
 
            <!-- Floater -->
            <Paragraph>
                <Floater Background="AliceBlue" BorderBrush="Black" BorderThickness="1" 
                         Margin="10" Width="230" HorizontalAlignment="Left">
                    <Paragraph FontWeight="Bold">
                        Historical impact on physics
                    </Paragraph>
                    <Paragraph>
                        In 1984 the European Journal of Physics published P.R. Girard's essay 
                        "The quaternion group and modern physics". It "shows how various physical 
                        covariance groups: SO(3), the Lorentz group, the general relativity group, 
                        the Clifford algebra SU(2), and the conformal group can be readily related 
                        to the quaternion group" in modern algebra.
                    </Paragraph>
                </Floater>
            </Paragraph>
 
            <!-- 4th paragraph -->
            <Paragraph>
                Quaternions have had a revival since the late 20th Century, primarily due to 
                their utility in describing spatial rotations. The representations of rotations 
                by quaternions are more compact and quicker to compute than the representations 
                by matrices. In addition, unlike Euler angles they are not susceptible to 
                gimbal lock. For this reason, quaternions are used in computer graphics,computer 
                vision, robotics, control theory, signal processing, attitude control, physics, 
                bioinformatics, molecular dynamics, computer simulations, and orbital mechanics. 
                For example, it is common for the attitude-control systems of spacecraft to be 
                commanded in terms of quaternions. Quaternions have received another boost from 
                number theory because of their relationships with the quadratic forms.
            </Paragraph>
 
        </FlowDocument>
    </FlowDocumentScrollViewer>
 
</Window>

wpf_floater1.jpg

Figure

Figure is similar to Floater but it has an ability to be positioned more precisely.

An example of the Figure element:

<Window x:Class="QuickTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="550" Width="750">
 
    <FlowDocumentReader>
        <FlowDocument ColumnWidth="400" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
            <Section FontSize="12">
 
                <Paragraph>
                    <Bold>A quasi-stellar radio source ("quasar")</Bold> is a very energetic and
                    distant active galactic nucleus. Quasars are extremely luminous and were first 
                    identified as being high redshift sources of electromagnetic energy, including 
                    radio waves and visible light, that were point-like, similar to stars, rather 
                    than extended sources similar to galaxies.
                </Paragraph>
 
                <Paragraph>
                    <Figure Width="200" Height="60" Background="AntiqueWhite" HorizontalAnchor="PageLeft" 
                            HorizontalOffset="150" VerticalOffset="85">
                        <Paragraph TextAlignment="Left" Background="White" Foreground="Navy" Padding="5">
                            Some quasars located at the distance of about 10 billion light years 
                            are visible from Earth.
                        </Paragraph>
                    </Figure>
                </Paragraph>
 
                <Paragraph>
                    Quasars show a very high redshift, which is an effect of the expansion of 
                    the universe between the quasar and the Earth. They are among the most 
                    luminous, powerful, and energetic objects known in the universe. They tend 
                    to inhabit the very centers of active young galaxies and can emit up to 
                    a thousand times the energy output of the Milky Way. When combined with 
                    Hubble's law, the implication of the redshift is that the quasars are very 
                    distant-and thus, it follows, objects from much earlier in the universe's history. 
                    The most luminous quasars radiate at a rate that can exceed the output of average 
                    galaxies, equivalent to two trillion (2×1012) suns. This radiation is emitted 
                    across the spectrum, almost equally, from X-rays to the far-infrared with a peak 
                    in the ultraviolet-optical bands, with some quasars also being strong sources 
                    of radio emission and of gamma-rays. In early optical images, quasars looked 
                    like single points of light (i.e., point sources), indistinguishable from 
                    stars, except for their peculiar spectra. With infrared telescopes and the Hubble 
                    Space Telescope, the "host galaxies" surrounding the quasars have been identified 
                    in some cases. These galaxies are normally too dim to be seen against the glare 
                    of the quasar, except with these special techniques. Most quasars cannot be seen 
                    with small telescopes, but 3C 273, with an average apparent magnitude of 12.9, 
                    is an exception. At a distance of 2.44 billion light-years, it is one of the most 
                    distant objects directly observable with amateur equipment.
                </Paragraph>
 
                <Paragraph>
                    <Figure Width="200" Height="80" Background="AntiqueWhite" 
                            HorizontalAnchor="PageCenter" WrapDirection="Both"
                            HorizontalOffset="0" VerticalOffset="20">
                        <Paragraph TextAlignment="Left" Background="White" Foreground="Navy" Padding="5">
                            A cosmic mirage known as the Einstein Cross is a direct visual 
                            confirmation of the theory of general relativity.
                        </Paragraph>
                    </Figure>
                </Paragraph>
 
                <Paragraph>
                    While the nature of these objects was controversial until as recently as 
                    the early 1980s, there is now a scientific consensus that a quasar is 
                    a compact region in the center of a massive galaxy surrounding its central 
                    supermassive black hole. Its size is 10-10,000 times the Schwarzschild radius 
                    of the black hole. The quasar is powered by an accretion disc around the black hole.
                </Paragraph>
 
                <Paragraph></Paragraph>
                <Paragraph FontStyle="Italic">
                    Source: Wikipedia (http://en.wikipedia.org/wiki/Quasar)
                </Paragraph>
            </Section>
        </FlowDocument>
    </FlowDocumentReader>
 
</Window>

wpf_figure1.jpg

List

List can be numbered or bulleted.

Example: A numbered list:

<List MarkerStyle="Decimal">
    <ListItem>
        <Paragraph>First item</Paragraph>
    </ListItem>
    <ListItem>
        <Paragraph>Second item</Paragraph>
    </ListItem>
    <ListItem>
        <Paragraph>Third item</Paragraph>
    </ListItem>
</List>

Other marker styles for the List:

  • UpperRoman
  • LowerRoman
  • UpperLatin
  • LowerLatin
  • Circle
  • Disc
  • Square
  • Box

Table

Example: A Table element:

<Window x:Class="QuickTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="400">
 
    <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Disabled"
                              VerticalScrollBarVisibility="Disabled">
        <FlowDocument>
            <Table CellSpacing="6">
 
                <!-- Column Definitions -->
                <Table.Columns>
                    <TableColumn Width="90"/>
                    <TableColumn Width="80"/>
                    <TableColumn Width="Auto"/>
                </Table.Columns>
 
                <!-- Title and Table Header -->
                <TableRowGroup>
                    <TableRow FontSize="20" FontWeight="Bold">
                        <TableCell ColumnSpan="3" TextAlignment="Center" >
                            <Paragraph>C# Versions</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow FontWeight="Bold" FontSize="14" Background="LightGray">
                        <TableCell>
                            <Paragraph>C# Version</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>Released</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET Version</Paragraph>
                        </TableCell>
                    </TableRow>
                </TableRowGroup>
 
                <!-- Table Rows -->
                <TableRowGroup>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 1.0</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2002</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 1.0</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 1.2</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2003</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 1.1</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 2.0</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2006</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 2.0</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 3.0</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2007</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 2.0, 3.0, 3.5</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 4.0</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2010</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 4</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>C# 5.0</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>2012</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>.NET 4.5</Paragraph>
                        </TableCell>
                    </TableRow>
 
                </TableRowGroup>
            </Table>
 
        </FlowDocument>
    </FlowDocumentScrollViewer>
 
</Window>

Table can be split into multiple pages when printing.

notes/wpf/documents.txt · Last modified: 2017/03/16 by admin