Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / MFC

Example of How to Print a Long CString across Multiple Pages of Output using the MFC Print Architecture

Rate me:
Please Sign up or sign in to vote.
4.50/5 (10 votes)
19 Jun 2002CPOL 126.8K   2.4K   26   21
An example project of how to break up a long string that you want to print across multiple pages

Sample Image - Preview.jpg

Introduction

A common question in the forums in the past has been how do I break up a long string and print it using the standard MFC architecture. Above is an example project that does just that. You can enter a long string to print in a MultiLine edit control. To see what the output would be like for your printer, you can select the Print Preview option from the file menu.

There are some drawbacks in the example in that it doesn't handle tabs correctly as it only uses CDC::TextOut() when printing, and CDC::GetTextExtent() when measuring whether the currently selected line will fit in one line, or needs to be broken up across multiple lines. This can be changed by yourselves as a programming exercise using the CDC::DrawText() function.

I hope this example will help out beginners at printing.

Enjoy!

License

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


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions

 
QuestionThank u! Pin
sally899821-Jun-11 22:38
sally899821-Jun-11 22:38 
GeneralTechnical adaptation to a CEditView derived SDI application Pin
Raymond_Gimilio8-Feb-10 20:35
Raymond_Gimilio8-Feb-10 20:35 
GeneralPage setup Pin
TariqZ5-Jun-07 19:33
TariqZ5-Jun-07 19:33 
GeneralFaster method (Great article Btw) Pin
Y_R1-Feb-07 9:55
Y_R1-Feb-07 9:55 
QuestionPage Breaks Using MFC 6.0+ Pin
omar6314-Nov-06 9:27
omar6314-Nov-06 9:27 
GeneralMultiple print job Pin
vivadot12-Aug-04 11:59
vivadot12-Aug-04 11:59 
Generalconstructing long string... Pin
vivadot8-Jul-04 6:01
vivadot8-Jul-04 6:01 
GeneralRe: constructing long string... Pin
Atewa22-Jul-04 10:44
Atewa22-Jul-04 10:44 
GeneralRe: constructing long string... Pin
vivadot31-Jul-04 12:00
vivadot31-Jul-04 12:00 
GeneralCEditView and CString Pin
The_Inventor21-Jun-04 20:14
The_Inventor21-Jun-04 20:14 
I am using Windows XP Pro. I am using Microsoft Visual C++ .NET 7.0 (2002). I am using a computer I have built and assembled myself. I have written a program that uses CEditView as the public base class for one of my Child View classes. I used SetWindowText(text) to place text into the view, (works great). Now I want to use the Print Preview and Print features of the base class.

I have tried calling the CEditView defaults of the OnBeginPrinting(), OnPrint(), OnEndPrinting(),
and all I get is the first character of the text string in the font that I want and nothing else.Mad | :mad:

I have applied your code in the proper places. It compiles fine. I used GetWindowText(text) as well as a CString object, in the proper places. IT bombs in the Print Preview durring execution. Cry | :(( In debugmode it is bombing at line 484 of viewedit.cpp:

UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1);// this is the line Mad | :mad:
ASSERT(nPageSave >= (UINT)m_aPageStart.GetSize());

I am not sure why this is as of course there might only be 1 page of print, and as I follow nPageSave in the watch window it is definatly set a 1.Confused | :confused:

I have spent many hours. I want to just see the text in the Print Preview as TEXT, not as a screen dump, or as a rendered bitmap or what ever, just the text and only the text. I don't want any background colors from the screen, JUST the TEXT.

Please help!!!!!!!!!!!!!!

Sincerely,

Donald

The World as we think we know it Has a lot more to it than meets the eye.

A Mad Scientist who has seen a wormhole for himself....
GeneralRe: CEditView and CString Pin
tom-kaltofen30-Jan-06 23:37
tom-kaltofen30-Jan-06 23:37 
GeneralWord Wrap Pin
Gerard Lim31-May-04 3:00
Gerard Lim31-May-04 3:00 
Generalthanks a lot Pin
abicer27-Apr-04 22:06
abicer27-Apr-04 22:06 
GeneralPrint question Pin
nikoladsp13-Jan-04 6:19
nikoladsp13-Jan-04 6:19 
GeneralPrinting to a Printer/Help Pin
melshehu16-Oct-02 5:58
melshehu16-Oct-02 5:58 
GeneralRe: Printing to a Printer/Help Pin
Roger Allen17-Oct-02 2:37
Roger Allen17-Oct-02 2:37 
GeneralRe: Printing to a Printer/Help Pin
melshehu17-Oct-02 5:54
melshehu17-Oct-02 5:54 
GeneralJust what I was looking for Pin
-Dy24-Jul-02 1:17
-Dy24-Jul-02 1:17 
GeneralRe: Just what I was looking for Pin
Roger Allen24-Jul-02 3:06
Roger Allen24-Jul-02 3:06 
GeneralGood job. Pin
Shog920-Jun-02 17:59
sitebuilderShog920-Jun-02 17:59 
GeneralRe: Good job. Pin
Roger Allen20-Jun-02 22:47
Roger Allen20-Jun-02 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.