ASP.NET - Connect to printer thru c# code

Asked By chrisma c on 06-Apr-10 02:14 AM

i am taking a button on a webpage,naming it print. i have in my web application..a virtual folder which contains some word docs and pdf format files.When i click my print button that document should directly get printed connecting to the printer. 
Using C#, asp.net in VS3.5.
P.S the documents are not from database, they are from my project virtual folder.ThankYou.

nanda Kishore replied to chrisma c on 06-Apr-10 02:28 AM
Class file:
public class RawPrinterHelper
{
    // Structure and API declarions:
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }
    [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level,  [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
    public static bool SendFileToPrinter( string szPrinterName, string szFileName )
 {       
        FileStream fs = new FileStream(szFileName, FileMode.Open); 
        BinaryReader br = new BinaryReader(fs); 
        Byte []bytes = new Byte[fs.Length];
        bool bSuccess = false;     
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);      
        bytes = br.ReadBytes( nLength );       
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);      
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);      
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);   
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }
}

Using System.Drawing.Printing; //aspx.cs code
    String pkInstalledPrinters;
    bool isDefaultPrinter=false;
            PrinterSettings pSettings = new PrinterSettings();
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                pSettings.PrinterName = pkInstalledPrinters;
       isDefaultPrinter =pSettings.IsDefaultPrinter;
if(isDefaultPrinter==true)
{
 RawPrinterHelper.SendStringToPrinter(pSettings.PrinterName, fileNamepdf);
}
            }
Anoop S replied to chrisma c on 06-Apr-10 02:43 AM

System
System.Drawing
System.Drawing.Printing

protected void PrintFile_Click (object sender, System.EventArgs e)
{
printDialog1.Document = ThePrintDocument;
string strText = this.richTextBox1.Text;
myReader = new StringReader(strText);
if (printDialog1.ShowDialog() == DialogResult.OK)
{
this.ThePrintDocument.Print();
}
}
protected void ThePrintDocument_PrintPage (object sender,System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.richTextBox1.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
// Iterate over the string using the StringReader, printing each line.
while(count < linesPerPage && ((line=myReader.ReadLine()) != null))
{
// calculate the next line position based on the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
// draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
myBrush.Dispose();
}
sravanthi replied to Anoop S on 09-Dec-10 05:20 AM
please help me i tried this code but it's not working
Anoop S replied to sravanthi on 10-Dec-10 12:18 PM
or you can use print is using Javascript. The command is window.print() which you could use either by putting it in the onLoad event of the form tag, or you could use it on a button click.
If your page has more on it than you want to print, you have two options. One is to create a different page that only has what you want printed on it (you could leave off menus and the like) or you can use the media type of CSS to tell the printer what to print and what not to. Here is a link to using CSS <a target="_new" href="http://www.w3.org/TR/REC-CSS2/media.html">http://www.w3.org/TR/REC-CSS2/media.html</a>