C# WPF Paginator printer ignoring user's printer selection
I am using the following code in my project. The print dialog shows, but it always prints on the default printer regardless of the user's selection. I have read similar topics but none of them seem to use the SerializerWriterCollator. What is the problem?
PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
XpsDocumentWriter xpsWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);开发者_如何学Python
SerializerWriterCollator batchPrinter = xpsWriter.CreateVisualsCollator();
var printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
PrintTicket ticket = printDialog.PrintTicket;
ticket.PageOrientation = PageOrientation.Landscape;
var paginator1 = new PagePrinter(winchFlightsCount,
new Size(printDialog.PrintableAreaWidth,
printDialog.PrintableAreaHeight), winchFlights);
var paginator2 = new PagePrinter(tugFlightCount,
new Size(printDialog.PrintableAreaWidth,
printDialog.PrintableAreaHeight), tugFlights);
var paginator3 = new PagePrinter(selfFlightCount,
new Size(printDialog.PrintableAreaWidth,
printDialog.PrintableAreaHeight), selfFlights);
batchPrinter.BeginBatchWrite();
for (int i = 0; i < paginator1.PageCount; i++)
{
batchPrinter.Write(paginator1.GetPage(i).Visual, ticket);
}
for (int i = 0; i < paginator2.PageCount; i++)
{
batchPrinter.Write(paginator2.GetPage(i).Visual, ticket);
}
for (int i = 0; i < paginator3.PageCount; i++)
{
batchPrinter.Write(paginator3.GetPage(i).Visual, ticket);
}
batchPrinter.EndBatchWrite();
}
}
It's because your batchPrinter is created from the default PrintQueue from the LocalPrintServer rather than from the printDialog. Try this at the start:
var printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
PrintQueue printQueue = printDialog.PrintQueue;
XpsDocumentWriter xpsWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
SerializerWriterCollator batchPrinter = xpsWriter.CreateVisualsCollator();
精彩评论