I have a document which contains a first page which should be printed on the third tray (company templated paper). The sequential pages should be printed on the second tray (blanco paper). Ive read the example at https://chrizyuen.wordpress.com/2011/03/11/how-to-select-different-tray-for-different-page-for-printdocument-with-c/ which shows how to accomplish such a thing. But for some reason the papersource sticks to tray 3.
This is what i have tried so far:
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;
}