Thread: Printing text to a number of pages

  1. #1
    Registered User
    Join Date
    Mar 2011

    Question Printing text to a number of pages


    I'm trying to write a program to print some text on a page. I did some research and found out how to achieve it basicly:

    PRINTDLG pd;
    memset( &pd, 0, sizeof( pd ) );
    pd.lStructSize = sizeof( pd );
    if( !PrintDlg( &pd ) )
    MessageBox( NULL, "PrintDlg( &pd ) failed!", "Fatal Error", MB_OK ,MB_ICONERROR );
    return -1;
    DOCINFO di;
    HDC hPrinter = pd.hDC;
    memset( &di, 0, sizeof( di ) );
    di.cbSize = sizeof( di );
    StartDoc( hPrinter, &di );
    StartPage( hPrinter );
    TextOut( hPrinter, 100, 100, "Hello, world", 12);
    EndPage( hPrinter );
    EndDoc( hPrinter );
    DeleteDC( hPrinter );
    However, i'm trying to print text written by the user, so i don't have any idea how much pages it will take. Obviously, i won't be able to use the StartPage-EndPage structure. So how can i make this work for variable amount of text?

  2. #2
    Registered User
    Join Date
    Jun 2009
    I found that the Windows printing stuff can get rather complicated specially when you deal with things like: headers, trailers, margins, handling of tabs, etc.. I suggest checking out Petzold's, "Programming Windows" book for an example on printing for some basics. In order to print on multiple pages you need to calculate the number of characters that will fit on a line and the number of lines of text (for a given font) that will fit on a page. All these variables are then handled/incremented via loop(s). I hope this helps.

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    The problem is that font size differs greatly on a printer DC and screen DC.

    The 2 ways to do this are with world transformations and basic math.

    Basic math uses DrawText() with DT_CALCRECT to find the required area, divide into pages and print.
    The size of the printer DC can be found with GetDeviceCaps() using HORZRES and VERTRES and the printer DC
    You can also use something like GetTextExtPoint32() to find the height of one line with a given font on the Printer DC. You then calc the number of pages based on the number of lines you have.
    This is harder as you have to know how many lines you have and the width of a string varies depending on the actual combination of letters the string contains.

    Another way is convert your print preview DC into the printer DC using scaling functions like SetViewportExtEx(), SetViewportOrgEx(), SetWindowOrgEx(), SetWindowExtEx() and SetWorldTransform().
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  2. Getting number of pages printed
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 09-21-2004, 02:59 PM
  3. Determining number of pages in file
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2004, 09:53 PM
  4. Replies: 0
    Last Post: 03-28-2003, 07:20 AM
  5. how to create vdu pages in text mode for vga ?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-12-2002, 05:15 AM

Tags for this Thread