2011-10-29 25 views
1

私はprintdocument1.print()をしようとしています。このテキストデータがプリンタで印刷されないのはなぜですか?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Printing; 
using System.IO; 
using System.Management; 
using System.Diagnostics; 

namespace csvform 
{ 
public partial class Form3 : Form 
{ 

     private PrintDocument printDocument1 = new PrintDocument(); 
    private string stringToPrint; 
    public Form3() 
    { 
     InitializeComponent(); 


     printDocument1.PrintPage += 
      new PrintPageEventHandler(printDocument1_PrintPage); 

    } 


    private void ReadFile() 
    { 
     string docName = "testPage.txt"; 
     string docPath = @"c:\"; 
     printDocument1.DocumentName = docName; 
     using (FileStream stream = new FileStream(docPath + 
     docName, FileMode.Open)) 
     using (StreamReader reader = new StreamReader(stream)) 
     { 
      stringToPrint = reader.ReadToEnd(); 
     } 
    } 

    private void printDocument1_PrintPage(object sender, 
    PrintPageEventArgs e) 
    { 
     int charactersOnPage = 0; 
     int linesPerPage = 0; 
     Font nf = new Font(new FontFamily("Arial"), 10, 
     System.Drawing.FontStyle.Bold); 
     // Sets the value of charactersOnPage to the number of characters 
     // of stringToPrint that will fit within the bounds of the page. 
     e.Graphics.MeasureString(stringToPrint,nf, 
      e.MarginBounds.Size, StringFormat.GenericTypographic, 
      out charactersOnPage, out linesPerPage); 

     // Draws the string within the bounds of the page 
     e.Graphics.DrawString(stringToPrint,nf, Brushes.Black, 
      e.MarginBounds, StringFormat.GenericTypographic); 

     // Remove the portion of the string that has been printed. 
     stringToPrint = stringToPrint.Substring(charactersOnPage); 

     // Check to see if more pages are to be printed. 
     e.HasMorePages = (stringToPrint.Length > 0); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 


     ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT *   FROM Win32_Printer"); 
     string printerName = ""; 
     foreach (ManagementObject printer in searcher.Get()) 
     { 
      printerName = printer["Name"].ToString().ToLower(); 
      if (printerName.Equals(@"\\chenraqdc2.raqmiyat.local\hp color laserjet cp1510")) 
      {            
        // printDocument1.Print(); 
        try 
        { 
         // Assumes the default printer. 
         ReadFile(); 
         printDocument1.Print(); 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show("An error occurred while printing", ex.ToString()); 
        }      

      } 
     }  
    } 
} 
} 
+2

印刷ジョブをプリンタドライバに渡すと、プリンタドライバの動作を制御することはできません。 – Oded

+0

@oded:それは印刷されていないdonmine!私は今質問を編集しました – pravz

+0

私はこれを私のコードのように設定すると、それは私が適切なプリンタを選択した場合、プリンタの設定ダイアログボックスを表示していたことを意味しています印刷が正しく実行されていれば自動的に印刷を検出しなければなりません印刷ダイアログボックスが表示されないようにするにはどうすればいいですか?(printDialog1.ShowDialog()== DialogResult.OK) { printDocument1.PrintController = printcontrol; printDocument1.Print(); } – pravz

答えて

1

For historical purposes

システムは、ここでは任意のもの enter image description here

は、コードの後ろに私のC#コードでファイル名とシステムとしてポップアップ表示されたエラーを投げるうちに黙ってばかりの小さな模型を見せていました

// namespace declaration 
using System.Management; 

// code sample for setting default printer 

ManagementObjectCollection objManagementColl; 
// class used to invoke the query for specified management collection 
ManagementObjectSearcher objManagementSearch = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); 

// invokes the specified query and returns the resulting collection 
objManagementColl = objManagementSearch.Get(); 

foreach(ManagementObject objManage in objManagementColl) 
    { 
     if (objManage["Name"].ToString() == "RequiredPrinterName") // compares the name of printers 
      { 
      objManage.InvokeMethod("SetDefaultPrinter", null); // invoke [SetDefaultPrinter] method 
      break; 
      } 
     } 
関連する問題