C#WPF Paginator打印机忽略用户的打印机选择

时间:2010-05-30 17:41:34

标签: c# wpf printing

我在我的项目中使用以下代码。打印对话框显示,但无论用户选择什么,它始终打印在默认打印机上。我已经阅读了类似的主题,但似乎没有人使用SerializerWriterCollat​​or。有什么问题?

            PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
            XpsDocumentWriter xpsWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
            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();
            }
        }

1 个答案:

答案 0 :(得分:2)

这是因为您的batchPrinter是从LocalPrintServer的默认PrintQueue创建的,而不是从printDialog创建的。一开始就试试这个:

var printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
    PrintQueue printQueue = printDialog.PrintQueue;
    XpsDocumentWriter xpsWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
    SerializerWriterCollator batchPrinter = xpsWriter.CreateVisualsCollator();