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

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()

        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));

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

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

            PageContent miContenido1 = new PageContent();


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

            if (myDialog.ShowDialog() == true)
                    //Print the image.
                    myDialog.PrintDocument(miDocumento.DocumentPaginator, string.Empty);
                    //@#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)

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

<Window x:Class="ImprimirControlesUsuario.MainWindow"
        Title="MainWindow" Height="450" Width="800">

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

There is no code behind in this user control.


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
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