Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Printing to printer in C++

Posted on 2002-04-04
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
print.open("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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

TheFriend earned 80 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: http://www.experts-exchange.com/commspt/


Expert Comment

ID: 7138044
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

610 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