Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

Got this code in the net to print a file in a folder. Now I get an error
C#
"System.Drawing.PrinterException:No printers are installed"



I have the printer in the network.
Below is the code

C#
public void printimage()
     {



         string path = Server.MapPath("~/bin/Reciept3.rtf");

         reader = new StreamReader(path);
         //Create a Verdana font with size 10
         verdana10Font = new Font("Verdana", 10);
         //Create a PrintDocument object
         PrintDocument pd = new PrintDocument();
         //Add PrintPage event handler

         pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
         //Call Print Method

         pd.Print();

     }

     private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)
     {
         //Get the Graphics object
         Graphics g = ppeArgs.Graphics;
         float linesPerPage = 0;
         float yPos = 0;
         int count = 0;
         //Read margins from PrintPageEventArgs
         float leftMargin = ppeArgs.MarginBounds.Left;
         float topMargin = ppeArgs.MarginBounds.Top;
         string line = null;
         //Calculate the lines per page on the basis of the height of the page and the height of the font
         linesPerPage = ppeArgs.MarginBounds.Height /
         verdana10Font.GetHeight(g);
         //Now read lines one by one, using StreamReader
         while (count < linesPerPage && ((line = reader.ReadLine()) != null))
         {
             //Calculate the starting position
             yPos = topMargin + (count *
             verdana10Font.GetHeight(g));
             //Draw text
             g.DrawString(line, verdana10Font, Brushes.Black,
             leftMargin, yPos, new StringFormat());
             //Move to next line
             count++;
         }
         //If PrintPageEventArgs has more pages to print
         if (line != null)
         {
             ppeArgs.HasMorePages = true;
         }
         else
         {
             ppeArgs.HasMorePages = false;
         }
     }

     private void releaseObject(object obj)
     {
         try
         {
             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
             obj = null;
         }
         catch (Exception ex)
         {
             obj = null;
             MessageBox.Show("Unable to release the Object " + ex.ToString());
         }
         finally
         {
             GC.Collect();
         }
     }


What should i do to get the printer?
Posted

Is this an asp.net application?

The following statement makes me suspect that it is: string path = Server.MapPath("~/bin/Reciept3.rtf");

If this, printing from a server side asp.net application, is what you are trying to do then you need to make sure that the asp.net user has access to the printer.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Abhinav S 5-Apr-12 4:55am    
My 5.
Espen Harlinn 5-Apr-12 5:09am    
Thank you Abhinav!
As the error suggests, check if printers are installed on your machine.
Goto Start -> Devices and printers and ensure that at least one printer is installed and set as default.
 
Share this answer
 
Comments
Anele Ngqandu 2-Apr-12 6:48am    
no sir not working...printer is instaled but stil...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900