Hey guys,

I have been looking around for a while but I can't seem to find any tutorial on how to print the contents of a RichEdit to a printer. Any help would be appreciated and if anyone has any links to a good tutorial please post them.


Thanks ANcient Dragon, however my program does not currently use MFC and both of those examples do. (Sorry I should have mentioned that before) Do you know of any ways to do it with just Win32 and not MFC?


Hey guys,

I have been working on my program some more and currently I am using this function from the msdn docs to print from my richedit:

BOOL PrintRTF(HWND hwnd, HDC hdc)

    HDC hDC = hdc;

    DOCINFO di = { sizeof(di) };

    if (!StartDoc(hdc, &di))
        return FALSE;

    int cxPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETX);
    int cyPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETY);

    int cxPhys = GetDeviceCaps(hdc, PHYSICALWIDTH);
    int cyPhys = GetDeviceCaps(hdc, PHYSICALHEIGHT);

    SendMessage(hwnd, EM_SETTARGETDEVICE, (WPARAM)hdc, cxPhys/2);


    fr.hdc       = hdc;
    fr.hdcTarget = hdc;

    // Set page rect to physical page size in twips.
    fr.rcPage.top    = 0;
    fr.rcPage.left   = 0;
    fr.rcPage.right  = 12000;//MulDiv(cxPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSX));
    fr.rcPage.bottom = 15000;//MulDiv(cyPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSY));

    // Set the rendering rectangle to the pintable area of the page.
    fr.rc.left   = cxPhysOffset;
    fr.rc.right  = 12000;//cxPhysOffset + cxPhys;
    fr.rc.top    = cyPhysOffset;
    fr.rc.bottom = 15000;//cyPhysOffset + cyPhys;

    SendMessage(hwnd, EM_SETSEL, 0, (LPARAM)-1);          // Select the entire contents.
    SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);  // Get the selection into a CHARRANGE.

    BOOL fSuccess = TRUE;

    // Use GDI to print successive pages.
    while (fr.chrg.cpMin < fr.chrg.cpMax && fSuccess)
        fSuccess = StartPage(hdc) > 0;

        if (!fSuccess) break;

        int cpMin = SendMessage(hwnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);

        if (cpMin <= fr.chrg.cpMin)
            fSuccess = FALSE;

        fr.chrg.cpMin = cpMin;
        fSuccess = EndPage(hdc) > 0;

    SendMessage(hwnd, EM_FORMATRANGE, FALSE, 0);

    if (fSuccess)



    return fSuccess;


This works great for the first time you print, but after it runs once the richedit gets a bit messed up and the words in it all get shoved to the left. Any ideas why this is happening and how to fix it?

I took some pictures to show what it looks like before and after I print:

Before: 0d7de499c350193ab7ec1c97e05e20ee

After: 879784159bf90aa93b8a236e6fc3b2c9

This article has been dead for over six months. Start a new discussion instead.