In upgrading from version 23.5 (Framework 4.8) to 24.3 (.Net 8), tray selection functionality no longer works. In version 23.5 which uses System.Drawing.Printing DLL, we print to Tray 3 for page 1 and another tray for other pages. During upgrade to version 24.3, which now uses Aspose.Pdf.Printing, tray selection does not work.
.Net Framework (Aspose.PDF V23.5)
string printerName = "TLCAPPSCP710NS";
var document = System.IO.File.ReadAllBytes(@"c:\users\public\HCR14-Parch88R.pdf");
var doc = new Aspose.Pdf.Document(new MemoryStream(document));
PdfViewer viewer = new PdfViewer(doc);
viewer.AutoResize = true;
viewer.AutoRotate = true;
viewer.PrintPageDialog = false;
viewer.PrintAsImage = false;
viewer.BindPdf(doc);
// Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
// Set printer name
ps.PrinterName = printerName;
pgs.PaperSize = new System.Drawing.Printing.PaperSize { RawKind = (int)System.Drawing.Printing.PaperKind.Legal };
pgs.Margins = new System.Drawing.Printing.Margins(1, 1, 1, 1);
pgs.PaperSource = GetPaperSource(printerName, "Tray 3");
ps.Copies = 1;
ps.FromPage = 1;
ps.ToPage = 1;
ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
public static System.Drawing.Printing.PaperSource GetPaperSource(string printerName, string trayName)
{
System.Drawing.Printing.PaperSource ps = null;
System.Drawing.Printing.PrintDocument prtDoc = new System.Drawing.Printing.PrintDocument();
prtDoc.PrinterSettings.PrinterName = printerName;
for (int i = 0; i < prtDoc.PrinterSettings.PaperSources.Count; i++)
{
if (prtDoc.PrinterSettings.PaperSources[i].SourceName.ToLower().Equals(trayName.ToLower()))
{
ps = prtDoc.PrinterSettings.PaperSources[i];
break;
}
}
return ps;
}
.Net 8 (Aspose.Pdf v24.3)
PdfViewer viewer = new PdfViewer(doc);
viewer.AutoResize = true;
viewer.AutoRotate = true;
viewer.PrintPageDialog = false;
viewer.PrintAsImage = false;
viewer.BindPdf(doc);
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set printer name
ps.PrinterName = printerName;
//pgs.PaperSize = Aspose.Pdf.Printing.PaperSizes.Legal;
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("Legal", 850, 1400);
pgs.Margins = new Aspose.Pdf.Devices.Margins(1, 1, 1, 1);
pgs.PaperSource = GetPaperSource("Tray 3");
ps.Copies = 1;
ps.FromPage = 1;
ps.ToPage = 1;
ps.PrintRange = Aspose.Pdf.Printing.PrintRange.SomePages;
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
private static void PdfQueryPageSettingsEventHandler(object sender, Aspose.Pdf.Printing.PdfQueryPageSettingsEventArgs queryPageSettingsEventArgs, PdfPrintPageInfo currentPageInfo)
{
bool isOdd = currentPageInfo.PageNumber % 2 != 0;
if (isOdd)
{
// Set the correct paper size for the requested tray
queryPageSettingsEventArgs.PageSettings.PaperSize = PaperSizes.Letter;
queryPageSettingsEventArgs.PageSettings.PaperSource = GetPaperSource("Tray 3");
}
else
{
queryPageSettingsEventArgs.PageSettings.PaperSize = PaperSizes.Legal;
queryPageSettingsEventArgs.PageSettings.PaperSource = GetPaperSource("Tray 4");
}
}
Printer papersources are:
0 - {[PaperSource Automatically Select Kind=FormSource]}
1 - {[PaperSource Use printer settings Select Kind=Custom]}
2 - {[PaperSource Tray 1 Select Kind=Custom]}
3 - {[PaperSource Tray 2 Select Kind=Custom]}
4 - {[PaperSource Tray 3 Select Kind=Custom]}
5 - {[PaperSource Tray 4 Select Kind=Custom]}
6 - {[PaperSource Tray 5 Select Kind=Custom]}
7 - {[PaperSource Manual Paper Kind=Custom]}
8 - {[PaperSource Manual Envelope Kind=Custom]}