Friday, May 16, 2014

Get paper trays available on network printer

(this is a job)

static void PrinterTest(Args _args)
{
    System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
    System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
    System.Drawing.Printing.PaperSource[] printSource;
    System.Drawing.Printing.PaperSource printSourceMain;
    System.Exception printException;
    int trayCount, totalCount;
    int rawId;
    str trayName;
   
    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        //set which printer we need to query
        printSettings.set_PrinterName(@"\\server\networkprintersharename");
        printDocument.set_PrinterSettings(printSettings);
        //load in all of the current trays(paper sources_ for the printer and get the count
        printSource = printSettings.get_PaperSources();
        totalCount = printSource.get_Count();
        //loop through all of the trays but make sure the raw kindid is less than 1000 to get valid trays that we can print to
        for(trayCount = 0; trayCount < totalCount; ++trayCount)
        {
            printSourceMain = new System.Drawing.Printing.PaperSource();
            //get the current tray
            printSourceMain = printSource.get_Item(trayCount);
            //get the trays raw kind id
            rawId = printSourceMain.get_RawKind();
            //check to see if the tray is a custom object or something we can print to
            if(rawId < 1000)
            {
                //get the trays name
                trayName = printSourceMain.get_SourceName();
                info(trayName);  
            }
        }
    }
    catch
    {
        printException = CLRInterop::getLastException();
        info(printException.ToString());
    }
}


No comments:

Post a Comment