Compartilhar via


Como: Validação e mescla de PrintTickets

Novo para Windows Vista é o Esquema de impressão que inclui o flexível e extensível PrintCapabilities e PrintTicket elementos. O primeiro item-iza os recursos de um dispositivo de impressão e o último especifica como o dispositivo deve usar esses recursos com relação a uma sequência específica de documentos, a um documento individual ou a a uma página individual.

Uma sequência típica de tarefas para um aplicativo que ofereça suporte a impressão seria a seguinte.

  1. Determinar os recursos da impressora.

  2. Configurar um PrintTicket para usar esses recursos.

  3. Validar o PrintTicket.

Este artigo mostra como fazer isso.

Exemplo

No exemplo simples abaixo, estamos interessados apenas em se uma impressora pode suportar duplexing — impressão de dois lados. As principais etapas são as seguintes.

  1. Obter um objeto PrintCapabilities com o método GetPrintCapabilities.

  2. Testar a presença do recurso desejado. No exemplo abaixo, nós testamos a propriedade DuplexingCapability do objeto PrintCapabilities em busca da capacidade de impressão nos dois lados de uma folha de papel com a opção de "virar página" sendo ao longo do lado comprido da folha. Como DuplexingCapability é uma coleção, usamos o método Contains de ReadOnlyCollection<T>.

    ObservaçãoObservação:

    Essa etapa não é estritamente necessária. O MergeAndValidatePrintTicket método usado abaixo verificará cada solicitação PrintTicket em relação a recursos da impressora. Se não houver suporte da impressora ao recurso solicitado, o driver de impressora substituirá uma solicitação alternativa no PrintTicket retornado pelo método.

  3. Se a impressora oferecer suporte a duplexing, o código de exemplo cria um PrintTicket que pede duplexing. Mas o aplicativo não especifica todas as possível configurações de impressora disponíveis no elemento PrintTicket. Isso seria um gasto desnecessário de tempo do programador e do programa. Em vez disso, o código define apenas a solicitação de duplexing e, em seguida, mescla este PrintTicket com um PrintTicket existente, totalmente configurado e validados, nesse caso o PrintTicket padrão do usuário.

  4. Da mesma forma, o exemplo chama o método MergeAndValidatePrintTicket para mesclar o novo PrintTicket mínimo com o PrintTicket padrão do usuário. Este retorna um ValidationResult que inclui o novo PrintTicket como uma de suas propriedades.

  5. O exemplo, em seguida, testa se o novo PrintTicket solicita duplexing. Em caso afirmativo, o exemplo torna-o o novo ticket de impressão padrão para o usuário. Se a etapa 2 acima tivesse sido deixada de fora e a impressora não oferecesse suporte a duplexing ao longo do lado comprido, o teste teria resultado em false. (Consulte a observação acima).

  6. A última etapa significativa é confirmar a alteração da propriedade UserPrintTicket da PrintQueue com o método 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);
    }
}

Para que você possa rapidamente testar esse exemplo, o restante dele é apresentado abaixo. Crie um projeto e um espaço de nomes e cole os dois trechos de código contidos neste artigo dentro do bloco do espaço de nomes.

/// <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

Consulte também

Conceitos

Documentos em Windows Presentation Foundation

Visão Geral de Impressão

Referência

PrintCapabilities

PrintTicket

GetPrintQueues

PrintServer

EnumeratedPrintQueueTypes

PrintQueue

GetPrintCapabilities

Outros recursos

Exemplos de impressão