系统相关
首页 > 系统相关> > Windows 8以编程方式打印Postscript文件

Windows 8以编程方式打印Postscript文件

作者:互联网

我在打印Postscript文件时发现了一个奇怪的问题.

所以这是我的设置:

我有一台Windows 8 PC,在此PC上有一个C#应用程序“ NetworkPrintTest.exe”,该应用程序在执行时应打开PDF,生成Postscript文件并最终进行打印.
但是它什么也没做.我没有收到错误,但也不会打印.
同一程序在Windows 7上运行无错误,我什至让打印机打印文件.

如上所述,.ps文件已在两个操作系统上成功生成,但打印失败.

这是我应该打印文件的源代码.

public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount, params string[] docName)
        {
            int dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);
            DOCINFOA di = new DOCINFOA();
            bool flag = false;
            di.pDocName = "print document";
            if (docName.Length > 0)
                di.pDocName = docName[0];
            di.pDataType = "RAW";
            if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                if (StartDocPrinter(hPrinter, 1, di))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        flag = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            if (!flag)
            {
                Marshal.GetLastWin32Error();
            }
            return flag;
        }

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

我使用了一些DLL导入

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

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

[DllImport("gdi32.dll")]
    private static extern int GetDeviceCaps(IntPtr hdc, int capindex);

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

我发现GDI32.dll的版本有所不同,但到目前为止我还没有发现任何问题.

Windows 7-> 6.1.7601.18275

Windows 8-> 6.2.9200.16654

我的应用程序是用.Net Framework 2.0中的C#编写的

解决方法:

从Windows Vista开始,对于基于XPS驱动程序的打印机,您需要使用数据类型“ XPS_PASS”而不是“ RAW”.

标签:postscript,printing,windows-8,dllimport,c
来源: https://codeday.me/bug/20191030/1966086.html