Need help to throw exception or raise alarm from Printjob if the desired source tray not selected

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF PrintManager for .NET (v4)  /  Need help to throw exception or raise alarm from Printjob if the desired source tray not selected

DynamicPDF PrintManager for .NET (v4) Forum

HI Team,
can i ask how to raise alarm or raise failed print job event if the print job is not having  desired paper source tray select. i have created below source code  and i realize if i am not setting any paper source tray option, API is using "Automatically select" as the paperSource Tray.

        private void PrintCO(PrintType printType) {
            try {
                if (null == this.printerConfiguration) {
                    return;
                }
                //TestPrintJob_Succeeded();
                String tempFile = Path.GetTempFileName();
                byte[] pdfContent = null;
                switch (printType) {
                    case PrintType.ORIGINAL:
                        pdfContent = System.Convert.FromBase64String(coDoc.orignal);
                        break;

                    case PrintType.COPY:
                        pdfContent = System.Convert.FromBase64String(coDoc.copy);
                        break;
                }
                File.WriteAllBytes(tempFile, pdfContent);
                PrintJob printJob = new PrintJob(this.printerConfiguration.Name, tempFile);
                printJob.DocumentName = "Letter Portrait";
                if (printJob.Printer.Color)
                    printJob.PrintOptions.Color = true;
                printJob.PrintOptions.Copies = 1;
                PaperSize paperSize = printJob.Printer.PaperSizes.A4;
                if (paperSize != null) printJob.PrintOptions.PaperSize = paperSize;
                printJob.PrintOptions.HorizontalAlign = HorizontalAlign.Center;
                printJob.PrintOptions.Orientation.Type = OrientationType.Portrait;
                AutoPageScaling scaling = new AutoPageScaling(ScaleTo.PagePrintableArea, true, true);
                printJob.PrintOptions.Scaling = PageScaling.ActualSize;
                printJob.PrintOptions.Scaling = scaling;
                switch (coDoc.coMediaType) {
                    case "PrePrinted":
                        if (0 != this.printerConfiguration.PinkTray) printJob.PrintOptions.PaperSource = printJob.Printer.PaperSources[this.printerConfiguration.PinkTray];
                        break;

                    case "A4":
                        if (0 != this.printerConfiguration.WhiteTray) printJob.PrintOptions.PaperSource = printJob.Printer.PaperSources[this.printerConfiguration.WhiteTray];
                        break;

                    case "PrePrintedWithLogo":
                        if (0 != this.printerConfiguration.PPWithLogoTray) printJob.PrintOptions.PaperSource = printJob.Printer.PaperSources[this.printerConfiguration.PPWithLogoTray];
                        break;
                }
                printJob.PrintOptions.PrintAnnotations = false;
                printJob.PrintOptions.Resolution = printJob.Printer.Resolutions[this.printerConfiguration.Resolution];
                printJob.PrintOptions.VerticalAlign = VerticalAlign.Top;
                printJob.Succeeded += PrintJob_Succeeded;
                printJob.Failed += PrintJob_Failed;
                printJob.Updated += PrintJob_Updated;
                        //at this point in printing i want to raise an alarm if the printJob.PrintOptions.PaperSource is "Automatically Select"
                printJob.Print();
            } catch (Exception ex) {
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }
Posted by a ceTe Software moderator
Hello,

Yes, you can throw an exception if PrintJob is not set with paper source other than Auto. You will need to check for the paper source set for the PrintJob dynamically and raise an exception. Below is the code sample.

            PrintJob printJob = new PrintJob("Printer name", @"PDF file path");
            PaperSource paperSourceObj=printJob.PrintOptions.PaperSource;
            if (paperSourceObj.Name.Equals("Automatically Select"))
            {
                throw new Exception("Paper Source is set to Auto");
            }
           
            printJob.Print();

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 8:40 PM.