|
| 1 | +# Background |
| 2 | +When open a PDF file in Edge, there will be a toolbar at the top. The toolbar provide the functionality like print, save and annotation. |
| 3 | +And this PDF viewer is also available in WebView2. And we provide the API to enable users to customization the PDF toolbar. |
| 4 | + |
| 5 | + |
| 6 | +# Description |
| 7 | +We add a new `HiddenPdfToolbarItems` property in `CoreWebView2Settings`. This API allows users to hide buttons on the PDF toolbar. |
| 8 | +Currently, this API can hide the _save button_, _save as button_, and _print button_. |
| 9 | +By default, `HiddenPdfToolbarItems` is equal to `None` which means no button will be hidden. |
| 10 | + |
| 11 | +# Examples |
| 12 | +## C++ |
| 13 | + |
| 14 | +```cpp |
| 15 | +wil::com_ptr<ICoreWebView2> webView; |
| 16 | +void SettingsComponent::ToggleHidePdfToolbarButtons() |
| 17 | +{ |
| 18 | + // Get webView's current settings |
| 19 | + wil::com_ptr<ICoreWebView2Settings> coreWebView2Settings; |
| 20 | + CHECK_FAILURE(webView->get_Settings(&coreWebView2Settings)); |
| 21 | + |
| 22 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM hiddenPdfToolbarItems; |
| 23 | + CHECK_FAILURE(coreWebView2Settings->get_HiddenPdfToolbarItems(&hiddenPdfToolbarItems)); |
| 24 | + |
| 25 | + if (hiddenPdfToolbarItems == COREWEBVIEW2_PDF_TOOLBAR_ITEM::COREWEBVIEW2_PDF_TOOLBAR_ITEM_NONE) |
| 26 | + { |
| 27 | + CHECK_FAILURE(coreWebView2Settings->put_HiddenPdfToolbarItems( |
| 28 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM::COREWEBVIEW2_PDF_TOOLBAR_ITEM_PRINT | |
| 29 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM::COREWEBVIEW2_PDF_TOOLBAR_ITEM_SAVE)); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + CHECK_FAILURE(coreWebView2Settings->put_HiddenPdfToolbarItems( |
| 34 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM::COREWEBVIEW2_PDF_TOOLBAR_ITEM_NONE)); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## C# |
| 40 | +```c# |
| 41 | +private WebView2 _webView; |
| 42 | +void ToggleHidePdfToolbarButtons(object target, ExecutedRoutedEventArgs e) |
| 43 | +{ |
| 44 | + var coreWebView2Settings = _webView.CoreWebView2.Settings; |
| 45 | + if(WebViewSettings.HiddenPdfToolbarItems == CoreWebView2PdfToolbarItem.Save | CoreWebView2PdfToolbarItem.Print) |
| 46 | + { |
| 47 | + WebViewSettings.HiddenPdfToolbarItems = CoreWebView2PdfToolbarItem.None; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + WebViewSettings.HiddenPdfToolbarItems = CoreWebView2PdfToolbarItem.Save; |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +# API Notes |
| 57 | +See [API Details](#api-details) section below for API reference. |
| 58 | + |
| 59 | +# API Details |
| 60 | +## Win32 C++ |
| 61 | +```c# |
| 62 | +/// Specifies the PDF toolbar item types used for the `ICoreWebView2StagingSettings::put_HiddenPdfToolbarItems` method. |
| 63 | +[v1_enum] |
| 64 | +typedef enum COREWEBVIEW2_PDF_TOOLBAR_ITEM { |
| 65 | + |
| 66 | + /// No item |
| 67 | +
|
| 68 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM_NONE = 0x0, |
| 69 | + |
| 70 | + /// The save button |
| 71 | +
|
| 72 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM_SAVE = 0x0001, |
| 73 | + |
| 74 | + /// The print button |
| 75 | +
|
| 76 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM_PRINT = 0x0002, |
| 77 | + |
| 78 | + /// The save as button |
| 79 | +
|
| 80 | + COREWEBVIEW2_PDF_TOOLBAR_ITEM_SAVE_AS = 0x0004, |
| 81 | + |
| 82 | + |
| 83 | +} COREWEBVIEW2_PDF_TOOLBAR_ITEM; |
| 84 | +cpp_quote("DEFINE_ENUM_FLAG_OPERATORS(COREWEBVIEW2_PDF_TOOLBAR_ITEM);") |
| 85 | + |
| 86 | +[uuid(183e7052-1d03-43a0-ab99-98e043b66b39), object, pointer_default(unique)] |
| 87 | +interface ICoreWebView2Settings6 : ICoreWebView2Settings5 { |
| 88 | + /// `HiddenPdfToolbarItems` is used to customize the PDF toolbar items. By default, it displays all of the items. |
| 89 | + /// \snippet SettingsComponent.cpp ToggleHidePdfToolbarItems |
| 90 | +
|
| 91 | + [propget] HRESULT HiddenPdfToolbarItems([out, retval] COREWEBVIEW2_PDF_TOOLBAR_ITEM* hidden_pdf_toolbar_items); |
| 92 | + |
| 93 | + [propput] HRESULT HiddenPdfToolbarItems([in] COREWEBVIEW2_PDF_TOOLBAR_ITEM hidden_pdf_toolbar_items); |
| 94 | +} |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +## .NET and WinRT |
| 100 | + |
| 101 | +```c# |
| 102 | +namespace Microsoft.Web.WebView2.Core |
| 103 | +{ |
| 104 | + // |
| 105 | + // Summary: |
| 106 | + // Specifies the PDF toolbar item types used for the <see cref="CoreWebView2Settings.HiddenPdfToolbarItems"/> |
| 107 | + [Flags] |
| 108 | + public enum CoreWebView2PdfToolbarItem |
| 109 | + { |
| 110 | + // |
| 111 | + // Summary: |
| 112 | + // No item. By default the `CoreWebView2Settings.HiddenPdfToolbarItems` equal to this value. |
| 113 | + None = 0, |
| 114 | + |
| 115 | + // |
| 116 | + // Summary: |
| 117 | + // The save button on PDF toolbar. |
| 118 | + Save = 1, |
| 119 | + |
| 120 | + // |
| 121 | + // Summary: |
| 122 | + // The print button on PDF toolbar. |
| 123 | + Print = 2, |
| 124 | + |
| 125 | + // |
| 126 | + // Summary: |
| 127 | + // The save as button on PDF toolbar. |
| 128 | + SaveAs = 4 |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +namespace Microsoft.Web.WebView2.Core |
| 133 | +{ |
| 134 | + public partial class CoreWebView2Settings |
| 135 | + { |
| 136 | + // |
| 137 | + // Summary: |
| 138 | + // Used to customize the PDF toolbar items. |
| 139 | + // |
| 140 | + // Remarks: |
| 141 | + // By default, it equal to `CoreWebView2PdfToolbarItem.None` which means displays all of the items. |
| 142 | + public CoreWebView2PdfToolbarItem HiddenPdfToolbarItems { get; set; } |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
0 commit comments