在多个纸盘上打印文件

时间:2018-08-09 07:30:32

标签: c# printing

我有一个文档,其中包含第一页,该页应打印在第三个纸盘(公司模板纸)上。顺序的页面应打印在第二个纸盘(白纸)上。我已经阅读了https://chrizyuen.wordpress.com/2011/03/11/how-to-select-different-tray-for-different-page-for-printdocument-with-c/上的示例,该示例显示了如何完成此操作。但是由于某种原因,纸张来源会粘在纸盘3上。

这是我到目前为止尝试过的:

    private static int _pageNum = 1;
    private static PrintDocument _printDocument;
    private static PrinterSettings _printerSettings;
    private static PageSettings _pageSettings;
    private static PrinterOption _printOptions;
    private static int _totalpage;

    private static void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
    {
        switch (_pageNum)
        {
            case 1:
                foreach (PaperSource paperSource in _printDocument.PrinterSettings.PaperSources)
                {
                    if (_printOptions.FirstPageTraySource.ToLower()
                        .Equals(paperSource.SourceName.ToLower()))
                    {
                        e.PageSettings.PaperSource = paperSource;
                    }
                }
                break;

            default:
                foreach (PaperSource paperSource in _printDocument.PrinterSettings.PaperSources)
                {
                    if (_printOptions.SequentialPageTraySource.ToLower()
                        .Equals(paperSource.SourceName.ToLower()))
                    {
                        e.PageSettings.PaperSource = paperSource;
                    }
                }
                break;
        }
    }

    private static void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.HasMorePages = _pageNum < _totalpage;

        _pageNum++;
    }

    private static bool PrintFromMultiTraySource(
            byte[] pdfbytes,
            string printername,
            PrinterOption options)
    {
        try
        {
            // Create the printer settings for our printer
            _printerSettings = GetPrinterSettings(printername, options);
            _pageSettings = GetPageSettings(_printerSettings, options);

            using (Stream stream = new MemoryStream(pdfbytes))
            {
                // Now print the PDF document
                using (var document = PdfDocument.Load(stream))
                {
                    _printDocument = document.CreatePrintDocument();
                    _printDocument.PrinterSettings = _printerSettings;

                    // _printDocument.DefaultPageSettings = _pageSettings;
                    // 

                    _printDocument.PrintPage += PrintDoc_PrintPage;
                    _printDocument.QueryPageSettings += PrintDoc_QueryPageSettings;

                    _totalpage = document.PageCount;
                    _printOptions = options;

                    //print the first page
                    if (!printername.ToLower().Equals("debug"))
                    {
                        _printDocument.Print();
                    }
                    return true;
                }
            }
        }
        catch (Exception ex)
        {
            Serilog.Log.Error(ex, "Failed to print file");
        }
        return false;
    }

0 个答案:

没有答案