Windows 8 printing Postscript file programmatically

拟墨画扇 提交于 2020-01-23 09:11:07

问题


I've spotted a strange problem while printing a Postscript file.

So here is my setup:

I have a Windows 8 PC, on this PC there is an C# application "NetworkPrintTest.exe", which, when executed, should open a PDF, generate a Postscript file and ultimately should print it. But it doesn't do anything. I don't get an error but it won't print either. The same program runs error free on windows 7 and i even get the printer to print the file.

As mentioned above the .ps file is generated successfully on both operating systems but the printing failes.

Here is my source code which should print the file.

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;
        }

I used some DLL imports

    [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);

I've found that the GDI32.dll differs in version but i don't see any problems so far.

Windows 7 -> 6.1.7601.18275

Windows 8 -> 6.2.9200.16654

My Application is written in C# in .Net Framework 2.0


回答1:


With Windows Vista onwards you need to use the data type "XPS_PASS" instead of "RAW" for printers with XPS based drivers.



来源:https://stackoverflow.com/questions/19975988/windows-8-printing-postscript-file-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!