Tray selection using PrintDocumentWithSettings and .Net

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]}

@Greg103

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56965

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Greg103

@jishnubh

Beginning with the release 24.8, the RawKind public property will be added to the Aspose.Pdf.Printing.PaperSource class, so that you will be able to set the correct RawKind for the custom paper source.

You can modify your GetPaperSource() method the following way (using the provided ToAsposePaperSource() method to convert the System.Drawing.Printing.PaperSource to its Aspose.PDF analog while retaining the RawKind of the custom paper source):

public static Aspose.Pdf.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.ToAsposePaperSource();
}