When I print to pdf with Microsoft to PDF printer a WPF user control with an image backgruound I get an empty age

ComptonAlvaro 166 Reputation points
2022-04-25T11:25:04.89+00:00

I want to print a user control to PDF. This user control has an image as background, but when I use Microsoft to PDF printer, I get an empty page. however, if I use another virtual pdf printer like Cute PDF, bullzip or PDF Creatir, I get the expected result. Also if I print in XPS with Microsoft XPS printer I get the expected result.

Also I have realized that if I use the image as background of the user control, it is printed as expected with Microsoft to PDF printer.

So it seems the problem is with the Microsoft to PDF printer in the case that I use a backgruound.

The code of the application that I use is this:

The code behind of the main window:

using System.Windows.Markup;
using System.Linq;
using System.Windows;
using System.Windows.Documents;

namespace ImprimirControlesUsuario
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }




        private void Imprimir()
        {
            ControlUsuarioParaImprimir miControlUsuarioParaImprimir = new ControlUsuarioParaImprimir();


            FixedDocument miDocumento = new FixedDocument();


            miControlUsuarioParaImprimir.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            miControlUsuarioParaImprimir.Arrange(new Rect(miControlUsuarioParaImprimir.DesiredSize));
            miControlUsuarioParaImprimir.UpdateLayout();

            Size miTamañoPagina = new Size(miControlUsuarioParaImprimir.ActualWidth, miControlUsuarioParaImprimir.ActualHeight);


            FixedPage miPagina1 = new FixedPage();
            miPagina1.Width = miTamañoPagina.Width;
            miPagina1.Height = miTamañoPagina.Height;
            miPagina1.Children.Add(miControlUsuarioParaImprimir);


            PageContent miContenido1 = new PageContent();
            ((IAddChild)miContenido1).AddChild(miPagina1);

            miDocumento.Pages.Add(miContenido1);




            System.Windows.Controls.PrintDialog myDialog = new System.Windows.Controls.PrintDialog();

            if (myDialog.ShowDialog() == true)
            {
                try
                {
                    //Print the image.
                    myDialog.PrintDocument(miDocumento.DocumentPaginator, string.Empty);
                }
                catch
                {
                    //@#MEJORAR: realmente una librería nunca debería tener un messagebox. Se tiene que lanzar una excepción.
                    MessageBox.Show("El documento no se ha podido crear.\r\n\r\n"
                        + "Si está abierto y se está intentando sobreescribir, se recomienda cerrar el documento antes"
                        + " de imprimirlo.");
                }
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Imprimir();
        }
    }
}

The xaml of the user control that I want to print:

<Window x:Class="ImprimirControlesUsuario.MainWindow"
        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:ImprimirControlesUsuario"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">


    <Grid>
        <Button Content="Imprimir" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click"/>
    </Grid>
</Window>

There is no code behind in this user control.

Thanks.

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,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,629 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,937 questions
{count} votes