Reputation: 2359
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.
Upvotes: 0
Views: 1590
Reputation: 35881
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.
Upvotes: 1