0

Is there an easy way to print any file using the windows API? Basically, send a PostScript, PDF, JPG, etc to the API and have it print. Every C code example I've found uses a Device Context and draws to it, then passes the DC to the API. I know there is a RAW option where you can bypass the printer driver and send the file straight to the printer, but I'm finding that it doesn't work with all printers. I've been searching for a few hours and can't find anything.

Sean N.
  • 963
  • 2
  • 10
  • 23
  • Does this answer your question? [ShellExecute, "Print"](https://stackoverflow.com/questions/16604844/shellexecute-print) – Zeus May 13 '21 at 01:27
  • 2
    @SongZhu-MSFT Except the solution provided by that answer only applies to HTML files specifically, not to other file types generally. And even `ShellExecute("print")` doesn't work reliably, as not all programs register their file types to support `print` at all, or [do not register it to actually output to a printer](https://stackoverflow.com/questions/16892113/) (ie, Adobe, etc). – Remy Lebeau May 13 '21 at 01:54
  • It's about the best you can do as far as "print any file" goes though. – Jonathan Potter May 13 '21 at 05:12
  • @SongZhu-MSFT I don't understand how your suggestion can be used to print files other than HTML. – Sean N. May 13 '21 at 13:36

1 Answers1

1

Windows API has no idea about the content of "any file". So it cannot render it on paper (That's what I understand by "printing").

You have either to handle yourself the rendering, interpreting the content of the file (That's the kind of code you already found with DeviceContext and drawing) or you can pass the file to an application which is able to print it.

For the later, Windows API has a mechanism that Song Zhu already mentioned in a comment: calling ShellExecute with the print command. This generally works fine but not always since some application do not register them self as able to print their file.

As you mentioned in your question there is also the possibility to bypass WinApi to send content directly to the printer (RAW write). This allows an application to use specific feature of the printer that is not handled by his device driver. Using that method, your application becomes highly dependent on the printer model while using the printer driver (DeviceContext and drawing) make your application - if well written - to print on almost any printer supported by Window.

fpiette
  • 11,983
  • 1
  • 24
  • 46