?
Solved

Printing to printer in C++

Posted on 2002-04-04
7
Medium Priority
?
601 Views
Last Modified: 2008-03-17
Hi,

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.
0
Comment
Question by:jaldu
[X]
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
7 Comments
 
LVL 4

Expert Comment

by:ct.smith
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.
0
 

Author Comment

by:jaldu
ID: 6920744
Thanks.

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.

0
 
LVL 4

Expert Comment

by:ct.smith
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.
0
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

by:
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.

TheFriend
0
 

Author Comment

by:jaldu
ID: 7005855
Hi TheFriend,

Thanks.

>>>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.

Thanks.

RJ

0
 
LVL 11

Expert Comment

by:griessh
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

     "TheFriend"

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/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7138044
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

777 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