2010-12-16 2 views
2

수정 더 이상 표시하지 않아도되는 코드를 재구성하려고했습니다. 나는 대화 상자를 사용하여 선택할 수있는 기능을 노출하지 않는 프린터 설정 클래스의 klimitation 일 뿐이라고 생각합니다. 그것은 구성 할 수 있어야하고 PrintDocument에 printerSettings 개체를 할당 한 다음 그 PrintDocument를 인쇄 할 수 있어야합니다 ... ??? 나는 여기에서 생각하지 않느냐? 또는?어떻게 프린터 셋팅을 위해 C#을 사용할 수 있습니까?

편집 다시 나는 모든 세터가 'printerSettings.DefaultPageSettings'에 앉아 있다고 생각합니다. 이렇게하면 프린터 설정을 수정할 수 있습니다. 나는 아직 그것을 증명하지만,하지 않은 것입니다 나중에

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#을 (수표, 편지, 문서)에서 워드 편지 병합 문서를 생성해야하지만이 모든 다른 프린터 설정 (확인 = 사용자 정의 설정, 문자 = DL을 필요로 Env, documents = A4)

프린터 설정 대화 상자를로드 할 때 이러한 설정이 저장되어 액세스 할 수 있지만 프린터 설정을 수동으로 변경하는 대신 코드로 만들 수 있습니다. 주위를 둘러 보았지만 프린터 설정 클래스가 있어야하지만 제대로 작동하지 않는 것 같습니다. 내가

//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은 메소드 실행 방법을 보여줍니다 :-).

편집 : WMI를 사용하여 일부 프린터 설정을 변경하는 것으로 보이는이 StackOverflow article: How do I programatically change printer settings ...이 눈치 챘습니다.

내 검색 코드는 다음과 같습니다

//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) 
     { 
     } 
    } 
관련 문제