2010-12-16 21 views
2

編集 私はもはや表示する必要がないコードを再構築しようとしました。私はそれがダイアログを使用して選択できる機能を公開していない、プリンター設定クラスの単なるklimitationだと思う。それは、PrintDocumentにprinterSettingsオブジェクトを設定して割り当て、そのPrintDocumentを印刷できるはずです... ???私はここで思っていないのですか?プリンタ設定を設定するのにC#を使用するにはどうすればよいですか?

EDIT AGAIN 私は、すべてのセッターが 'printerSettings.DefaultPageSettings'に座っていると思います。これにより、プリンタ設定を変更することができます。私はそれを証明していない、まだしかし、=カスタム設定を確認してください(= DL手紙を後で

PrintDocument pd = new PrintDocument(); 
pd.DocumentName = "test.doc"; 

PrinterSettings printerSettings = new PrinterSettings(); 
printerSettings.?? <- I want to set the printer setting here e.g. DL, A4, etc 
pd.PrinterSettings = printerSettings; 
pd.Print(); 

は、私が(小切手、手紙、文書)C#で単語差し込み印刷文書を生成する必要がありますが、これらのすべてが、別のプリンタの設定が必要になりますプリンタ設定ダイアログを読み込むときにこれらの設定を保存してアクセスできますが、プリンタの設定を手動で変更する代わりにコードに組み込みたいと思っています。私は周りを見回して、それはプリンタの設定クラスがそうする必要がありますが、私はそれを動作させるように見えることはできません。私は

//create the mail merge 
IList<Letter> letters = MailMerge.Create(enum.letters) 
Printer.Print(letters) //<-- in here I am trying set the printing preferences to DL Env 


//create the mail merge 
IList<Document> docs = MailMerge.Create(enum.documents) 
Printer.Print(docs) //<-- in here I am trying set the printing preferences to A4 

感謝任意の助けをやろうとしています何の

例の擬似コード。

ありがとう

答えて

3

おそらくWMIを使用できます。私の唯一のWMI経験は、WMIがいくつかのプリンタプロパティを取得するためのC#コードですが、プリンタプロパティを設定しようとはしていませんが、可能であると思います。おそらく、これらのMSDNリンクとコードは、あなたが始めるのを助けることができます。

WMI Tasks: Printers and Printingは、VBスクリプトのコマンドを示しています。 How To: Retrieve Collections of Managed Objectsは、SelectQueryと列挙の使い方を示しています。 How To: Execute a Methodはメソッドの実行方法を示しています:-)。

編集:私はちょうどこのStackOverflow article: How do I programatically change printer settings ...に気付きました。これは、WMIを使用して一部のプリンタ設定を変更しているようです。

マイ取得するコードは次のようになります。

//using System.Management; 

    private void GetPrinterProperties(object sender, EventArgs e) 
    { 
     // SelectQuery from: 
     // http://msdn.microsoft.com/en-us/library/ms257359.aspx 
     // Build a query for enumeration of instances 
     var query = new SelectQuery("Win32_Printer"); 
     // instantiate an object searcher 
     var searcher = new ManagementObjectSearcher(query); 
     // retrieve the collection of objects and loop through it 
     foreach (ManagementObject lPrinterObject in searcher.Get()) 
     { 
      string lProps = GetWmiPrinterProperties(lPrinterObject); 
      // some logging, tracing or breakpoint here... 
     } 
    } 

    // log PrinterProperties for test-purposes 
    private string GetWmiPrinterProperties(ManagementObject printerObject) 
    { 
     // Win32_Printer properties from: 
     // http://msdn.microsoft.com/en-us/library/aa394363%28v=VS.85%29.aspx 
     return String.Join(",", new string[] { 
       GetWmiPropertyString(printerObject, "Caption"), 
       GetWmiPropertyString(printerObject, "Name"), 
       GetWmiPropertyString(printerObject, "DeviceID"), 
       GetWmiPropertyString(printerObject, "PNPDeviceID"), 
       GetWmiPropertyString(printerObject, "DriverName"), 
       GetWmiPropertyString(printerObject, "Portname"), 
       GetWmiPropertyString(printerObject, "CurrentPaperType"), 
       GetWmiPropertyString(printerObject, "PrinterState"), 
       GetWmiPropertyString(printerObject, "PrinterStatus"), 
       GetWmiPropertyString(printerObject, "Location"), 
       GetWmiPropertyString(printerObject, "Description"), 
       GetWmiPropertyString(printerObject, "Comment"), 
      }); 
    } 

    private string GetWmiPropertyString(ManagementObject mgmtObject, string propertyName) 
    { 
     if (mgmtObject[propertyName] == null) 
     { 
      return "<no "+ propertyName + ">"; 
     } 
     else 
     { 
      return mgmtObject[propertyName].ToString(); 
     } 
    } 
} 
0
private void startPrintingButton_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     if (DialogResult.OK == ofd.ShowDialog(this)) 
     { 
      PrintDocument pdoc = new PrintDocument(); 

      pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d"; 
      pdoc.DefaultPageSettings.Landscape = true; 
      pdoc.DefaultPageSettings.PaperSize.Height = 140; 
      pdoc.DefaultPageSettings.PaperSize.Width = 104; 

      Print(pdoc.PrinterSettings.PrinterName, ofd.FileName); 
     } 
    } 

    private void Print(string printerName, string fileName) 
    { 
     try 
     { 
      ProcessStartInfo gsProcessInfo; 
      Process gsProcess; 

      gsProcessInfo = new ProcessStartInfo(); 
      gsProcessInfo.Verb = "PrintTo"; 
      gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      gsProcessInfo.FileName = fileName; 
      gsProcessInfo.Arguments = "\"" + printerName + "\""; 
      gsProcess = Process.Start(gsProcessInfo); 
      if (gsProcess.HasExited == false) 
      { 
       gsProcess.Kill(); 
      } 
      gsProcess.EnableRaisingEvents = true; 

      gsProcess.Close(); 
     } 
     catch (Exception) 
     { 
     } 
    } 
関連する問題