A template is used in software applications to specify how data, documents, or UI elements are presented visually, either on screen or when printed. They provide an organized way to define and instantiate common patterns or layouts that can be used throughout an application, such as page numbers, document title, author name, dates, etc.
You can also create a template( or data template) for the C1RichTextBox control to provide a consistent layout while displaying its content. Moreover, the template also helps you control how the content appears on the printed page, including margins, headers, footers, and other printing-specific elements. For example, you can display the header and footer on each page of a multi-page document while displaying it within the C1RichTextBox control.
Following section explains how you can create and customize a template using .NET and .NET Framework versions to display headers and footers on a multi-page RTF document within the C1RichTextBox control.
XAML |
Copy Code
|
---|---|
<Window.Resources> <local:PageNumberConverter x:Key="PageNumberConverter"></local:PageNumberConverter> <DataTemplate x:Key="printtemplate"> <Grid Margin="4"> <Grid.Resources> <c1:ZoomToScaleTransformConverter x:Key="zoomConverter" /> </Grid.Resources> <Border BorderThickness="1" BorderBrush="#FF646464" Grid.Row="1" Grid.Column="1" Background="{Binding ViewManager.PresenterInfo.Background}"> <Border.Effect> <DropShadowEffect Opacity="0.3" /> </Border.Effect> </Border> <c1:C1LayoutTransformer LayoutTransform="{Binding ViewManager.PresenterInfo.Zoom, Converter={StaticResource zoomConverter}}"> <Grid Height="{Binding ViewManager.PresenterInfo.Height}" Width="{Binding ViewManager.PresenterInfo.Width}"> <c1:C1RichTextPresenter Source="{Binding}" Grid.Row="1" Grid.Column="1" Margin="{Binding ViewManager.PresenterInfo.Padding}"/> <TextBlock Text="{Binding Index,StringFormat='Mark Twain'}" HorizontalAlignment="Right" VerticalAlignment="Top" FontWeight="UltraBold" FontSize="16" Margin="50" Foreground="Red" /> <TextBlock Text="{Binding Index,Converter={StaticResource PageNumberConverter}}" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontStyle="Italic" FontSize="14" Margin="50" Foreground="Green" /> </Grid> </c1:C1LayoutTransformer> </Grid> </DataTemplate> </Window.Resources> |
XAML |
Copy Code
|
---|---|
<Grid> <c1:C1RichTextBox PrintTemplate="{StaticResource printtemplate}" x:Name="rtb" Grid.Row="1" Grid.Column="1" Margin="{Binding ViewManager.PresenterInfo.Padding}"/> </Grid> |
CS |
Copy Code
|
---|---|
var stream = Application.GetResourceStream(new Uri("./../Resources/MarkTwain.rtf", UriKind.RelativeOrAbsolute)).Stream; var rtf = new StreamReader(stream).ReadToEnd(); rtb.Document = new RtfFilter().ConvertToDocument(rtf); rtb.ViewMode = TextViewMode.Print; |
VB |
Copy Code
|
---|---|
Dim stream = Application.GetResourceStream(New Uri("./../Resources/MarkTwain.rtf", UriKind.Relative)).Stream Dim rtf = New StreamReader(stream).ReadToEnd() rtb.Document = New RtfFilter().ConvertToDocument(rtf) rtb.ViewMode = TextViewMode.Print |
CS |
Copy Code
|
---|---|
public class PageNumberConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is int index) { return $"{index + 1}"; } return string.Empty; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
VB |
Copy Code
|
---|---|
Public Class PageNumberConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert If TypeOf value Is Integer Then Dim index As Integer = DirectCast(value, Integer) Return $"{index + 1}" End If Return String.Empty End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New NotImplementedException() End Function End Class |
On running the application, you can see header and footer on each page of the RTF document within the C1RichTextbox control.