0

I am trying to print .pdf and .tif files using C# in windows application.

Printing is done successfully, but my problem is Adobe reader is opening in background for pdf files and windows print dialogue opens for tif files.

Actually i will run my method using a service, so these process should occur silently. What can I do to avoid this?

Here is my code

public void PrintDocument(string filepath)
        {

            //PrintDialog pd = new PrintDialog();            
            printProcess.StartInfo.FileName = filepath; 
            //Also tried usecellexcecution=false;
            //Redirect=true; something like this
            printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            printProcess.StartInfo.Verb = "Print";
            printProcess.StartInfo.CreateNoWindow = true;
            PrinterSettings setting = new PrinterSettings();
            setting.DefaultPageSettings.Landscape = true; 
            printProcess.Start();
            printProcess.CloseMainWindow(); 
        }

I have tried to use RawprinterHelper suggested in MSDN, but it prints junk characters.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Satheesh
  • 892
  • 2
  • 16
  • 38
  • 2
    In the future, please use correct grammar and spelling. `plzzzz` and multiple `.....` have no place in a serious question. Fixed that for you. – Oded Aug 12 '11 at 07:43

4 Answers4

1

If your printer is on a network and you know it's IP address it might be possible to send the file directly to the printer using TcpClient. I've got this to work for my printer, but have only tried it for PDFs so I don't know how well it will work for other printers/file types.

You will have to change your printer settings so that it is using a tcp port (In devices and printers select your printer (single click), then click print server properties, in the wizard that opens you can add a new TCP port). You will also have to set the printer to raw rather than lpc settings

I then used something similar to the following method;

        public void SilentPrint(string filePath, string printerIPAddress)
    {
        byte[] bytes = System.IO.File.ReadAllBytes(filePath);

        var client = new TcpClient(printerIPAddress, 9100);//9100 is the default print port for raw data

        using (var stream = client.GetStream())
        {
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
        }
    }
mark_h
  • 5,233
  • 4
  • 36
  • 52
1

Get the window handle of the pdf process and then hide it or use process class's windowstyle to minimize it.

Yes it gets launched because you are using Process.Start.

dplante
  • 2,445
  • 3
  • 21
  • 27
Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

try "PrintTo" as your Verb. This should disable the 'print preview' stage.

else
  • 872
  • 6
  • 15
0

If you want to print a PDF silently then you will need to use the right tool for the job. In this case it isn't Adobe Reader because it will always launch the application window, it doesn't load in silent mode.

Find a a PDF library that will silently let you print PDFs.

Rowan
  • 2,384
  • 2
  • 21
  • 22
  • But am not looking for any 3rd party dll.Is it impossible with c# code – Satheesh Aug 16 '11 at 06:08
  • You need code for rendering a PDF so that it can be sent to the printer. This is complex code, so it's going to be at a minimum hundreds, if not thousands, of lines of code. You might be able to find a C# open source project that lets you print PDFs, but off the top of my head I don't know any. The most popular C# open source library, iTextSharp, does not let you print PDFs. This is why you will probably need to use a third party DLL. You may not want to, but you might have to. – Rowan Aug 18 '11 at 12:39