Export (0) Print
Expand All

Finding Installed Printers with the Script Task

The data that is transformed by Integration Services packages often has a printed report as its final destination. The System.Drawing.Printing namespace in the Microsoft .NET Framework provides classes for working with printers.

NoteNote

If you want to create a task that you can more easily reuse across multiple packages, consider using the code in this Script task sample as the starting point for a custom task. For more information, see Developing a Custom Task.

The following example locates printers installed on the server that support legal size paper (as used in the United States). The code to check supported paper sizes is encapsulated in a private function. To enable you to track the progress of the script as it checks the settings for each printer, the script uses the Log method to raise an informational message for printers with legal size paper, and to raise a warning for printers without legal size paper. These messages appear in the Output window of the Microsoft Visual Studio Tools for Applications (VSTA) IDE when you run the package in the designer.

To configure this Script Task example

  1. Create the variable named PrinterList with type Object.

  2. On the Script page of the Script Task Editor, add this variable to the ReadWriteVariables property.

  3. In the script project, add a reference to the System.Drawing namespace.

  4. In your code, use Imports statements to import the System.Collections and the System.Drawing.Printing namespaces.

Code

public void Main()
        {

            PrinterSettings currentPrinter = new PrinterSettings();
            PaperSize size;
            Boolean Flag = false;

            ArrayList printerList = new ArrayList();
            foreach (string printerName in PrinterSettings.InstalledPrinters)
            {
                currentPrinter.PrinterName = printerName;
                if (PrinterHasLegalPaper(currentPrinter))
                {
                    printerList.Add(printerName);
                    Dts.Events.FireInformation(0, "Example", "Printer " + printerName + " has legal paper.", String.Empty, 0, ref Flag);
                }
                else
                {
                    Dts.Events.FireWarning(0, "Example", "Printer " + printerName + " DOES NOT have legal paper.", String.Empty, 0);
                }
            }

            Dts.Variables["PrinterList"].Value = printerList;

            Dts.TaskResult = (int)ScriptResults.Success;

        }

        private bool PrinterHasLegalPaper(PrinterSettings thisPrinter)
        {

            bool hasLegal = false;

            foreach (PaperSize size in thisPrinter.PaperSizes)
            {
                if (size.Kind == PaperKind.Legal)
                {
                    hasLegal = true;
                }
            }

            return hasLegal;

        }
Integration Services icon (small) Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN or TechNet:

For automatic notification of these updates, subscribe to the RSS feeds available on the page.

Community Additions

ADD
Show:
© 2014 Microsoft