Postupy: Tisk souborů XPS z programu
Můžete použít jedno přetížení AddJob metody k tisku souborů XPS (XML Paper Specification) bez otevření PrintDialog nebo v zásadě jakékoli uživatelské rozhraní (UI).
Soubory XPS můžete vytisknout také pomocí mnoha XpsDocumentWriter.Write a XpsDocumentWriter.WriteAsync metod. Další informace naleznete v tématu Tisk dokumentu XPS.
Dalším způsobem tisku XPS je použití PrintDialog.PrintDocument metod nebo PrintDialog.PrintVisual metod. Viz Vyvolání dialogového okna Tisk.
Příklad
Hlavní kroky použití metody se třemi parametry AddJob(String, String, Boolean) jsou následující. Následující příklad obsahuje podrobnosti.
Určete, zda je tiskárna XPSDrv. Další informace o xpSDrv najdete v tématu Přehled tisku.
Pokud tiskárna není tiskárnou XPSDrv, nastavte byt vlákna na jedno vlákno.
Vytvoření instance tiskového serveru a tiskového objektu fronty
Zavolejte metodu, zadejte název úlohy, soubor, který se má vytisknout, a Boolean příznak označující, zda je tiskárna XPSDrv nebo ne.
Následující příklad ukazuje, jak dávkově vytisknout všechny soubory XPS v adresáři. I když aplikace vyzve uživatele k zadání adresáře, metoda se třemi parametry AddJob(String, String, Boolean) nevyžaduje uživatelské rozhraní (UI). Dá se použít v libovolné cestě kódu, kde máte název souboru XPS a cestu, kterou do něj můžete předat.
Tříparametrové AddJob(String, String, Boolean) přetížení AddJob musí běžet v jednom vláknovém apartmánu vždy, když Boolean je false
parametr , který musí být při použití tiskárny mimo XPSDrv. Výchozí stav bytu pro .NET je však více vláken. Toto výchozí nastavení musí být obrácené, protože v příkladu se předpokládá tiskárna, která není xpSDrv.
Výchozí nastavení můžete změnit dvěma způsoby. Jedním zezpůsobůch STAThreadAttribute[System.STAThreadAttribute()]
Main
static void Main(string[] args)
Mnoho aplikací však vyžaduje, aby Main
metoda měla vícevláknový stav bytu, takže existuje druhá metoda: vložte volání do AddJob(String, String, Boolean) samostatného vlákna, jehož stav bytu je nastaven na STASetApartmentState. Následující příklad používá tuto druhou techniku.
Proto příklad začíná vytvořením instance objektu Thread a jeho předáním PrintXPS metoda jako ThreadStart parametr. (Metoda PrintXPS je definována dále v příkladu.) Dále je vlákno nastaveno na jeden vlákno byt. Jediný zbývající kód Main
metody spustí nové vlákno.
Maso příkladu je v static
BatchXPSPrinter.PrintXPS metoda. Po vytvoření tiskového serveru a fronty metoda vyzve uživatele k zadání adresáře obsahujícího soubory XPS. Po ověření existence adresáře a přítomnosti souborů *.xps v něm metoda přidá každý takový soubor do tiskové fronty. Příklad předpokládá, že tiskárna není XPSDrv, takže předáváme false
poslední parametr AddJob(String, String, Boolean) metody. Z tohoto důvodu metoda ověří kód XPS v souboru předtím, než se pokusí převést na jazyk popisu stránky tiskárny. Pokud se ověření nezdaří, vyvolá se výjimka. Ukázkový kód zachytí výjimku, upozorní uživatele na ni a pak přejde ke zpracování dalšího souboru 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
Pokud používáte tiskárnu XPSDrv, můžete nastavit konečný parametr na true
hodnotu . V takovém případě, protože XPS je jazyk popisu stránky tiskárny, metoda odešle soubor do tiskárny bez ověření nebo převodu do jiného jazyka popisu stránky. Pokud si nejste jistí, zda aplikace bude používat tiskárnu XPSDrv, můžete aplikaci upravit tak, aby ji četla IsXpsDevice vlastnost a větev podle toho, co najde.
Vzhledem k tomu, že bude zpočátku k dispozici několik tiskáren XPSDrv ihned po vydání systému Windows Vista a Microsoft .NET Framework, možná budete muset převlečení tiskárny mimo XPSDrv jako tiskárnu XPSDrv. Uděláte to tak, že přidáte Pipelineconfig.xml do seznamu souborů v následujícím klíči registru počítače, na kterém běží vaše aplikace:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environment\systém Windows NT x86\Drivers\Version-3\<PseudoXPSPrinter>\DependentFiles
kde <PseudoXPSPrinter> je libovolná tisková fronta. Počítač se pak musí restartovat.
Tento převlek vám umožní předat true
jako konečný parametr AddJob(String, String, Boolean) bez příčiny výjimky, ale protože <PseudoXPSPrinter> není skutečně XPSDrv tiskárna, vytiskne se pouze uvolňování paměti.
Poznámka:
Pro zjednodušení výše uvedený příklad používá jako svůj test přítomnost rozšíření *.xps, že soubor je XPS. Soubory XPS však nemusí mít tuto příponu. isXPS.exe (isXPS Conformance Tool) je jedním ze způsobů, jak otestovat soubor pro platnost XPS.
Viz také
.NET Desktop feedback
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro