The problem is that when I print the FlowDocument
, it prints the same as what the Window
is showing, and not the FlowDocument
itself.
Here's the XAML:
<Window x:Class="POS.DailySalesReport"
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:POS"
mc:Ignorable="d"
Height="500"
Width="816"
Loaded="Window_Loaded"
WindowStartupLocation="CenterScreen">
<Grid>
<FlowDocumentPageViewer x:Name="flwView">
<FlowDocument x:Name="flwDoc"
TextAlignment="Center"
ColumnWidth="{Binding ElementName=flwView, Path=ActualWidth}">
<BlockUIContainer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid x:Name="grdDetails"
Grid.Row="0"
Grid.Column="0"
Margin="0,0,0,30">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Ellipse Grid.Row="0"
Grid.Column="0"
Height="90"
Width="90"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush x:Name="imgUser" />
</Ellipse.Fill>
</Ellipse>
<Grid Grid.Row="0"
Grid.Column="1"
Margin="10, 0, 0, 0">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="lblCompanyName"
Grid.Row="0"
Grid.Column="0"
Text="COMPANY NAME"
FontFamily="{StaticResource RobotoRegular}"
FontSize="18"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyAddress"
Grid.Row="1"
Grid.Column="0"
Text="Business Address"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyNumber"
Grid.Row="2"
Grid.Column="0"
Text="Contact Number"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyTIN"
Grid.Row="3"
Grid.Column="0"
Text="TIN Number"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
</Grid>
<StackPanel Grid.Row="0"
Grid.Column="2">
<TextBlock x:Name="lblReportType"
FontFamily="{StaticResource RobotoBold}"
FontSize="12"
HorizontalAlignment="Right" />
<TextBlock x:Name="lblReportDate"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
HorizontalAlignment="Right" />
</StackPanel>
</Grid>
<ListView x:Name="lstView"
Grid.Row="1"
Grid.Column="0"
BorderThickness="0"
SelectionMode="Single"
Width="{Binding ElementName=flwDoc, Path=ActualWidth}">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" />
</Setter.Value>
</Setter>
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="Black" />
</Setter.Value>
</Setter>
<Setter Property="FontSize"
Value="11" />
<Setter Property="FontFamily"
Value="{StaticResource RobotoMedium}" />
<Setter Property="Height"
Value="35" />
<Setter Property="BorderThickness"
Value="0, 1, 0, 1" />
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="Black" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="Black"
Opacity="0.8" />
</Setter.Value>
</Setter>
<Setter Property="FontFamily"
Value="{StaticResource RobotoMedium}" />
<Setter Property="FontSize"
Value="17" />
<Setter Property="Background"
Value="White" />
<Setter Property="Padding"
Value="0, 10, 0, 10" />
<Setter Property="Margin"
Value="0, 0, 0, 10" />
<Style.Resources>
<!-- Foreground for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black"
Opacity="0.8" />
<!-- Background for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="White" />
<!-- Foreground for Inactive Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black"
Opacity="0.8" />
<!-- Background for Inactive Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="White" />
</Style.Resources>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn x:Name="lstHeader1"
Header="Description">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductDescription}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader2"
Header="Supplier">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Supplier}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader3"
Header="Sold">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductSold}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader4"
Header="Cost Price">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductCost}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader5"
Header="Retail Price">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductRetail}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader6"
Header="Total(Qty * Retail Price)">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TotalRetail}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Grid Grid.Row="2"
Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0"
Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="Total Items Sold"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalSold"
Grid.Row="0"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="Total Cost Amount"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalCost"
Grid.Row="1"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="2"
Grid.Column="0"
Text="Total Retail Amount"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalRetail"
Grid.Row="2"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="3"
Grid.Column="0"
Text="Profit"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblProfit"
Grid.Row="3"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
</Grid>
</Grid>
</Grid>
</BlockUIContainer>
</FlowDocument>
</FlowDocumentPageViewer>
</Grid>
</Window>
Overall, the items in the ListView
is dynamic. The problem appears when the list became longer than the height of the Window
, ruining the appearance of the report (like the ellipse
), and it is the same as the printed version.
Here's the code-behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new PrintDialog();
var actualWidth = lstView.ActualWidth - 10;
lstHeader1.Width = actualWidth / 6;
lstHeader2.Width = actualWidth / 6;
lstHeader3.Width = actualWidth / 6;
lstHeader4.Width = actualWidth / 6;
lstHeader5.Width = actualWidth / 6;
lstHeader6.Width = actualWidth / 6;
PopulateListView(ChosenDate);
string businessName = "";
string businessAddress = "";
string businessTIN = "";
string businessContact = "";
string businessImage = "";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string query = "SELECT * FROM dbo.Company";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
try
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
businessName = reader["Name"].ToString();
businessAddress = reader["Address"].ToString();
businessTIN = reader["TIN"].ToString();
businessContact = reader["Contact"].ToString();
businessImage = reader["ImagePath"].ToString();
}
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message + err.StackTrace);
}
}
}
BitmapImage img;
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + businessImage))
{
img = new BitmapImage();
img.BeginInit();
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + businessImage);
img.EndInit();
}
else
{
img = new BitmapImage(new Uri("pack://application:,,,/Resources/default_product_img.png"));
}
imgUser.ImageSource = img;
lblCompanyName.Text = businessName;
lblCompanyAddress.Text = businessAddress;
lblCompanyTIN.Text = businessTIN;
lblCompanyNumber.Text = businessContact;
lblReportType.Text = ReportType;
lblReportDate.Text = ChosenDate.ToString("MM/dd/yyyy");
lblTotalSold.Text = TotalSold.ToString();
lblTotalCost.Text = TotalCost.ToString("F2");
lblTotalRetail.Text = TotalRetail.ToString("F2");
lblProfit.Text = TotalProfit.ToString("F2");
IDocumentPaginatorSource idpSource = flwDoc;
var result = printDlg.ShowDialog();
if (result == true)
{
printDlg.PrintDocument(idpSource.DocumentPaginator, "Document");
}
}
I thought that the UI appearing in the Window
is just for visual display, and not really what the PrintDialog
prints, but the FlowDocument
itself. If it is not possible, I'm planning to re-create the controls in code-behind just to maintain responsiveness. So I'm asking here first if there is a fix for this.