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

Method PrintToPdf

SampleApps/WebView2APISample/FileComponent.cpp:102–142  ·  view source on GitHub ↗

[PrintToPdf] Shows the user a file selection dialog, then uses the selected path when printing to PDF. If `enableLandscape` is true, the page is printed in landscape mode, otherwise the page is printed in portrait mode.

Source from the content-addressed store, hash-verified

100// printing to PDF. If `enableLandscape` is true, the page is printed
101// in landscape mode, otherwise the page is printed in portrait mode.
102void FileComponent::PrintToPdf(bool enableLandscape)
103{
104 WCHAR defaultName[MAX_PATH] = L"WebView2_PrintedPdf.pdf";
105 OPENFILENAME openFileName = CreateOpenFileName(defaultName, L"PDF File\0*.pdf\0");
106 if (GetSaveFileName(&openFileName))
107 {
108 wil::com_ptr<ICoreWebView2PrintSettings> printSettings = nullptr;
109 if (enableLandscape)
110 {
111 wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6;
112 CHECK_FAILURE(m_appWindow->GetWebViewEnvironment()->QueryInterface(
113 IID_PPV_ARGS(&webviewEnvironment6)));
114 if (webviewEnvironment6)
115 {
116 CHECK_FAILURE(webviewEnvironment6->CreatePrintSettings(&printSettings));
117 CHECK_FAILURE(
118 printSettings->put_Orientation(COREWEBVIEW2_PRINT_ORIENTATION_LANDSCAPE));
119 }
120 }
121
122 wil::com_ptr<ICoreWebView2_7> webview2_7;
123 CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webview2_7)));
124 if (webview2_7)
125 {
126 m_printToPdfInProgress = true;
127 CHECK_FAILURE(webview2_7->PrintToPdf(
128 openFileName.lpstrFile, printSettings.get(),
129 Callback<ICoreWebView2PrintToPdfCompletedHandler>(
130 [this](HRESULT errorCode, BOOL isSuccessful) -> HRESULT {
131 CHECK_FAILURE(errorCode);
132 m_printToPdfInProgress = false;
133 m_appWindow->AsyncMessageBox(
134 (isSuccessful) ? L"Print to PDF succeeded"
135 : L"Print to PDF failed",
136 L"Print to PDF Completed");
137 return S_OK;
138 })
139 .Get()));
140 }
141 }
142}
143//! [PrintToPdf]
144
145bool FileComponent::IsPrintToPdfInProgress()

Callers

nothing calls this directly

Calls 2

GetWebViewEnvironmentMethod · 0.80
AsyncMessageBoxMethod · 0.80

Tested by

no test coverage detected