0
votes

I am trying to send a PDF to a thermal printer using c#. I have looked at the RawPrinterHelper class here http://support.microsoft.com/kb/322091 but the SendFileToPrinter is not printing the file.

There is no error and the file appears to be printing from the print queue but nothing happens.

The printer works fine as I have been able to print other items on it.

Does anyone know how I can send a PDF to print or how I can possibly use the SendFileToPrinter to work for me.

I am working on Windows 7.

Here is a sample of the code I am using to call the SendFileToPrinter method.

try
        {
            RawPrinterHelper.SendFileToPrinter(PrinterName,@"C:\test.pdf");

        }
        catch (Exception ex)
        {
            Console.WriteLine(" EXCEPTION: {0}", ex.Message);
        }

Update: Ok, maybe I spoke too soon. I am able to print the PDF to the thermal printer but the issue now is that this is taking a couple of seconds to print and I am looking for something that is "quick". The reason it is slow is that Adobe needs to open up first.

Anyone have any ideas on how to get around this?

1
I have no idea what printer model you are using, but it's unlikely that it understands the PDF format natively. You'll want to print to the Zebra printer driver. - David Crowell
@DavidCrowell, I can print to the Zebra Printer (GK420d) using the PDF Preview screen. - Nollaig
The PDF Preview screen probably prints using the Windows API. It doesn't send the PDF file unprocessed to the printer. - David Crowell
@DavidCrowell, Any ideas how I can print to the Zebra printer driver? - Nollaig
The printing is the easy part. Look up the System.Drawing and System.Drawing.Printing namespaces. The hard part is loading the PDF file and drawing it to a surface. You might be better off using an application that can do the PDF printing for you, and calling that from your app. - David Crowell

1 Answers

2
votes

Ok, actaully got it sorted.

I was able to do the following and it works perfectly.

string tempFile = @"C:\test.pdf";
            try
            {
                ProcessStartInfo gsProcessInfo;
                Process gsProcess;
                string printerName = PrinterName; 

                gsProcessInfo = new ProcessStartInfo();
                gsProcessInfo.Verb = "PrintTo";
                gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
                gsProcessInfo.FileName = tempFile;
                gsProcessInfo.Arguments = "\"" + printerName + "\"";
                gsProcess = Process.Start(gsProcessInfo);
                if (gsProcess.HasExited == false)
                {

                    gsProcess.Kill();
                }
                gsProcess.EnableRaisingEvents = true;

                gsProcess.Close();
            }
            catch (Exception)
            {
            }

@DavidCrowell, thats for the assistance.

Noel.