70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
|
|
class TrayTest
|
|
{
|
|
static void Main()
|
|
{
|
|
var printerName = "Brother HL-L6415DW series Printer";
|
|
var trayNumbers = new int[] { 1, 2, 256, 257, 270 }; // Tray 1, 2, 3, 4, 5
|
|
var currentPage = 0;
|
|
|
|
var printDoc = new PrintDocument
|
|
{
|
|
PrinterSettings = {
|
|
PrinterName = printerName,
|
|
Duplex = Duplex.Simplex // Force single-sided
|
|
}
|
|
};
|
|
|
|
// QueryPageSettings fires BEFORE PrintPage - this is where we set the tray
|
|
printDoc.QueryPageSettings += (sender, e) =>
|
|
{
|
|
if (e.PageSettings == null)
|
|
return;
|
|
|
|
var trayNumber = trayNumbers[currentPage];
|
|
|
|
// Set the tray for this page
|
|
PaperSource? paperSource = null;
|
|
foreach (PaperSource source in printDoc.PrinterSettings.PaperSources)
|
|
{
|
|
if (source.RawKind == trayNumber)
|
|
{
|
|
paperSource = source;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (paperSource != null)
|
|
{
|
|
e.PageSettings.PaperSource = paperSource;
|
|
Console.WriteLine($"Page {currentPage + 1}: Using tray {trayNumber} ({paperSource.SourceName})");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Page {currentPage + 1}: WARNING - Tray {trayNumber} not found!");
|
|
}
|
|
};
|
|
|
|
printDoc.PrintPage += (sender, e) =>
|
|
{
|
|
if (e.Graphics == null)
|
|
return;
|
|
|
|
// Draw the page number
|
|
var font = new Font("Courier New", 24, FontStyle.Bold);
|
|
var text = $"Page #{currentPage + 1}";
|
|
e.Graphics.DrawString(text, font, Brushes.Black, 100, 100);
|
|
|
|
currentPage++;
|
|
e.HasMorePages = currentPage < trayNumbers.Length;
|
|
};
|
|
|
|
Console.WriteLine($"Printing 5 pages to {printerName}...");
|
|
printDoc.Print();
|
|
Console.WriteLine("Done!");
|
|
}
|
|
}
|