将文件发送到XPS打印机

XPS打印机允许我们创建xps文件,无论是来自图像还是txt或doc文件。

我想在c#中以编程方式执行相同的操作。

如何将文件发送到xps打印机并让打印机将其转换为.xps文件?

有任何想法吗?

我谷歌这个但到目前为止还没有找到太多。

也可以使用打印队列打印到XPS文档编写器,但它始终显示文件对话框。

请参阅下面的其他替代方法,以转换和打印到XPS文件。

以编程方式将文件转换为XPS

这并不像你希望那里的许多用户尝试过那么简单,并且有许多不同的方法可以实现所有这些并不是最好的。

将文档转换为XPS的一种方法(最简单的方法)是使用WORD 2007+ API来完成它。 请参阅下面的MSDN论坛问题

若要使用Word 2007以编程方式将docx转换为xps,请参阅Word对象模型参考中的Document.ExportAsFixedFormat ( http://msdn2.microsoft.com/en-us/library/Bb256835.aspx )。 但是,由于这是服务器端方案,因此您应该注意KB 257757服务器端自动化Office的注意事项(请参阅http://support.microsoft.com/kb/257757 )。

将图像打印到XPS

您可以使用下面的代码轻松地将图像打印到XPS文件。 下面的代码是WPF示例,您传递给write方法的图像需要包装在canvas中,请参阅此post的示例: http : //denisvuyka.wordpress.com/2007/12/03/wpf-diagramming-saving-您帆布到图像的XPS文档或-原始XAML /

XpsDocument xpsd = new XpsDocument("C:\\YourXPSFileName.xps", FileAccess.ReadWrite); System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd); xw.Write(YourImageYouWishToWrite); 

请参阅下面的扩展示例:

 public void Export(Uri path, Canvas surface) { if (path == null) return; // Save current canvas transorm Transform transform = surface.LayoutTransform; // Temporarily reset the layout transform before saving surface.LayoutTransform = null; // Get the size of the canvas Size size = new Size(surface.Width, surface.Height); // Measure and arrange elements surface.Measure(size); surface.Arrange(new Rect(size)); // Open new package Package package = Package.Open(path.LocalPath, FileMode.Create); // Create new xps document based on the package opened XpsDocument doc = new XpsDocument(package); // Create an instance of XpsDocumentWriter for the document XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc); // Write the canvas (as Visual) to the document writer.Write(surface); // Close document doc.Close(); // Close package package.Close(); // Restore previously saved layout surface.LayoutTransform = transform; } 

有第三方工具允许您将PDF和其他文件格式打印到XPS。

  • PDF到XPS

打印XPS文件

可以以编程方式打印XPS文档至少对于此解决方案,您将需要.Net 4。

下面的示例使用WPF中的“打印”对话框以及System.Windows.Xps和System.Printing中的某些类。

下面的代码将Xps文件打印到系统上的默认打印机,但是如果要打印到其他打印机甚至打印到打印服务器,则需要在打印对话框中更改PrintQueue对象。

使用System.Printing命名空间非常简单。

请参阅下面的示例。

请记住,因为它正在使用WPF对话框,所以它需要在STATThread模型上运行。

 using System; using System.Collections.Generic; using System.Linq; using System.Printing; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Xps.Packaging; namespace ConsoleApplication { class Program { [STAThread] static void Main(string[] args) { PrintDialog dlg = new PrintDialog(); XpsDocument xpsDoc = new XpsDocument(@"C:\DATA\personal\go\test.xps", System.IO.FileAccess.Read); dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Document title"); } } } 

请参阅以下一些有用的链接

  1. Xps文档文档
  2. 从WPF文档打印对话框
  3. System.Printing命名空间文档

希望这有助于满足您的需求

 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