One possibility is CoreWebView2. It is the same component used by Microsoft Edge for showing content - including PDF documents. Since this is a graphical user control, it only really makes sense for use in client facing applications (WinUI, WPF, Winforms, etc.) where you want to print documents from a document preview. It was not designed for a service even though you may be able to get it to work. (I haven't tried.)
Check these two methods:
Here's an overview of three methods for printing from the control.
Also, a hint for getting started is to make sure to use the CoreWebView2InitializationCompleted event to initialize the control or you'll run into errors related to null values while setting properties. This is a rough template for getting started. I used this with a C# Winforms application in .Net6:
private static Microsoft.Web.WebView2.Core.CoreWebView2Environment _webView2Environment;
private Microsoft.Web.WebView2.Core.CoreWebView2Deferral _webView2Deferral;
private Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs _webView2Args;
public PanelDocumentViewer()
{
InitializeComponent();
}
private void PanelDocumentViewer_Load(object sender, EventArgs e)
{
webView2.CoreWebView2InitializationCompleted += CoreWebView2_CoreWebView2InitializationCompleted;
webView2.EnsureCoreWebView2Async(_webView2Environment);
}
private void CoreWebView2_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
webView2.CoreWebView2.Settings.HiddenPdfToolbarItems =
CoreWebView2PdfToolbarItems.Search
| CoreWebView2PdfToolbarItems.Save
| CoreWebView2PdfToolbarItems.SaveAs;
if (_webView2Deferral != null)
{
_webView2Args.NewWindow = webView2.CoreWebView2;
_webView2Deferral.Complete();
}
webView2.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView2.CoreWebView2.Settings.AreDevToolsEnabled = false;
webView2.CoreWebView2.ContextMenuRequested += CoreWebView2_ContextMenuRequested;
webView2.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
private void CoreWebView2_ContextMenuRequested(object? sender, CoreWebView2ContextMenuRequestedEventArgs e)
{
e.Handled = true; // set to true to prevent the default right-click menu from displaying
e.MenuItems.Clear(); // remove all right-click menu items
}
private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs e)
{
_webView2Args = e;
_webView2Deferral = e.GetDeferral();
}
private void ButtonPrint_Click(object? sender, EventArgs e)
{
PrintNowAsync();
}
private async Task PrintNowAsync(CancellationToken cancellationToken = default)
{
// This prompts the user with a dialog box before printing.
webView2.CoreWebView2.ShowPrintUI(CoreWebView2PrintDialogKind.System);
}