USB port printing through c# code.

USB port printing through c# code.

Page 1 of 1

1 Replies - 5977 Views - Last Post: 17 September 2009 - 06:12 AM Rate Topic: -----

#1 Harendra.Kumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-September 09

USB port printing through c# code.

Posted 08 September 2009 - 10:20 PM

Hi

Can u help me to get the usb port inforamtion in c# code.
And also how to do usb port printing using c#.

This post has been edited by Harendra.Kumar: 08 September 2009 - 11:43 PM

Is This A Good Question/Topic? 0
  • +

Replies To: USB port printing through c# code.

#2 Harendra.Kumar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-September 09

Re: USB port printing through c# code.

Posted 17 September 2009 - 06:12 AM

Hi All
Below is the code given for any type of printer either usb port ,serial port.
Before using this code just change file path

Hope ! this will help other guys who faced the same problem.

public class PrintingExample : System.Windows.Forms.Form 
{
	private System.ComponentModel.Container components;
	private System.Windows.Forms.Button printButton;
	private Font printFont;
	private StreamReader streamToPrint;

   public PrintingExample() : base() 
   {
	  // The Windows Forms Designer requires the following call.
	  InitializeComponent();
   }

   // The Click event is raised when the user clicks the Print button.
   private void printButton_Click(object sender, EventArgs e) 
   {
	  try 
	  {
		  streamToPrint = new StreamReader
			 ("C:\\My Documents\\MyFile.txt");
		  try 
		  {
			 printFont = new Font("Arial", 10);
			 PrintDocument pd = new PrintDocument();
			 pd.PrintPage += new PrintPageEventHandler
				(this.pd_PrintPage);
			 pd.Print();
		  }  
		  finally 
		  {
			 streamToPrint.Close();
		  }
	  }  
	  catch(Exception ex) 
	  {
		  MessageBox.Show(ex.Message);
	  }
   }

   // The PrintPage event is raised for each page to be printed.
   private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
   {
	  float linesPerPage = 0;
	  float yPos = 0;
	  int count = 0;
	  float leftMargin = ev.MarginBounds.Left;
	  float topMargin = ev.MarginBounds.Top;
	  string line = null;

	  // Calculate the number of lines per page.
	  linesPerPage = ev.MarginBounds.Height / 
		 printFont.GetHeight(ev.Graphics);

	  // Print each line of the file.
	  while(count < linesPerPage && 
		 ((line=streamToPrint.ReadLine()) != null)) 
	  {
		 yPos = topMargin + (count * 
			printFont.GetHeight(ev.Graphics));
		 ev.Graphics.DrawString(line, printFont, Brushes.Black, 
			leftMargin, yPos, new StringFormat());
		 count++;
	  }

	  // If more lines exist, print another page.
	  if(line != null)
		 ev.HasMorePages = true;
	  else
		 ev.HasMorePages = false;
   }


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 17 September 2009 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1