Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am working on printing a document for my class group task. but i couldn't find anything to print from c++ console app. is it possible to do so? if yes, how?

What I have tried:

cannot implement PAGESETUPDLG, PrintDocument,
Posted
Updated 6-Jun-21 17:25pm

If your document uses a format that is registered for printing, you might use the PowerShell (e.g. by writing a script executed from your console application):
start-process -File-Path "path-to-document" -Verb Print


Otherwise you have to prepare your data to be in a format understood by the printer (raw data).

All the following applies only if you have plain text data and your printer has a text mode.

Then prepare the print data by placing the command to select the text mode (see printer language documentation), followed by the text (which might require to be converted to a different character set), and optionally the command to leave the text mode.

Once you have that, you can use CreateFile and WriteFile to send the data to the printer. This requires having the device name for the printer. In the good old times of the Centronics interface this was just "LPT1". With modern USB or network printers you have to enumerate the printers (explaining how to do this would exceed quick answer) or look it up in the registry.

It might be also possible to use the Windows Print and Sppol API but I have never used it:
HOWTO: Send Raw Data to a Printer by Using the Win32 API[^]
 
Share this answer
 
Printing means to send data to a special output, in this case the printer. The basic approach is explained in this stack overflow answer.

Maybe in this discussion on MSDN is a useful link.

The basic tasks are explained in this How To: Print from a Windows Program. If you need to print from console, you must create a DC in the program, draw on it and then print it out.

An additional article about Win32 API Printer Tutorial C++ using GDI Functions.
 
Share this answer
 
v2
Comments
Member 13541763 27-Nov-17 9:40am    
in the second link, actually they don't need printer dialog while i need that. i think this is must for me.
Dave Kreskowiak 27-Nov-17 10:34am    
There is no dialog in a CONSOLE application. If you need the print dialog, a console app is not the correct project type.
Member 13541763 27-Nov-17 13:17pm    
ok then it mean i have to save it to an image file. currently i will not have printer in my class. thank you for the help. :)
Dave Kreskowiak 27-Nov-17 15:15pm    
OK, so you're trying to "print" to a Bitmap? So what's stopping you?

Create a Bitmap of an appropriate size (remember DPI!) and write the code to draw the text on the "page".
While that shows up on google on page 1.

A possible solution would be to use:

PdfCreateRenderer function (windows.data.pdf.interop.h) - Win32 apps | Microsoft Docs[^]

then clone the GDI, convert to the right bitmap format to send to the printer. Indeed pages and related must be handled then.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900