【问题标题】:How to reprint a spool file using XPS printer?如何使用 XPS 打印机重新打印假脱机文件?
【发布时间】:2013-01-27 18:26:59
【问题描述】:

我正在构建一个程序来捕获打印的文档,然后将这些文档转换为 xps\image。

目前我正在使用FileSystemWatcher 监视目录“C:\Windows\System32\spool\PRINTERS\”并复制 spl 文件,确保其不重复然后尝试将其转换为xps 文件,通过使用Win32 Spooler APIMicrosoft XPS Document Writer 预定义的打印机打印 spl 文件,但是当我指定如下所示的输出文件时,如果我离开它,则会返回错误代码 1804清空它成功,但我没有得到输出文件。

    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount, string outputFile, string dataType, out int errorCode)
    {
        IntPtr hPrinter;
        var di = new DOCINFOA();
        var bSuccess = false; // Assume failure unless you specifically succeed.

        di.pDocName = "Spool Doc";
        di.pDataType = dataType;
        di.pOutputFile = outputFile;

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    var dwWritten = 0;
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        errorCode = bSuccess == false ? Marshal.GetLastWin32Error() : 0;
        return bSuccess;
    }

那么,我做错了什么,以及如何将假脱机文件打印\转换为 XPS 文档,如果可能的话,还可以转换为图像文件和文本文件。

编辑:添加附加信息

这里是 DOCINFOA 的实现

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;
    }

这是完整的调用链:

应用程序运行以下代码

    var dataTypes = new[] { null, "RAW", "RAW [FF appended]", "RAW [FF auto]", "NT EMF 1.003", "NT EMF 1.006", "NT EMF 1.007", "NT EMF 1.008", "TEXT", "XPS_PASS", "XPS2GDI" };
    foreach (var dataType in dataTypes)
    {
        int errorCode;
        RawPrinterHelper.SendFileToPrinter(@"Microsoft XPS Document Writer", sourceFile, outputFile, dataType, out errorCode);
        //print errorCode
    }

而在 RawPrinterHelper

    public static bool SendFileToPrinter(string szPrinterName, string szFileName, string outputPath, string dataType, out int errorCode)
    {
        // Open the file.
        var fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        var br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        // Your unmanaged pointer.
        var documentPath = Path.GetFileName(szFileName);
        var nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        var bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        var pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        var bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength, outputPath, dataType, out errorCode);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        fs.Close();
        return bSuccess;
    }

还有API函数

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

【问题讨论】:

  • 您将 pDataType 设置为什么?这很可能是错误的原因。尝试将其设置为空。如果这不起作用,请尝试“RAW”。
  • @CareyGregory 我试过这些类型 null,“RAW”,“RAW [FF appended]”,“RAW [FF auto]”,“NT EMF 1.003”,“NT EMF 1.006”,“NT循环中的 EMF 1.007"、"NT EMF 1.008"、"TEXT"、"XPS_PASS"、"XPS2GDI" 都给出了相同的错误
  • 请将您的 DOCINFOA 声明添加到您的帖子中。
  • @CareyGregory 已添加,感谢您抽出宝贵时间提供帮助。

标签: c# printing print-spooler-api


【解决方案1】:

问题在于您对 DOC_INFO_1 结构的声明。它与the layout Windows expects 不匹配。 Windows 声明如下所示:

typedef struct _DOC_INFO_1 {
  LPTSTR pDocName;
  LPTSTR pOutputFile;
  LPTSTR pDatatype;
} DOC_INFO_1;

但是你的声明是这样的:

public class DOCINFOA
{
    public string pDataType;
    public string pDocName;
    public string pOutputFile;
}

重新排序声明中的元素以匹配 Windows 结构,它应该可以解决您的问题。

编辑:坏消息。你的方法行不通。查看WritePrinter 的文档:

WritePrinter 仅支持 GDI 打印,不得用于 XPS 印刷。如果您的打印作业使用 XPS 或 OpenXPS 打印路径, then use the XPS Print API。将 XPS 或 OpenXPS 打印作业发送到 不支持使用 WritePrinter 的假脱机程序,可能会导致 未确定的结果。

【讨论】:

  • 非常感谢,这解决了错误代码1804的问题,但是输出路径下保存的文件是另一个spl文件(主要是副本)而不是XPS文件,并且仍然弹出一个窗口最多保存文件,虽然它以一种奇怪的方式出现,并显示一条消息“程序无法在您的桌面上显示消息”,所以 pOutputFile 可以用于指定 xps 目标位置,如果没有,是否还有另一个怎么办?
  • @MEYWD 查看我添加到答案中的信息。恐怕您将不得不重新设计它。
猜你喜欢
  • 2012-11-01
  • 1970-01-01
  • 2020-11-10
  • 2013-09-19
  • 2011-09-21
  • 1970-01-01
  • 2021-04-22
  • 2019-09-22
相关资源
最近更新 更多