Álvaro García
Álvaro García

Reputation: 19356

The document is empty when I try to print a WPF user control

I would like to print some user controls that has a A4 size. but the result is an empty document of a size of 0 bytes.

To do that, I am extended DocumentPaginator. The code is this:

class DocumentPaginatorImpl : DocumentPaginator { private List Pages { get; set; }

public DocumentPaginatorImpl(List<UIElement> pages)
{
    Pages = pages;
}

public override DocumentPage GetPage(int pageNumber)
{
    return new DocumentPage(Pages[pageNumber]);
}

public override bool IsPageCountValid
{
    get { return true; }
}

public override int PageCount
{
    get { return Pages.Count; }
}

public override System.Windows.Size PageSize
{
    get
    {
        /* Assume the first page is the size of all the pages, for simplicity. */
        if (Pages.Count > 0)
        {
            UIElement page = Pages[0];

            if (page is Canvas)
                return new Size(((Canvas)page).Width, ((Canvas)page).Height);
            // else if ...
        }

        return Size.Empty;
    }
    set
    {
        /* Ignore the PageSize suggestion. */
    }
}

public override IDocumentPaginatorSource Source
{
    get { return null; }
}

}

And the method that prints:

    public static void ImprimirWpfV2(IEnumerable<UIElement> paramIeViewsParaImprimir)
    {
        System.Windows.Controls.PrintDialog dlg = new System.Windows.Controls.PrintDialog();
        dlg.PrintDocument(new DocumentPaginatorImpl(paramIeViewsParaImprimir.ToList()), "Print Job Description");
    }

But the document that I get is an empty document.

Thanks.

Upvotes: 0

Views: 134

Answers (1)

Ohad Cohen
Ohad Cohen

Reputation: 617

You can print as follows:

var userControl = yourUserControl as UserControl;
var pd = new PrintDialog();
pd.PrintVisual(userControl, "");

Upvotes: 1

Related Questions