2010-01-01 4 views
0

J'utilise Visual Studio 2010 pour créer un modèle Word. J'ai créé un ruban avec des boutons: imprimer en couleur, imprimer en B & W. J'utilise la fonction Document.printout() pour imprimer le document.Impression en niveaux de gris dans Word 2007 à partir de C#

Comment régler l'imprimante sur l'impression en niveaux de gris à partir du code?
Je ne souhaite pas utiliser printDialog.

J'ai essayé d'utiliser:

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

Mais cela ne fonctionne pas en combinaison avec Word

Répondre

0

J'ai trouvé une solution avec le DEVMODE et quelques pInvokes; Devmode: (http://msdn.microsoft.com/en-us/library/aa927408.aspx) Cette structure contient des informations sur l'environnement de l'imprimante et l'initialisation du périphérique.

Il contient un champ: dmColor (court) en réglant ceci sur 1 signifie échelle de gris/monochrome, ce réglage sur 2 signifie couleur. La modification de ces paramètres affecte directement l'imprimante et remplace les paramètres utilisateur.

[DllImport ("Winspool.drv", charset = CharSet.Ansi, SetLastError = true)]
extern private static bool SetPrinter (IntPtr hPrinter, int Niveau, IntPtr pPrinter, commande int);

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 peut être récupéré via:
System.Drawing.Printing.PrinterSettings.InstalledPrinters

Questions connexes