Printing to printer in C++

Posted on 2002-04-04
Last Modified: 2008-03-17

I am writing a C++ client application that will be receiving a set of lines for printing ie. say 50 lines per transmission from a server.  My application is supposed to receive these lines on a socket and send it to the printer.

The question is how do I send these periodic lines that I receive from the server and send it to the printer?  How do I decide when the lines are equal to one page and forward it to the printer?

Any kind of help would be appreciated.  An example code would be really helpful.
Thanks in advance.

P.S: This is my first time in asking a question so please bear with me if you dont understand it.
Question by:jaldu

Expert Comment

ID: 6919740
A relatively easy approach is to jsut write to some sort of temp file and then fire it off to the printer queue.  then things like page sizes and such are not an issue.

Or, on unix systems, you can write directly to the printer's file descriptor, just like any other file (not usually a great option).

If it's not simple text, you'll need to learn the API of some sort of third party library.  For portability, I recommend Qt.  A good library will do most of the work for you, like getting the page size etc, then you typically define what's to be printed by creating a "graphic" of what you want on the page.  But all this varies depending on what library you use.

Author Comment

ID: 6920744

Writing to a temp file and then firing it off to a printer queue is a good idea.  I have tried to look up for code on how to send a file to a printer but couldnt find any.  I have seen examples like:

#include <fstream.h>

int main()

ofstream print; // stream variable declaration"LPT1"); // open stream

// Print Text (the character ‘\f’ will produce a form feed)
print << "This text will print on the printer.\f";
print.close(); // close stream
return 0;

But I want a dialog box to come up(on NT) and let the user pick any network printer.

So in lieu of the above example I should be writing all that I receive from the server to a temp file and then once done read the file line by line and send it to the printer?

Thanks in advance.


Expert Comment

ID: 6922023
Sorry, I have no idea how to get the printer selcteion APIs on Windows.  You may have a bit of luck looking up that part on MSDN.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Accepted Solution

TheFriend earned 20 total points
ID: 7004215
Here's the sequence, see doc's for proper syntax

Call PrintDlg() to get a DC, paper size, printer capabilities. That dialog works in both ways, with user interaction and without.
Calculate the surface size you need to print. If you print text only, use GetTextExtentPoint32() to get the height of a line of current font.
You can start a print job right away with StartDoc().
Then use StartPage() to start a new page.
Use TextOut() to print to the printer DC.
Check if have printed enough lines on this page.
Call EndPage() and goto StartPage() again.
Call EndDoc() when you are done.

PrintDlg can give you the default printer as well.

If the time between transmitions from the server is too long, you might print one page per document. So don't go to StartPage() but call EndPage() and EndDoc() and the wait for more data from that 50-line-server. This releases the spooler and completes the print job after one pagefull.

All the functions mentioned have their 'overview' in MSDN publications with examples included.

I am not including any example since the structures used are to big with too many parameters and require a lot of typing. Sorry.


Author Comment

ID: 7005855
Hi TheFriend,


>>>I am not including any example since the structures used are to big with too many parameters and require
a lot of typing. Sorry.

What you have written will at least get me going.  So thanks.

I will let you know once I look into it and try out few things to get a better understanding.



LVL 11

Expert Comment

ID: 7122083
Dear jaldu

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept


comment(s) as an answer.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is:


Expert Comment

ID: 7138044
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question