1

I have found this question and associated answer which is very useful to me. There are other questions but not really the same situation.

So I created a CLR project is Visual C++ and for now added this:

int main()
{
    // initialize PrintDocument object
    PrinterSettings^ mySettings = gcnew PrinterSettings();

    // set the printer to 'Microsoft Print to PDF'
    mySettings->PrinterName = "Microsoft Print to PDF";

    // tell the object this document will print to file
    mySettings->PrintToFile = true;

    mySettings->PrintFileName = "d:\\testprint.pdf";

    // set the filename to whatever you like (full path)
    PrintDocument^ doc = gcnew PrintDocument();
    doc->PrinterSettings = mySettings;

    doc->Print();

    return 0;
}

In itself it works and will create a empty PDF document. Not much use. :)

My issue is that the context is all wrong. I have an unmanaged application written in Visual C++ and it includes a HTML Viewer. This is derived from CHtmlView. At the moment I support the ability for the user to print to a document. It is done like this:

void CChristianLifeMinistryHtmlView::DoPrintPreview()
{
    HWND      HWND_PP, HWND_FG ;
    TCHAR     szClassName[256] ;
    ULONGLONG t1, t2 ;
    RECT      workArea;

    ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, nullptr, nullptr);

    HWND_PP = nullptr ;
    t1 = ::GetTickCount64();
    t2 = ::GetTickCount64(); // Extra line required for 'while' rearrangement.
    while (HWND_PP == nullptr && t2 - t1 <= 6000) // if found or Timeout reached...
    {
        HWND_FG = ::GetForegroundWindow(); // Get the ForeGroundWindow
        if (HWND_FG != nullptr)
        {
            ::GetClassName(HWND_FG, szClassName, 256 );
            if (lstrcmp(szClassName, IE_PPREVIEWCLASS) == 0) // Check for Print Preview Dialog
                HWND_PP = HWND_FG ;
        }

        theApp.PumpMessage();
        t2 = ::GetTickCount64();
    }

    if (HWND_PP != nullptr)
    {
        // Showing maximized crops bottom of preview dialog
        ::SystemParametersInfo( SPI_GETWORKAREA, 0, &workArea, 0 );
        ::MoveWindow(HWND_PP, workArea.left, workArea.top,
            workArea.right - workArea.left, workArea.bottom - workArea.top, TRUE);

    }

}

Works no problems at all. But what I am now trying to do is add the ability, from this derived CHtmlView object to print to a PDF file using Microsoft Print to PDF. This needs to be done in the background (at the request of the user) and then I will email this printed PDF as an attachment to the parties concerned.

I know how to create and send emails with a attachments. And in principle I know how to use CLR to kick start printing to a PDF document.

I understand that I most likely must write a CLR Class Library and then from Visual C++ call the managed library code to do the printing. But I am at a loss as to the basic mechanics of how to do what I want and I can't find anything on the internet.

I did find this that basically describes printing to PDF but again, the context is not right for my purposes.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

0 Answers0