Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

564 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