Printing Web Pages like Internet Explorer

.

To implement the print function i.e. when we click on the print button in the toolbar no print setup dialog will be displayed and the application will automatically start printing the document. This feature is also present in Microsoft Word. To implement this feature in your application just use the code given below.

void CMyBrowser::OnPrint() 
{
	LPOLECOMMANDTARGET pCmdTarg = NULL; 

	m_pDisp = m_WebBrowser2.GetDocument(); //get the IDispatch interface pointer
	ASSERT(m_pDisp);

	m_pDisp->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarg); //query for olecommandtarget interface
	ASSERT(pCmdTarg);

	pCmdTarg->Exec(NULL, //call the olecommandtarget's Exec method
		OLECMDID_PRINT,
		0,
		NULL,
		NULL
	);


	if (pCmdTarg) 
		pCmdTarg->Release(); // release document's command target

	if (m_pDisp) 
		m_pDisp->Release(); // release document's dispatch interfac

}
Get the IDispatch interface pointer by calling the Webbrowser controls GetDocument method. Then Query for the IOleCommandTarget interface. After getting the IOleCommandTarget pointer then call that interface's Exec method passing the OLECMDID_PRINT as the first parameter. For detailed explanation for Ole and the Interfaces involved in this piece of code please refer to visual c++ help. If you have any comments and suggestions or if you find any bugs in this article let me know.

Updated on May 23 1998



Comments

  • m_pDisp -- undeclared identifier

    Posted by dacylet on 03/01/2006 11:50am

    Would someone tell me what is this type? What should I declare it as? Thank you

    Reply
  • Name to print file as

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: Deepthi

    Hi,
    I am printing the file using execwb method and have set my default printer to acrobat distiller.It opens up the dailog box to name the file as and the path it needs to save to.Can I send these parameters without having the dailog box to open up.

    TIA

    Reply
  • The only idiot with a question

    Posted by Legacy on 03/03/2003 12:00am

    Originally posted by: Budlo

    Hey guru's,
    
    

    I'm having a little problem. The code given works great. I thought that m_WebBrowser2.ExecWB(OLECMDID_PRINT, 1, NULL, NULL);works even better. Now here's the problem:

    I want my program to print the page as is, without the address or pagenumber bullshit. So can anyone help me?

    thanks,
    budlo

    Reply
  • whats about dhtml control using IOleCommandTarget

    Posted by Legacy on 02/10/2003 12:00am

    Originally posted by: bhavani

    Hi,
    Instead of WEBBROWSER object i have taken DHTML control.How the print and printview functionality implements using IOleCommanadTarget.Please anyone guide me.

    Reply
  • HTML Printing problem

    Posted by Legacy on 04/24/2002 12:00am

    Originally posted by: Bahar

    Y doesnt the following code work.
    I'm trying to print a html file.Though the file is seen in the print queue no output is seen in the prinout.
    Plz help.
    thanks in advance
    Bahar

    CPrintDialog dlg(FALSE, PD_NOPAGENUMS | PD_NOSELECTION | PD_USEDEVMODECOPIES | PD_DISABLEPRINTTOFILE|PD_RETURNDC );
    // Get the default printer.
    dlg.DoModal();


    DEVMODE* dm = (DEVMODE*)::GlobalLock(dlg.m_pd.hDevMode);

    HDC hdc = dlg.CreatePrinterDC();

    CDC dc;
    dc.Attach( hdc );
    if ( dc )
    {


    DOCINFO docinfo;
    memset(&docinfo, 0, sizeof(docinfo));

    docinfo.cbSize = sizeof(docinfo);
    docinfo.lpszDocName =htmlDocumentName ;

    dc.StartPage();

    dc.StartDoc(&docinfo);


    //CGdiObject* pOldFont = dc.SelectStockObject(SYSTEM_FONT);
    //dc.TextOut(50, 50, _T("Hello World!"), 12);
    //dc.SelectObject(pOldFont);
    TR_PRINT(_T("Orientation %d "),dm->dmOrientation);

    dc.EndPage();
    dc.EndDoc();

    }
    dc.Detach();

    CDC::FromHandle(hdc)->DeleteDC();

    ::GlobalUnlock(dlg.m_pd.hDevMode);

    Reply
  • How to open the page from the cusomizes app

    Posted by Legacy on 11/12/2001 12:00am

    Originally posted by: Ram

    Hi
    I have built customized web browser application using the CHtmlView and the print functionality provided in your article. However whenever I try to browse a page , it opens the url in a new IE browser window. I would like to open it from the custom MFC application browser window. Is there a way to do this ?
    thanks
    ram

    Reply
  • How to intercept the contents of Homepage before showing in the IE browser without proxy?

    Posted by Legacy on 02/20/2001 12:00am

    Originally posted by: Yung-Ming Kuo

    Dear friends:

    I want to intercept and get the contents(including text and image) of homepage from Web server ,before it shows the IE browser(version 5.0).But it is not through proxy.

    And i have ability to prevent HTML document from showing the browser.

    Could you offer some suggestion or source code sample to solve my question?

    Thank you ^_^

    email:ymkuo@neural.ee.ncku.edu.tw

    by Kuo

    Reply
  • make a form print from a print button on page

    Posted by Legacy on 08/08/2000 12:00am

    Originally posted by: Alan Payne

    Can any one help i want a form to print by clicking a print button in a form thanks Alan
    

    Reply
  • ExecWB and Debugger

    Posted by Legacy on 10/05/1999 12:00am

    Originally posted by: G. Bremer

    Printing Web Pages using ExecWB works fine - if the App is run separately. However, if it is run under Debugger control (VS6/SP3) the behaviour is quite strange (such as the Printer Dialog always popping up regardless the Mode specified) and it actually doesn't print. The errCode returned by ExecWB is also not very helpful. Does anyone have an Idea how this can be avoided?

    Reply
  • CWebBrowser2::GetDocument fails!

    Posted by Legacy on 09/29/1999 12:00am

    Originally posted by: Simon Hill

    I have a CWebBrowser control which has successfully
    navigated to an html file and I can see this file.
    However when I try to print the file the program ASSERTS
    at the CWebBrowser2::GetDocument() stage.

    Help!

    Simon Hill

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds