rtf Printing in ActiveX Control

Posted on 2006-04-27
Last Modified: 2011-09-20
I have created an ActiveX control which provides the local printers as properties and which prints RAW data onto one of them (the data comes from a server in base64).

I'd like to be able to print RTF format (which I'm assured is plain 7-bit text commands) along the lines of (Javascript) :-

   var printer = new ActiveXObject("RatSoft.RatPrint.1");

   printer.printerName = "Epson 700";


where string is a RTF string (eg: {\rtf1\ansi\ansicpg1252\deff0\deflang1031{\f.......)

How do I proceed?

Question by:BigRat
    LVL 86

    Accepted Solution

    Can you use a 'CRichEditCtrl' in your aActiveX (it does not have to be displayed, however it would make handling of both RTF and printing a lot easier)? The outline would be to

        BOOL            bCtrlReady;

        CRichEditCtrl*  pCtrl;
        EDITSTREAM      esIn;

        esIn.dwCookie       =   ( DWORD) [pointer to RTF text];
        esIn.pfnCallback    =   ReadStreamCallBack; // just copes the text from 'dwCookie' to a buffer

        lBytes  =   pCtrl->StreamIn ( SF_RTF, esIn);

        HDC hPrinter = CreateDC(pDriver,pPrinter,pPort,NULL);

        FORMATERANGE fr; // set that up

        pCtrl->FormatRange( &fr, TRUE);

        pCtrl->DisplayBand( &rect);

        pCtrl->DestroyWindow ();

        delete  pCtrl;

    LVL 27

    Author Comment

    >>Can you use a 'CRichEditCtrl' in your aActiveX

    If I remember rightly this control is wrapped by WordPad so I don't need to install anything additional. So yes.

    I don't understand the FORMATTERRANGE and the Rect bit. I thought that an RTF document was complete? I don't want any user interaction, it should just print the contents.
    LVL 86

    Expert Comment

    Sorry for the delay, but I simply forgot :-(

    The article at ("How to print the content of a Rich Edit Control") describes how to handle these, I only used the control itself (as an invisible window) for RTF<->Text conversion.

    BTW, the missing part in the above is 'ReadStreamCallBack()' which basically would be

    ReadStreamCallBack  (   DWORD   dwCookie,
                            LPBYTE  pbBuff,
                            LONG    cb, // number of bytes to read or write
                            LONG*   pcb // pointer to number of bytes transferred
        BOOL    bRC =   TRUE;

        char* pRtf =    ( char*) dwCookie;

        *pcb = strlen ( pRtf)

        CopyMemory  (   pbBuff, pRtf,   strlen ( pRtf));

        return  (   0);

    for the simple case that the buffer is big enough.
    LVL 27

    Author Comment

    Hmmmm. I downloaded the project code and built the program with VS 6.0. When I open any RTF file (eg: save from WordPad or one of the license texts on my XP) it shows nothing and prints a blank page on my OKI printer.
    LVL 27

    Author Comment

    I'll try it later on my Win2000 at home.
    LVL 27

    Author Comment

    I'm afraid jkr's code did not really help, since I'm writing a simple ActiveX control without MFC. I have gotten so far :-

    // local print procedure
    int Print(HDC hPrinterDC, HWND hRTFWnd, LPPRINTDLG pd)
          FORMATRANGE fr;
          int nHorizRes   = GetDeviceCaps(hPrinterDC, HORZRES),
              nVertRes    = GetDeviceCaps(hPrinterDC, VERTRES),
              nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX),
              nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
          LONG lTextLength;   // Length of document.
          LONG lTextPrinted;  // Amount of document printed.

          // Ensure the printer DC is in MM_TEXT mode.
          if(!::SetMapMode ( hPrinterDC, MM_TEXT))
                return HRESULT_FROM_WIN32(::GetLastError());

          // Rendering to the same DC we are measuring.
          ZeroMemory(&fr, sizeof(fr));
          fr.hdc = fr.hdcTarget = hPrinterDC;

          // Set up the page.
          fr.rcPage.left     = = 0;
          fr.rcPage.right    = ::MulDiv(nHorizRes, 1440, nLogPixelsX);
          fr.rcPage.bottom   = ::MulDiv(nVertRes, 1440, nLogPixelsY);

          // Set up 1" margins all around.
          fr.rc.left   = fr.rcPage.left + 1440;  // 1440 TWIPS = 1 inch.
    = + 1440;
          fr.rc.right  = fr.rcPage.right - 1440;
          fr.rc.bottom = fr.rcPage.bottom - 1440;

          // Default the range of text to print as the entire document.
          fr.chrg.cpMin = 0;
          fr.chrg.cpMax = -1;

          // Set up the print job (standard printing stuff here).
        DOCINFO DocInfo1;
          ZeroMemory(&DocInfo1, sizeof(DocInfo1));
          DocInfo1.cbSize       = sizeof(DOCINFO);
        DocInfo1.lpszDocName  = L"Bibdia";
        DocInfo1.lpszOutput   = NULL;
        DocInfo1.lpszDatatype = NULL;
          DocInfo1.fwType       = 0;

          // Start the document.
          if(::StartDoc(hPrinterDC, (DOCINFO*)&DocInfo1)<=0)
                return HRESULT_FROM_WIN32(::GetLastError());

          // Find out real size of the document in characters.
          GETTEXTLENGTHEX getTextLengthEx;

          getTextLengthEx.flags = GTL_PRECISE;
          getTextLengthEx.codepage = CP_ACP;

          lTextLength = SendMessage ( hRTFWnd, EM_GETTEXTLENGTHEX,
                                                          (WPARAM)&getTextLengthEx, 0);

                // Start the page.
                      return HRESULT_FROM_WIN32(::GetLastError());

                // Print as much text as can fit on a page. The return value is
                // the index of the first character on the next page. Using TRUE
                // for the wParam parameter causes the text to be printed.

                lTextPrinted = SendMessage(hRTFWnd,

                // Print last page.
                      return HRESULT_FROM_WIN32(::GetLastError());

                // If there is more text to print, adjust the range of characters
                // to start printing at the first character of the next page.
                if (lTextPrinted < lTextLength)
                      fr.chrg.cpMin = lTextPrinted;
                      fr.chrg.cpMax = -1;
          } while (lTextPrinted < lTextLength);

          // Tell the control to release cached information.
          SendMessage(hRTFWnd, EM_FORMATRANGE, 0, (LPARAM)NULL);

          if(::EndDoc (hPrinterDC)<=0)
                return HRESULT_FROM_WIN32(::GetLastError());
          return S_OK;


    which works reasonably. I now have to incorporate collation, multiple copies and working out how many pages there are in order to fill in the Print Dialog box.
    LVL 20

    Expert Comment

    Then why did you accept the answer here? :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now