2011-10-29 79 views
1
當我試圖做printdocument1.print()

;系統顯示小模型彈出的文件名和系統得到保持沉默,用了拋出任何錯誤的任何東西 enter image description here爲什麼這個文本數據沒有通過打印機打印?

這裏是代碼隱藏我的C#代碼:

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

如果我在我的代碼中設置了這個代碼,它就意味着它顯示打印機設置對話框,如果我選擇適當的打印機意味着打印正在發生但是我的需求自動地應該檢測到打印必須打印並且打印必須是HAPPENING對於如何避免不來打印機對話框任何一個可以PLEASE如果(printDialog1.ShowDialog()== DialogResult.OK) { printDocument1.PrintController = printcontrol; printDocument1.Print(); } – pravz

回答

1

For historical purposes

// 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; 
      } 
     } 
相關問題