【问题标题】:Send file to XPS Printer将文件发送到 XPS 打印机
【发布时间】:2014-04-23 20:57:10
【问题描述】:

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

我想在 c# 中以编程方式做同样的事情。

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

有什么想法吗?

我用谷歌搜索了这个,但到目前为止还没有找到太多。

【问题讨论】:

  • 如果您的文档已准备好打印,则将 xps 打印机设置为默认打印机并保存。
  • 我们不需要编写任何代码来将我们的文档保存为 xps 格式。我们只需要使我们的文档Ready 可以打印,并在控制面板中将xps 打印机设置为默认打印机。然后,当您按下打印按钮时,xps 打印机会自动为我们提供保存选项。
  • 这个问题现在回答了吗?
  • 是的,我会给你积分,别担心

标签: c# printing


【解决方案1】:

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

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

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

这并不像您希望许多用户尝试过的那么简单,并且有许多不同的方法可以完成这一切,但都不是最好的。

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

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

将图像打印到 XPS

您可以使用以下代码轻松地将图像打印到 XPS 文件。 下面的代码是 WPF 示例,您传递给 write 方法的图像需要包装在画布中,请参阅此帖子以获取示例:http://denisvuyka.wordpress.com/2007/12/03/wpf-diagramming-saving-you-canvas-to-image-xps-document-or-raw-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。

打印 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 Document documentation
  2. Print Dialog from WPF documentation
  3. System.Printing namespace documentation

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

【讨论】:

  • 对不起,我说的是拥有任何文件(docx、jpeg、txt)并将其发送到 Xps 打印机(转换为 xps)。有一个名为 Xps Document Writer 的 PrinterQueue。每台计算机都应该有它。我不是在谈论 priting 已经存在的 Xps。有什么想法吗?
  • 是的,您可以编写自定义 xps 文档并使用我只从磁盘加载一个的方法打印它,因为它更容易。但是以编程方式创建 xps 文档很容易,堆栈中应该有其他问题可以回答
  • 你能给我发一个这样的链接吗?一个docx在xps文档中获取转换器?我找不到它
  • 您可以对 xps 打印机进行静默打印。当我回到我的办公桌上时,我会更新我的答案
  • 是的,请稍等
【解决方案2】:
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

【讨论】:

  • 我要求在 c# 中而不是手动执行此操作。无论如何感谢您的帮助:)
  • 这是微软官方的 C# 程序化打印方法。
  • 请停止处理来自微软的代码。我已经说过代码对我不起作用。再次阅读我的问题。
  • 您不能让打印机自动转换为.XPS。您必须手动更改它。
  • 对节目感到抱歉。没听懂问题。
猜你喜欢
  • 2012-11-01
  • 1970-01-01
  • 2017-12-31
  • 2018-01-01
  • 2013-07-01
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多