如何使用 DHTML 打印整个文档中的特定内容

发布于 2024-12-08 07:54:04 字数 2093 浏览 0 评论 0原文

在这些示例中,我想打印 ID tab_form_1 中的内容。在尝试这些时 示例打印 tab_form_1 内容,当使用 CComptr 查询接口时 lpOleCommandTarget 变为 NULL。

如何解决这个问题?

样本

    IHTMLDocument2 *pDoc;
GetDHtmlDocument(&pDoc);


CComPtr<IHTMLElement2> spControl;       
if(GetElementInterface(_T("tab_form_1") ,&spControl) != S_OK)
    return; 

//ole command target
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;
spControl->QueryInterface(IID_IOleCommandTarget,(void**)&lpOleCommandTarget);ASSERT(lpOleCommandTarget);
//lpDispatch->Release();

if(lpOleCommandTarget == NULL) return;

//prepare header
CString header   = _T("Project Path: &w");

VARIANT     header_variant;
VariantInit(&header_variant);
V_VT(&header_variant)   = VT_BSTR;
V_BSTR(&header_variant) = CString(header).AllocSysString();

//prepare footer
CString footer   = _T("&d &t&b Page&p of &P");

VARIANT     footer_variant;
VariantInit(&footer_variant);
V_VT(&footer_variant)   = VT_BSTR;
V_BSTR(&footer_variant) = CString(footer).AllocSysString();

//prepare header footer safe arrray
SAFEARRAYBOUND  parameter_array_bound[1];
SAFEARRAY       *parameter_array = NULL;

parameter_array_bound[0].cElements = 2;
parameter_array_bound[0].lLbound   = 0;

parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound);

//HRESULT hr;
long index;

index = 0;
HRESULT hr    = SafeArrayPutElement(parameter_array,&index,&header_variant);

index = 1;
hr    = SafeArrayPutElement(parameter_array,&index,&footer_variant);

VARIANT parameter;
VariantInit(&parameter);
V_VT(&parameter)    = VT_ARRAY | VT_BYREF;
V_ARRAY(&parameter) = parameter_array;

// print contents of web browser control
lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, &parameter,NULL);

//clear all variants
VariantClear(&header_variant);
VariantClear(&footer_variant);
if (parameter_array != NULL) {
    SafeArrayDestroy(parameter_array);
}

lpOleCommandTarget->Release();

In these example, I want to print the contents in the ID tab_form_1.While trying these
sample to print the tab_form_1 contents,lpOleCommandTarget become NULL while query interface using CComptr .

How to solve this issue?

SAMMPLE

    IHTMLDocument2 *pDoc;
GetDHtmlDocument(&pDoc);


CComPtr<IHTMLElement2> spControl;       
if(GetElementInterface(_T("tab_form_1") ,&spControl) != S_OK)
    return; 

//ole command target
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;
spControl->QueryInterface(IID_IOleCommandTarget,(void**)&lpOleCommandTarget);ASSERT(lpOleCommandTarget);
//lpDispatch->Release();

if(lpOleCommandTarget == NULL) return;

//prepare header
CString header   = _T("Project Path: &w");

VARIANT     header_variant;
VariantInit(&header_variant);
V_VT(&header_variant)   = VT_BSTR;
V_BSTR(&header_variant) = CString(header).AllocSysString();

//prepare footer
CString footer   = _T("&d &t&b Page&p of &P");

VARIANT     footer_variant;
VariantInit(&footer_variant);
V_VT(&footer_variant)   = VT_BSTR;
V_BSTR(&footer_variant) = CString(footer).AllocSysString();

//prepare header footer safe arrray
SAFEARRAYBOUND  parameter_array_bound[1];
SAFEARRAY       *parameter_array = NULL;

parameter_array_bound[0].cElements = 2;
parameter_array_bound[0].lLbound   = 0;

parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound);

//HRESULT hr;
long index;

index = 0;
HRESULT hr    = SafeArrayPutElement(parameter_array,&index,&header_variant);

index = 1;
hr    = SafeArrayPutElement(parameter_array,&index,&footer_variant);

VARIANT parameter;
VariantInit(¶meter);
V_VT(¶meter)    = VT_ARRAY | VT_BYREF;
V_ARRAY(¶meter) = parameter_array;

// print contents of web browser control
lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, ¶meter,NULL);

//clear all variants
VariantClear(&header_variant);
VariantClear(&footer_variant);
if (parameter_array != NULL) {
    SafeArrayDestroy(parameter_array);
}

lpOleCommandTarget->Release();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-12-15 07:54:04

尝试这个示例。这将消除您的问题。此代码会将特定内容打印到新创建的文档中。

    // create DHtmlDocument for Newly Created Dialog
HRESULT hDispatch = printdlg->m_pBrowserApp->get_Document((IDispatch**)&pDisp);
if(hDispatch != S_OK) return;
HRESULT hResult = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pNewDoc);
if(hResult != S_OK) return;

//GetDHtmlDocument for new document
GetDHtmlDocument(&pDoc);

BSTR strSummaryText = GetElementHtml(L"tab_form_1");

// Creates a new one-dimensional array
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings == NULL) {

    return;
}

VARIANT *param;
HRESULT hresult = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = strSummaryText;

hresult = SafeArrayUnaccessData(psaStrings);
//it will write the div part of tab_form_1 into new document to pass the summary contents alone to the printer
hresult = pNewDoc->write(psaStrings);


// SafeArrayDestroy calls SysFreeString for each strtemp
if (psaStrings != NULL) {
    SafeArrayDestroy(psaStrings);
}

Try this sample.This will eradicate your problem.This code will print the particular contents into the newly created document.

    // create DHtmlDocument for Newly Created Dialog
HRESULT hDispatch = printdlg->m_pBrowserApp->get_Document((IDispatch**)&pDisp);
if(hDispatch != S_OK) return;
HRESULT hResult = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pNewDoc);
if(hResult != S_OK) return;

//GetDHtmlDocument for new document
GetDHtmlDocument(&pDoc);

BSTR strSummaryText = GetElementHtml(L"tab_form_1");

// Creates a new one-dimensional array
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings == NULL) {

    return;
}

VARIANT *param;
HRESULT hresult = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = strSummaryText;

hresult = SafeArrayUnaccessData(psaStrings);
//it will write the div part of tab_form_1 into new document to pass the summary contents alone to the printer
hresult = pNewDoc->write(psaStrings);


// SafeArrayDestroy calls SysFreeString for each strtemp
if (psaStrings != NULL) {
    SafeArrayDestroy(psaStrings);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文