原始打印设置DevMode选项(方向,副本,边距,默认来源等)

希望这里的某个人能够帮我解决问题,因为我正试图让这个工作成功。 基本上我需要打印使用winspool.Drv win32 API发送原始文本/ PCL命令到打印机。 这工作正常,但是,您在打印机对话框/首选项中select的打印机选项都不能执行任何操作。

最终我想从对话框PrinterSettings中获取DEVMODE,并将其用于原始打印,但是我甚至无法手动设置它。

我试图在WritePrinter之前更改DEVMODE,或者在单独的打开/closures打印机之前尝试更改DEVMODE,我试图只使用DocumentProperties调用,并使用GetPrinter / SetPrinter与PRINTER_INFO_8结构,没有任何粘附。 DEVMODE结构似乎填写正确,我改变它,似乎改变正确,但打印机总是打印相同的方式无论如何。

这里是我使用的代码:

方法一:

public static bool SetPrinterSettings(PrinterSettings settings, IntPtr hPrinter) { IntPtr hDevMode; // a handle to our current DEVMODE IntPtr pDevMode; // a pointer to our current DEVMODE String sPrinterName; // normalized printer name DEVMODE dm; // Setup sPrinterName = settings.PrinterName.Normalize(); // Obtain the current DEVMODE from the PrinterSettings hDevMode = settings.GetHdevmode(settings.DefaultPageSettings); // Obtain a lock on the handle and get an actual pointer so Windows won't move // it around while we're futzing with it pDevMode = GlobalLock(hDevMode); // test dm = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE)); // Our DEVMODE is 188 bytes, but dmSize returns 220, does this matter? // This code doesnt seem to work //dm.dmSize = (short)Marshal.SizeOf(dm); // Set size to our implementation of DevMode //int isize = GlobalSize(pDevMode).ToInt32() - (int)dm.dmSize; // Set the print drivers extra size //dm.dmDriverExtra = Convert.ToInt16(isize); // Change things dm.dmFields = DM_FIELD_TYPE.DM_ORIENTATION; dm.dmOrientation = 2; // Load the structure back into the buffer Marshal.StructureToPtr(dm, pDevMode, true); //Tell the printer about the new property int ret = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, pDevMode, pDevMode, (DM_IN_BUFFER | DM_OUT_BUFFER)); if (ret <= 0) { return false; } // We're done futzing GlobalUnlock(hDevMode); // Tell our printer settings to use the one we just overwrote //settings.SetHdevmode(hDevMode); // It's copied to our printer settings, so we can free the OS-level one GlobalFree(hDevMode); //test dm = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE)); // ^^ this contains correct orientation return true; } 

方法2:

 public static bool SetLandscapeMode(PrinterSettings settings, IntPtr hPrinter) { // Setup String sPrinterName = settings.PrinterName.Normalize(); // get current printer settings int memNeeded = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, IntPtr.Zero, IntPtr.Zero, 0); IntPtr pFullDevMode = Marshal.AllocHGlobal(memNeeded); DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, pFullDevMode, IntPtr.Zero, DM_OUT_BUFFER); DEVMODE dm = (DEVMODE)Marshal.PtrToStructure(pFullDevMode, typeof(DEVMODE)); // change the settings dm.dmFields = DM_FIELD_TYPE.DM_ORIENTATION; dm.dmOrientation = 2; Marshal.StructureToPtr(dm, pFullDevMode, true); PRINTER_INFO_8 PI8 = new PRINTER_INFO_8(); PI8.pDevMode = pFullDevMode; IntPtr pPI8 = Marshal.AllocHGlobal(Marshal.SizeOf(PI8)); Marshal.StructureToPtr(PI8, pPI8, true); // save the printer settings SetPrinter(hPrinter, 9, pPI8, 0); Marshal.FreeHGlobal(pPI8); return true; } 

testing打印的代码:

 private void testPrint_Click(object sender, EventArgs e) { IntPtr hPrinter; if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { hPrinter = RawPrint.OpenRAWPrinter(printDialog1.PrinterSettings.PrinterName); if (hPrinter == IntPtr.Zero) { MessageBox.Show("Error opening printer"); return; } if (!RawPrint.StartRAWDocument(hPrinter, "Test document")) { MessageBox.Show("Error starting raw document"); RawPrint.CloseRAWPrinter(hPrinter); return; } // Start a page. if (RawPrint.StartRawPage(hPrinter)) { // Set the printers settings //if (!RawPrint.SetPrinterSettings(printDialog1.PrinterSettings, hPrinter)) if(!RawPrint.SetLandscapeMode(printDialog1.PrinterSettings, hPrinter)) { MessageBox.Show("Error setting printer settings"); RawPrint.CloseRAWPrinter(hPrinter); return; } String test = "This is \r\n test print data"; byte[] buf = Encoding.Default.GetBytes(test); GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned); RawPrint.writeRAW(hPrinter, gch.AddrOfPinnedObject(), buf.Length); gch.Free(); RawPrint.EndRawPage(hPrinter); } RawPrint.EndRAWDocument(hPrinter); RawPrint.CloseRAWPrinter(hPrinter); } else { MessageBox.Show("Error starting raw page"); } } 

唯一奇怪的是Marshal.SizeOf(typeof(DEVMODE))或Marshal.SizeOf(dm)等于188,而返回到结构中的dmSize是220.我试着修改这个dmSize来匹配我们的结构,如注释出了代码,但似乎错误了或不工作,不知道是否重要。

除此之外,DevModes中的返回信息似乎都是正确的,并且正在工作。 我可以在打印对话框中设置选项,并在PrinterSettings devmode中查看它们。 在这些函数结束时,将pDevMode编组回到一个结构体具有改变的设置。

然而,这似乎没有任何影响,打印什么如此。 有什么我失踪了吗?

编辑:在testing函数的RawPrint调用只是围绕后台打印程序API调用的包装。 页面打印正常,它只是总是打印在肖像模式,没有任何选项的工作。

Solutions Collecting From Web of "原始打印设置DevMode选项(方向,副本,边距,默认来源等)"

通过使用WritePrinter而不是绕过打印机驱动程序,您应该正常开始打印作业,并使用ExtEscape()和PASSTHROUGH转义注入PCL。 这将允许所有PrinterSettings值应用于作业。 并非所有的打印机驱动程序都支持PASSTHROUGH,但您可以通过使用QUERYESCSUPPORT调用ExtEscape()来检查驱动程序支持。