从 C# 在 Word 2007 中进行灰度打印

发布于 2024-08-17 02:09:55 字数 311 浏览 3 评论 0原文

我正在使用 Visual Studio 2010 创建 Word 模板。我创建了一个带有按钮的功能区:彩色打印、黑白打印。我使用 Document.printout() 函数来打印文档。

如何通过代码将打印机设置为灰度打印?
我不想使用 printDialog。

我尝试使用这个:

PrinterSettings settings = new PrinterSettings();  
settings.DefaultPageSettings.Color = false;

但这不能与Word结合使用

I'm using Visual Studio 2010 to create a Word Template. I created a Ribbon with to buttons: print in color, print in B&W. I use the Document.printout() function to print the document.

How can I set the printer to Grayscale printing from code?
I don't want to use the printDialog.

I tried to use this:

PrinterSettings settings = new PrinterSettings();  
settings.DefaultPageSettings.Color = false;

But this doesn't work in combination with Word

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2024-08-24 02:09:55

我找到了一个使用 DEVMODE 和一些 pInvokes 的解决方案;

开发模式:(http://msdn.microsoft.com/en-us/library /aa927408.aspx)
该结构包含有关打印机环境和设备初始化的信息。

它包含一个字段:dmColor(短),将其设置为 1 表示灰度/单色,将其设置为 2 表示颜色。更改此设置会直接影响打印机并覆盖用户设置。

[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
私有静态 extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int command);

我使用此示例来创建我的代码

public bool setPrinterToGrayScale(string printerName) 
{
  short monochroom = 1;
  dm = this.GetPrinterSettings(printerName);
  dm.dmColor = monochroom;

  Marshal.StructureToPtr(dm, yDevModeData, true);
  pinfo.pDevMode = yDevModeData;
  pinfo.pSecurityDescriptor = IntPtr.Zero;

  Marshal.StructureToPtr(pinfo, ptrPrinterInfo, true);
  lastError = Marshal.GetLastWin32Error();

  nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, ptrPrinterInfo, 0));
  if (nRet == 0)
  {
    //Unable to set shared printer settings.

    lastError = Marshal.GetLastWin32Error();
    //string myErrMsg = GetErrorMessage(lastError);

    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());

   }
   if (hPrinter != IntPtr.Zero)
      ClosePrinter(hPrinter);
    return Convert.ToBoolean(nRet);
}

PrinterName 可以通过以下方式检索:
System.Drawing.Printing.PrinterSettings.InstalledPrinters

I Found a solution with the DEVMODE and some pInvokes;

Devmode: (http://msdn.microsoft.com/en-us/library/aa927408.aspx)
This structure contains information about a printer environment and device initialization.

It contains a field: dmColor (short) setting this to 1 means grayscale/monoschrome, settings this to 2 means color. Changing this settings effects the printer directly and overrides user settings.

[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int command);

I used this example to create my code

public bool setPrinterToGrayScale(string printerName) 
{
  short monochroom = 1;
  dm = this.GetPrinterSettings(printerName);
  dm.dmColor = monochroom;

  Marshal.StructureToPtr(dm, yDevModeData, true);
  pinfo.pDevMode = yDevModeData;
  pinfo.pSecurityDescriptor = IntPtr.Zero;

  Marshal.StructureToPtr(pinfo, ptrPrinterInfo, true);
  lastError = Marshal.GetLastWin32Error();

  nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, ptrPrinterInfo, 0));
  if (nRet == 0)
  {
    //Unable to set shared printer settings.

    lastError = Marshal.GetLastWin32Error();
    //string myErrMsg = GetErrorMessage(lastError);

    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());

   }
   if (hPrinter != IntPtr.Zero)
      ClosePrinter(hPrinter);
    return Convert.ToBoolean(nRet);
}

PrinterName can be retrieved via:
System.Drawing.Printing.PrinterSettings.InstalledPrinters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文