0

Below code am using in web application for the barcode(zebra barcode printer) printing .if i give the print in application server i am able print .

[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 SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
    Int32 dwError = 0, dwWritten = 0;
    IntPtr hPrinter = new IntPtr(0);
    DOCINFOA di = new DOCINFOA();
    bool bSuccess = false; // Assume failure unless you specifically succeed.

    //di.pDocName = "My C#.NET RAW Document";
    //di.pDataType = "RAW";

    // Open the printer.
    if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
    {
        // Start a document.
        if (StartDocPrinter(hPrinter, 1, di))
        {
            // Start a page.
            if (StartPagePrinter(hPrinter))

            {
                // Write your bytes.
                bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                EndPagePrinter(hPrinter);
            }
            EndDocPrinter(hPrinter);

        }
        ClosePrinter(hPrinter);
    }
    // If you did not succeed, GetLastError may give more information
    // about why not.
    if (bSuccess == false)
    {
        dwError = Marshal.GetLastWin32Error();
    }
    return bSuccess;
}

Here szPrinterName is printer name from drop down box from application. here I am able to get the printer name installed in application server only and print will go to server.

How do I get the client machine printer name and print in client machine printer ?

dmarietta
  • 1,940
  • 3
  • 25
  • 32
bharath
  • 33
  • 2
  • 6

1 Answers1

1

The short answer is, as noted in the comments above by @PabloRomeo, is that you cannot do what you are describing in a simple manner. The reason being the web browser does not allow direct access to local resources, including the printer. This is done for security purposes. With that said, there are some ways to get the type of experience you want if the machines that will be accessing the site will install some helper software you would need to write. I provided some significant detail on this in a similar type of question a while ago which you can see at the link below.

Print barcodes from web page to Zebra printer

Community
  • 1
  • 1
dmarietta
  • 1,940
  • 3
  • 25
  • 32