简体   繁体   中英

How do I print a blank page?

Given a valid ( System.Windows.Controls. ) PrintDialog instance, what is the simplest way to just spit out a blank page out of the printer? I have a case where I can print successfully with a page (actually a Grid ) populated with printable material, but if I set all UIElement.Visibility = Visibility.Hidden; , I can see the resulting document being consumed by the print queue, but there is no acknowledgement of receipt from the printer, on its display screen or any mechanical movement.

Edit: Printing code:

MyPageToPrint myPtP = new MyPageToPrint();

foreach (UIElement elt in myPtP.MainGrid.Children)
{
    elt.Visibility = Visibility.Hidden;
}

printDialog.PrintVisual(myPtP.MainGrid, "Print blank page");
myPtP.Close();

This results in no apparent acknowledgement by the printer, but if I do manipulate the UIElement s, then it will print.

Well, your question is a bit broad because if what you mean is "print just a blank page" then that will mean something different than "print one blank page of many pages". Probably the best way is to use a paginator and for whatever page want blank, simply have a blank canvas. For example:

class Paginator : DocumentPaginator
{
    public override DocumentPage GetPage(int pageNumber)
    {
        if (pageNumber == 0)
        {
            Canvas printCanvas = new Canvas();
            printCanvas.Measure(PageSize);
            return new DocumentPage(printCanvas);
        }
        else
        {
            // deal with other pages
            throw new NotImplementedException();
        }
    }

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

    public override int PageCount
    {
        get { return 1; }
    }

    public override Size PageSize
    {
        get
        {
            return new Size(8.5,11);
        }
        set
        {
            throw new NotImplementedException();
        }
    }

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

Then, in your print click handler, you may have something like this:

private void printButton_Click(object sender, RoutedEventArgs e)
{
    var dialog = new PrintDialog();
    if (dialog.ShowDialog() == true)
    {
        var paginator = new Paginator();
        dialog.PrintDocument(paginator, "Print demo");
    }
}

For the sake of brevity I used code-behind and a click-handler; it's recommended that you'd have that in a ViewModel and connect it to the View via a command--but that's a different topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM