I implemented the printer functionnality detailed on this page and I think I stumbled upon a bug in the printing behavior of System.Windows.Printing. I just added an Image in the layout, like this:
<Grid x:Name="documentRoot">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="25"/>
<RowDefinition />
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Image x:Name="logo" Source="/MyProject;component/Image/logo.jpg" Grid.Row="0" Width="265" Height="51" HorizontalAlignment="Left" Margin="0,0,0,15" VerticalAlignment="Bottom" />
<TextBlock x:Name="headerTextBlock" Grid.Row="1" HorizontalAlignment="Center" />
<TextBlock x:Name="bodyTextBlock" Grid.Row="2" TextWrapping="Wrap" />
<TextBlock x:Name="footerTextBlock" HorizontalAlignment="Center" Grid.Row="3"/>
</Grid>
What happens with this code is that whenever multiple pages have to be printed for the first time since the application has started, it will skip the first 2 pages and only print the rest of the pages. If you try to print the same thing again it will print the pages nicely as expected, and forever until you restart the application.
Please note that it has nothing to do with the added RowDefinition, as I tried to just remove the Image element and it worked perfectly fine. It is only when I put the Image element in my UserControl that the printer starts going bonkers.
So I am assuming this is yet another “minor” bug that Microsoft won’t ever bother to fix in years, such as this one or some other non-working flags in Powershell that I have had to struggle with in my early days.
This being said, I would be more than happy to be told wrong on this issue. But if I’m not, does anyone have any idea on a workaround?
Found a workaround to my problem: converting the image file into a XAML Canvas using this website, and then copy-pasting the whole Canvas code instead of the Image in my PrintPage.xaml file seems to do the trick. Even though it’s quite horrible. I know.