Share via


Como imprimir arquivos XPS de forma programática

Você pode usar uma sobrecarga do método para imprimir arquivos XPS (XML Paper Specification) sem abrir uma PrintDialog ou, em princípio, qualquer interface do AddJob usuário (UI).

Você também pode imprimir arquivos XPS usando os muitos XpsDocumentWriter.Write e XpsDocumentWriter.WriteAsync métodos. Para obter mais informações, consulte Imprimindo um documento XPS.

Outra maneira de imprimir XPS é usar os PrintDialog.PrintDocument métodos ou PrintDialog.PrintVisual . Consulte Invocar uma caixa de diálogo Imprimir.

Exemplo

As principais etapas para usar o método de três parâmetros AddJob(String, String, Boolean) são as seguintes. O exemplo abaixo fornece detalhes.

  1. Determine se a impressora é uma impressora XPSDrv. Consulte Visão geral de impressão para obter mais informações sobre XPSDrv.

  2. Se a impressora não for uma impressora XPSDrv, defina o apartment do thread como um thread único.

  3. Crie uma instância de um servidor de impressão e um objeto de fila de impressão.

  4. Chame o método, especificando um nome de trabalho, o arquivo a ser impresso e um Boolean sinalizador indicando se a impressora é ou não uma impressora XPSDrv.

O exemplo abaixo mostra como imprimir em lote todos os arquivos XPS em um diretório. Embora o aplicativo solicite que o usuário especifique o diretório, o método de três parâmetros AddJob(String, String, Boolean) não requer uma interface do usuário (UI). Ele pode ser usado em qualquer caminho de código onde você tem um nome de arquivo XPS e caminho que você pode passar para ele.

A sobrecarga de três parâmetros AddJob(String, String, Boolean) de deve ser executada em um único apartamento de AddJob thread sempre que o Boolean parâmetro for false, que deve ser quando uma impressora não-XPSDrv estiver sendo usada. No entanto, o estado de apartamento padrão para .NET é vários threads. Esse padrão deve ser revertido, uma vez que o exemplo supõe uma impressora não XPSDrv.

Há duas maneiras de alterar o padrão. Uma maneira é simplesmente adicionar o STAThreadAttribute (isto é, "") logo acima da primeira linha do método do Main aplicativo (geralmente "[System.STAThreadAttribute()]static void Main(string[] args)"). No entanto, muitos aplicativos exigem que o Main método tenha um estado de apartamento multi-threaded, portanto, há um segundo método: colocar a chamada para AddJob(String, String, Boolean) em um thread separado cujo estado de apartamento é definido STA como com SetApartmentState. O exemplo a seguir usa essa segunda técnica.

Assim, o exemplo começa instanciando um objeto e passando-lhe um Threadmétodo PrintXPS como parâmetro ThreadStart . (O O método PrintXPS é definido posteriormente no exemplo.) Em seguida, o thread é definido como um apartamento de thread único. O único código restante do método Main inicia o novo thread.

A parte principal do exemplo está no exemplo staticBatchXPSPrinter.PrintXPS. Depois de criar um servidor de impressão e uma fila, o método solicita ao usuário um diretório contendo arquivos XPS. Após validar a existência do diretório e a presença de arquivos *. xps nele, o método adiciona cada arquivo à fila de impressão. O exemplo assume que a impressora não é XPSDrv, então estamos passando false para o último parâmetro do AddJob(String, String, Boolean) método. Por esse motivo, o método validará a marcação XPS no arquivo antes de tentar convertê-lo para a linguagem de descrição de página da impressora. Se a validação falhar, uma exceção será lançada. O código de exemplo capturará a exceção, notificará o usuário sobre ela e, em seguida, processará o próximo arquivo XPS.

class Program
{
    [System.MTAThreadAttribute()] // Added for clarity, but this line is redundant because MTA is the default.
    static void Main(string[] args)
    {
        // Create the secondary thread and pass the printing method for
        // the constructor's ThreadStart delegate parameter. The BatchXPSPrinter
        // class is defined below.
        Thread printingThread = new Thread(BatchXPSPrinter.PrintXPS);

        // Set the thread that will use PrintQueue.AddJob to single threading.
        printingThread.SetApartmentState(ApartmentState.STA);

        // Start the printing thread. The method passed to the Thread
        // constructor will execute.
        printingThread.Start();
    }//end Main
}//end Program class

public class BatchXPSPrinter
{
    public static void PrintXPS()
    {
        // Create print server and print queue.
        LocalPrintServer localPrintServer = new LocalPrintServer();
        PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

        // Prompt user to identify the directory, and then create the directory object.
        Console.Write("Enter the directory containing the XPS files: ");
        String directoryPath = Console.ReadLine();
        DirectoryInfo dir = new DirectoryInfo(directoryPath);

        // If the user mistyped, end the thread and return to the Main thread.
        if (!dir.Exists)
        {
            Console.WriteLine("There is no such directory.");
        }
        else
        {
            // If there are no XPS files in the directory, end the thread
            // and return to the Main thread.
            if (dir.GetFiles("*.xps").Length == 0)
            {
                Console.WriteLine("There are no XPS files in the directory.");
            }
            else
            {
                Console.WriteLine("\nJobs will now be added to the print queue.");
                Console.WriteLine("If the queue is not paused and the printer is working, jobs will begin printing.");

                // Batch process all XPS files in the directory.
                foreach (FileInfo f in dir.GetFiles("*.xps"))
                {
                    String nextFile = directoryPath + "\\" + f.Name;
                    Console.WriteLine("Adding {0} to queue.", nextFile);

                    try
                    {
                        // Print the Xps file while providing XPS validation and progress notifications.
                        PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile, false);
                    }
                    catch (PrintJobException e)
                    {
                        Console.WriteLine("\n\t{0} could not be added to the print queue.", f.Name);
                        if (e.InnerException.Message == "File contains corrupted data.")
                        {
                            Console.WriteLine("\tIt is not a valid XPS file. Use the isXPS Conformance Tool to debug it.");
                        }
                        Console.WriteLine("\tContinuing with next XPS file.\n");
                    }
                }// end for each XPS file
            }//end if there are no XPS files in the directory
        }//end if the directory does not exist

        Console.WriteLine("Press Enter to end program.");
        Console.ReadLine();
    }// end PrintXPS method
}// end BatchXPSPrinter class
Friend Class Program
    <System.MTAThreadAttribute()>
    Shared Sub Main(ByVal args() As String) ' Added for clarity, but this line is redundant because MTA is the default.
        ' Create the secondary thread and pass the printing method for 
        ' the constructor's ThreadStart delegate parameter. The BatchXPSPrinter
        ' class is defined below.
        Dim printingThread As New Thread(AddressOf BatchXPSPrinter.PrintXPS)

        ' Set the thread that will use PrintQueue.AddJob to single threading.
        printingThread.SetApartmentState(ApartmentState.STA)

        ' Start the printing thread. The method passed to the Thread 
        ' constructor will execute.
        printingThread.Start()

    End Sub

End Class

Public Class BatchXPSPrinter
    Public Shared Sub PrintXPS()
        ' Create print server and print queue.
        Dim localPrintServer As New LocalPrintServer()
        Dim defaultPrintQueue As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

        ' Prompt user to identify the directory, and then create the directory object.
        Console.Write("Enter the directory containing the XPS files: ")
        Dim directoryPath As String = Console.ReadLine()
        Dim dir As New DirectoryInfo(directoryPath)

        ' If the user mistyped, end the thread and return to the Main thread.
        If Not dir.Exists Then
            Console.WriteLine("There is no such directory.")
        Else
            ' If there are no XPS files in the directory, end the thread 
            ' and return to the Main thread.
            If dir.GetFiles("*.xps").Length = 0 Then
                Console.WriteLine("There are no XPS files in the directory.")
            Else
                Console.WriteLine(vbLf & "Jobs will now be added to the print queue.")
                Console.WriteLine("If the queue is not paused and the printer is working, jobs will begin printing.")

                ' Batch process all XPS files in the directory.
                For Each f As FileInfo In dir.GetFiles("*.xps")
                    Dim nextFile As String = directoryPath & "\" & f.Name
                    Console.WriteLine("Adding {0} to queue.", nextFile)

                    Try
                        ' Print the Xps file while providing XPS validation and progress notifications.
                        Dim xpsPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob(f.Name, nextFile, False)
                    Catch e As PrintJobException
                        Console.WriteLine(vbLf & vbTab & "{0} could not be added to the print queue.", f.Name)
                        If e.InnerException.Message = "File contains corrupted data." Then
                            Console.WriteLine(vbTab & "It is not a valid XPS file. Use the isXPS Conformance Tool to debug it.")
                        End If
                        Console.WriteLine(vbTab & "Continuing with next XPS file." & vbLf)
                    End Try

                Next f ' end for each XPS file

            End If 'end if there are no XPS files in the directory

        End If 'end if the directory does not exist

        Console.WriteLine("Press Enter to end program.")
        Console.ReadLine()

    End Sub

End Class

Se estiver usando uma impressora XPSDrv, você poderá definir o parâmetro final como true. Nesse caso, como XPS é a linguagem de descrição de página da impressora, o método enviará o arquivo para a impressora sem validá-lo ou convertê-lo em outro idioma de descrição de página. Se você não tiver certeza em tempo de design se o aplicativo usará uma impressora XPSDrv, você pode modificar o aplicativo para que ele leia a propriedade e a IsXpsDevice ramificação de acordo com o que encontrar.

Como inicialmente haverá algumas impressoras XPSDrv disponíveis imediatamente após o lançamento do Windows Vista e do Microsoft .NET Framework, talvez seja necessário disfarçar uma impressora não-XPSDrv como uma impressora XPSDrv. Para fazer isso, adicione Pipelineconfig.xml à lista de arquivos na seguinte chave do Registro do computador que executa o aplicativo:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\PseudoXPSPrinter>\<DependentFiles

onde <PseudoXPSPrinter> é qualquer fila de impressão. O computador, então, deve ser reinicializado.

Esse disfarce permitirá que você passe true como o parâmetro final de AddJob(String, String, Boolean) sem causar uma exceção, mas como <o PseudoXPSPrinter> não é realmente uma impressora XPSDrv, apenas o lixo será impresso.

Observação

Para simplificar, o exemplo acima usa a presença de uma extensão *.xps como seu teste de que um arquivo é XPS. No entanto, os arquivos XPS não precisam ter essa extensão. O isXPS.exe (isXPS Conformance Tool) é uma maneira de testar a validade de um arquivo XPS.

Confira também