Printing PDF from specific printer tray problem

Dear all,

For a C# project I would like to be able to print PDF documents from specific printer trays.
Up to now I did succeed in doing half the work.

1. I can print PDF documents by doing a DllImport of winspool.drv and send the raw bytes to the network printer.
By doing so, the PS/PDF capable printer understands the PDF bytes and prints the PDF flawlessly albeit using the default printer tray.
I thought of making copies of the printername with different default trays and use the different names in my application, to no avail...

2. I found a way of printing with different printer trays (using WMI or even better C# PrintDocument).
This works perfectly apart from that it is only capable of printing Strings (plain text that is).
When I open a PDF file and send the bytes to PrintDocument, this result in lots of papers with pacman characters on it!

I do not understand how to merge both methods into one.
Is it possible to set the printer tray with winspool.drv or sending raw bytes using PrintDocument
Any help is greatly appreciated!

Love you all!

Rutger


Answer this question

Printing PDF from specific printer tray problem

  • Capitán Cavernícola

    At least I can send raw bytes from the PDF file to the printer and it will print the PDF...
    Maybe that is because it natively understands PS/PDF bytes

    The PInvoke method does not satisfy me a lot. It requires Acrobat Reader to be installed on my webserver... I read somewhere about DDE messages and a COM component from the SDK, so maybe I delve into that... Thanks for helping me so far!

  • AndyPham

    Hmmm... I think of using the Adobe development API then...
    Thanks for mentioning!
    Too bad this is not possible in the Framework itself.

  • kawing0510

    Rutger Otto wrote:
    Hmmm... I think of using the Adobe development API then...
    Thanks for mentioning!
    Too bad this is not possible in the Framework itself.
    Acrobat is a specific application for viewing/printing print-formatted information. The Framework can't possibly directly support third-party files with specific print features.

    About the only direct support for PDF you can get out of the framework is to use Acrobats Printto shell verb (you can extract the command line from registry value HKEY_CLASSES_ROOT\AcroExch.Document.7\shell\Printto\command. This is a version-specific key/value and you can extract the current version's key from HEK_CLASSES_ROOT\AcroExch.Document\CurVer, which gives you the AcroExch.Document.n part of the key. That value contains a command line with parameter specifications ("%n", where n is 1 to 4 for the printto command). For the printto command you only need the first two parameters, which are the filename and the printer name. So, if you wanted to print a specific PDF and the Printto\command value was "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe" and a printer in the printers control panel applet named "HP LaserJet 6P" then you could use the Process.Start method to print the PDF file as follows:

    Process.Start(@"C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe", @"/t ""C:\acrobat.pdf"" ""HP LaserJet 6P"" """" """"");

    I believe doing it that way leaves Acrobat opened (bug)...

  • managar

    hey,
    Thanks a lot for ur help , i really appreciate it ; but it seems my printer always takes paper from its main tray.. whatever i do.. except when i print it manually...

    Thanks
    Sapz

  • Netoblivion

    Hi,
    i am also stuck with the same problem of changing printer trays and printing pdf using C# Can u please tell me how exactly you implemented it..

    Thanks
    Sapz

  • CBueche

    Hi Sapz,

    The following solutions works for me:

    Within the OS settings, create three different printers all using the same IP address and raw port (9100). Tell them apart by giving them meaningful names like "invoice printer", "casewrap printer" and "label printer" for instance.
    Now change their default printer settings with different paper sources (my situation tray 2-4).

    Then download and install GhostGum from http://www.ghostgum.com.au (and probably GhostScript and fonts if you've not already done so). This package has a utility called gsprint.exe (see also: http://www.cs.wisc.edu/~ghost/gsview/gsprint.htm). Before coding C# code for this application try on the commandline if the following works for you:

    gsprint.exe -dPDFFitPage filename.pdf -printer "invoice printer"

    And hope and pray for the papersource to be the one defined for the invoice printer...

    In the end I made a C# wrapper along this commandline tool that allows me to set programmatically the render (mono, grey and colour) and orientation (landscape and portrait) which I tend to use quite a lot. Too bad for the commandline to be called from a Windows Service, but at least it works (and even without Acrobat Reader!). Today I even prefer to use GSview in favour of the Acrobat Reader due to their ignorant update policy. Bollocks. Sorry...

    Good luck!

  • jasmine pham

    The native and WinForms methods of printing are for printing draw graphics. If you want to print a PDF document you have to use Acrobat or an Acrobat API. See http://www.adobe.com/devnet/acrobat/. Alternatively you might be able to find a component that supports printing acrobat files, like dynamicPDF

  • Printing PDF from specific printer tray problem