Condividi tramite


Procedura: convalidare e unire PrintTicket

Aggiornamento: novembre 2007

Una novità di Windows Vista è lo schema di stampa, che include gli elementi flessibili ed estensibili PrintCapabilities e PrintTicket. Il primo specifica le funzionalità di un dispositivo di stampa, mentre il secondo specifica in che modo il dispositivo deve utilizzare tali funzionalità in relazione a una determinata sequenza di documenti, a un singolo documento o a una singola pagina.

Di seguito viene riportata una sequenza di attività tipica per un'applicazione che supporta la stampa.

  1. Determinare le funzionalità di una stampante.

  2. Configurare PrintTicket affinché utilizzi tali funzionalità.

  3. Convalidare PrintTicket.

In questo articolo viene illustrato come eseguire queste operazioni.

Esempio

Nel semplice esempio riportato di seguito viene unicamente stabilito se la stampante è in grado di supportare la stampa fronte retro. La procedura principale è la seguente.

  1. Ottenere un oggetto PrintCapabilities con il metodo GetPrintCapabilities.

  2. Verificare la presenza della funzionalità desiderata. Nell'esempio riportato di seguito viene eseguito il test della proprietà DuplexingCapability dell'oggetto PrintCapabilities per verificare la presenza della funzionalità di stampa fronte retro con il capovolgimento della pagina sul lato lungo. Poiché DuplexingCapability è un insieme, viene utilizzato il metodo Contains di ReadOnlyCollection<T>.

    Nota

    Questo passaggio non è strettamente necessario. Tramite il metodo MergeAndValidatePrintTicket utilizzato di seguito verrà controllata ogni richiesta in PrintTicket rispetto alle funzionalità della stampante. Se la funzionalità richiesta non è supportata dalla stampante, il driver della stampante sostituirà una richiesta alternativa nell'oggetto PrintTicket restituito dal metodo.

  3. Se la stampante supporta la stampa fronte retro, nel codice di esempio viene creato un oggetto PrintTicket che richiede la stampa fronte retro. L'applicazione, tuttavia, non specifica ogni possibile impostazione della stampante disponibile nell'elemento PrintTicket. Questa operazione implicherebbe un notevole dispendio di tempo da parte del programmatore e del programma. Nel codice, invece, viene impostata solo la richiesta di stampa fronte retro e, successivamente, questo oggetto PrintTicket viene unito a un oggetto PrintTicket esistente, completamente configurato e convalidato, in questo caso l'oggetto PrintTicket predefinito dell'utente.

  4. Di conseguenza, nell'esempio viene chiamato il metodo MergeAndValidatePrintTicket per unire il nuovo oggetto PrintTicket minimo all'oggetto PrintTicket predefinito dell'utente. Viene restituito un oggetto ValidationResult che include il nuovo oggetto PrintTicket come una sua proprietà.

  5. Nell'esempio viene quindi testato che il nuovo oggetto PrintTicket richieda la stampa fronte retro. In caso affermativo, viene impostato come nuovo Print Ticket predefinito per l'utente. Se il passaggio 2 precedente viene omesso e la stampante non supporta la stampa fronte retro sul lato lungo, il risultato del test sarebbe false. Vedere la nota riportata sopra.

  6. L'ultimo passaggio significativo consiste nell'eseguire il commit della modifica apportata alla proprietà UserPrintTicket dell'oggetto PrintQueue con il metodo Commit.

/// <summary>
/// Changes the user-default PrintTicket setting of the specified print queue.
/// </summary>
/// <param name="queue">the printer whose user-default PrintTicket setting needs to be changed</param>
static private void ChangePrintTicketSetting(PrintQueue queue)
{
    //
    // Obtain the printer's PrintCapabilities so we can determine whether or not
    // duplexing printing is supported by the printer.
    //
    PrintCapabilities printcap = queue.GetPrintCapabilities();

    //
    // The printer's duplexing capability is returned as a read-only collection of duplexing options
    // that can be supported by the printer. If the collection returned contains the duplexing
    // option we want to set, it means the duplexing option we want to set is supported by the printer,
    // so we can make the user-default PrintTicket setting change.
    //
    if (printcap.DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge))
    {
        //
        // To change the user-default PrintTicket, we can first create a delta PrintTicket with
        // the new duplexing setting.
        //
        PrintTicket deltaTicket = new PrintTicket();
        deltaTicket.Duplexing = Duplexing.TwoSidedLongEdge;

        //
        // Then merge the delta PrintTicket onto the printer's current user-default PrintTicket,
        // and validate the merged PrintTicket to get the new PrintTicket we want to set as the
        // printer's new user-default PrintTicket.
        //
        ValidationResult result = queue.MergeAndValidatePrintTicket(queue.UserPrintTicket, deltaTicket);

        //
        // The duplexing option we want to set could be constrained by other PrintTicket settings
        // or device settings. We can check the validated merged PrintTicket to see whether the
        // the validation process has kept the duplexing option we want to set unchanged.
        //
        if (result.ValidatedPrintTicket.Duplexing == Duplexing.TwoSidedLongEdge)
        {
            //
            // Set the printer's user-default PrintTicket and commit the set operation.
            //
            queue.UserPrintTicket = result.ValidatedPrintTicket;
            queue.Commit();
            Console.WriteLine("PrintTicket new duplexing setting is set on '{0}'.", queue.FullName);
        }
        else
        {
            //
            // The duplexing option we want to set has been changed by the validation process
            // when it was resolving setting constraints.
            //
            Console.WriteLine("PrintTicket new duplexing setting is constrained on '{0}'.", queue.FullName);
        }
    }
    else
    {
        //
        // If the printer doesn't support the duplexing option we want to set, skip it.
        //
        Console.WriteLine("PrintTicket new duplexing setting is not supported on '{0}'.", queue.FullName);
    }
}

Allo scopo di eseguire rapidamente il test di questo esempio, la parte restante viene presentata di seguito. Creare un progetto e uno spazio dei nomi, quindi incollare entrambi i frammenti di codice di questo articolo nel blocco dello spazio dei nomi.

/// <summary>
/// Displays the correct command line syntax to run this sample program.
/// </summary>
static private void DisplayUsage()
{
    Console.WriteLine();
    Console.WriteLine("Usage #1: printticket.exe -l \"<printer_name>\"");
    Console.WriteLine("      Run program on the specified local printer");
    Console.WriteLine();
    Console.WriteLine("      Quotation marks may be omitted if there are no spaces in printer_name.");
    Console.WriteLine();
    Console.WriteLine("Usage #2: printticket.exe -r \"\\\\<server_name>\\<printer_name>\"");
    Console.WriteLine("      Run program on the specified network printer");
    Console.WriteLine();
    Console.WriteLine("      Quotation marks may be omitted if there are no spaces in server_name or printer_name.");
    Console.WriteLine();
    Console.WriteLine("Usage #3: printticket.exe -a");
    Console.WriteLine("      Run program on all installed printers");
    Console.WriteLine();
}


[STAThread]
static public void Main(string[] args)
{
    try
    {
        if ((args.Length == 1) && (args[0] == "-a"))
        {
            //
            // Change PrintTicket setting for all local and network printer connections.
            //
            LocalPrintServer server = new LocalPrintServer();

            EnumeratedPrintQueueTypes[] queue_types = {EnumeratedPrintQueueTypes.Local,
                                                       EnumeratedPrintQueueTypes.Connections};

            //
            // Enumerate through all the printers.
            //
            foreach (PrintQueue queue in server.GetPrintQueues(queue_types))
            {
                //
                // Change the PrintTicket setting queue by queue.
                //
                ChangePrintTicketSetting(queue);
            }
        }//end if -a

        else if ((args.Length == 2) && (args[0] == "-l"))
        {
            //
            // Change PrintTicket setting only for the specified local printer.
            //
            LocalPrintServer server = new LocalPrintServer();
            PrintQueue queue = new PrintQueue(server, args[1]);
            ChangePrintTicketSetting(queue);
        }//end if -l

        else if ((args.Length == 2) && (args[0] == "-r"))
        {
            //
            // Change PrintTicket setting only for the specified remote printer.
            //
            String serverName = args[1].Remove(args[1].LastIndexOf(@"\"));
            String printerName = args[1].Remove(0, args[1].LastIndexOf(@"\")+1);
            PrintServer ps = new PrintServer(serverName);
            PrintQueue queue = new PrintQueue(ps, printerName);
            ChangePrintTicketSetting(queue);
         }//end if -r

        else
        {
            //
            // Unrecognized command line.
            // Show user the correct command line syntax to run this sample program.
            //
            DisplayUsage();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);

        //
        // Show inner exception information if it's provided.
        //
        if (e.InnerException != null)
        {
            Console.WriteLine("--- Inner Exception ---");
            Console.WriteLine(e.InnerException.Message);
            Console.WriteLine(e.InnerException.StackTrace);
        }
    }
    finally
    {
        Console.WriteLine("Press Return to continue...");
        Console.ReadLine();
    }
}//end Main

Vedere anche

Concetti

Documenti di Windows Presentation Foundation

Cenni preliminari sulla stampa

Riferimenti

PrintCapabilities

PrintTicket

GetPrintQueues

PrintServer

EnumeratedPrintQueueTypes

PrintQueue

GetPrintCapabilities

Altre risorse

Esempi relativi alla stampa