MCPcopy Create free account
hub / github.com/MicrosoftEdge/WebView2Samples / PrintToPrinter

Method PrintToPrinter

SampleApps/WebView2APISample/AppWindow.cpp:1300–1384  ·  view source on GitHub ↗

[PrintToPrinter] This example prints the current web page to a specified printer with the settings.

Source from the content-addressed store, hash-verified

1298//! [PrintToPrinter]
1299// This example prints the current web page to a specified printer with the settings.
1300bool AppWindow::PrintToPrinter()
1301{
1302 std::wstring printerName = GetPrinterName();
1303 // Host apps custom print settings based on the user selection.
1304 SamplePrintSettings samplePrintSettings = GetSelectedPrinterPrintSettings(printerName);
1305
1306 wil::com_ptr<ICoreWebView2_16> webView2_16;
1307 CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2_16)));
1308
1309 wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6;
1310 CHECK_FAILURE(m_webViewEnvironment->QueryInterface(IID_PPV_ARGS(&webviewEnvironment6)));
1311
1312 wil::com_ptr<ICoreWebView2PrintSettings> printSettings;
1313 CHECK_FAILURE(webviewEnvironment6->CreatePrintSettings(&printSettings));
1314
1315 wil::com_ptr<ICoreWebView2PrintSettings2> printSettings2;
1316 CHECK_FAILURE(printSettings->QueryInterface(IID_PPV_ARGS(&printSettings2)));
1317
1318 CHECK_FAILURE(printSettings->put_Orientation(samplePrintSettings.Orientation));
1319 CHECK_FAILURE(printSettings2->put_Copies(samplePrintSettings.Copies));
1320 CHECK_FAILURE(printSettings2->put_PagesPerSide(samplePrintSettings.PagesPerSide));
1321 CHECK_FAILURE(printSettings2->put_PageRanges(samplePrintSettings.Pages.c_str()));
1322 if (samplePrintSettings.Media == COREWEBVIEW2_PRINT_MEDIA_SIZE_CUSTOM)
1323 {
1324 CHECK_FAILURE(printSettings->put_PageWidth(samplePrintSettings.PaperWidth));
1325 CHECK_FAILURE(printSettings->put_PageHeight(samplePrintSettings.PaperHeight));
1326 }
1327 CHECK_FAILURE(printSettings2->put_ColorMode(samplePrintSettings.ColorMode));
1328 CHECK_FAILURE(printSettings2->put_Collation(samplePrintSettings.Collation));
1329 CHECK_FAILURE(printSettings2->put_Duplex(samplePrintSettings.Duplex));
1330 CHECK_FAILURE(printSettings->put_ScaleFactor(samplePrintSettings.ScaleFactor));
1331 CHECK_FAILURE(
1332 printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds));
1333 CHECK_FAILURE(
1334 printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds));
1335 CHECK_FAILURE(
1336 printSettings->put_ShouldPrintHeaderAndFooter(samplePrintSettings.HeaderAndFooter));
1337 CHECK_FAILURE(printSettings->put_HeaderTitle(samplePrintSettings.HeaderTitle.c_str()));
1338 CHECK_FAILURE(printSettings->put_FooterUri(samplePrintSettings.FooterUri.c_str()));
1339 CHECK_FAILURE(printSettings2->put_PrinterName(printerName.c_str()));
1340
1341 wil::unique_cotaskmem_string title;
1342 CHECK_FAILURE(m_webView->get_DocumentTitle(&title));
1343
1344 CHECK_FAILURE(webView2_16->Print(
1345 printSettings.get(),
1346 Callback<ICoreWebView2PrintCompletedHandler>(
1347 [title = std::move(title),
1348 this](HRESULT errorCode, COREWEBVIEW2_PRINT_STATUS printStatus) -> HRESULT
1349 {
1350 std::wstring message = L"";
1351 if (errorCode == S_OK && printStatus == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED)
1352 {
1353 message = L"Printing " + std::wstring(title.get()) +
1354 L" document to printer is succeeded";
1355 }
1356 else if (
1357 errorCode == S_OK &&

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected