mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 12:09:40 +00:00
1843486050
Add xml documentation to PdfPrintSettings Change return type from Task to Task<bool> (exceptions are expensive and unnecessary) Fix small bug in assigning HeaderFooterTitle and HeaderFooterUrl (values were switched) Allow PrintToPdfAsync to accept nullptr for settings (use defaults) Update xml documentation in various instances for PrintToPdfAsync
197 lines
6.4 KiB
C#
197 lines
6.4 KiB
C#
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using CefSharp.Example;
|
|
using CefSharp.Wpf.Example.Controls;
|
|
using CefSharp.Wpf.Example.ViewModels;
|
|
using Microsoft.Win32;
|
|
|
|
namespace CefSharp.Wpf.Example
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
private const string DefaultUrlForAddedTabs = "https://www.google.com";
|
|
|
|
public ObservableCollection<BrowserTabViewModel> BrowserTabs { get; set; }
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
DataContext = this;
|
|
|
|
BrowserTabs = new ObservableCollection<BrowserTabViewModel>();
|
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.New, OpenNewTab));
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CloseTab));
|
|
|
|
CommandBindings.Add(new CommandBinding(CefSharpCommands.Exit, Exit));
|
|
CommandBindings.Add(new CommandBinding(CefSharpCommands.OpenTabCommand, OpenTabCommandBinding));
|
|
CommandBindings.Add(new CommandBinding(CefSharpCommands.PrintTabToPdfCommand, PrintToPdfCommandBinding));
|
|
|
|
Loaded += MainWindowLoaded;
|
|
|
|
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
|
|
Title += " - " + bitness;
|
|
}
|
|
|
|
private void CloseTab(object sender, ExecutedRoutedEventArgs e)
|
|
{
|
|
if (BrowserTabs.Count > 0)
|
|
{
|
|
//Obtain the original source element for this event
|
|
var originalSource = (FrameworkElement)e.OriginalSource;
|
|
|
|
BrowserTabViewModel browserViewModel;
|
|
|
|
if (originalSource is MainWindow)
|
|
{
|
|
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
|
|
BrowserTabs.RemoveAt(TabControl.SelectedIndex);
|
|
}
|
|
else
|
|
{
|
|
//Remove the matching DataContext from the BrowserTabs collection
|
|
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
|
|
BrowserTabs.Remove(browserViewModel);
|
|
}
|
|
|
|
browserViewModel.WebBrowser.Dispose();
|
|
}
|
|
}
|
|
|
|
private void OpenNewTab(object sender, ExecutedRoutedEventArgs e)
|
|
{
|
|
CreateNewTab();
|
|
|
|
TabControl.SelectedIndex = TabControl.Items.Count - 1;
|
|
}
|
|
|
|
private void MainWindowLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
CreateNewTab(CefExample.DefaultUrl, true);
|
|
}
|
|
|
|
private void CreateNewTab(string url = DefaultUrlForAddedTabs, bool showSideBar = false)
|
|
{
|
|
BrowserTabs.Add(new BrowserTabViewModel(url) { ShowSidebar = showSideBar });
|
|
}
|
|
|
|
private async void PrintToPdfCommandBinding(object sender, ExecutedRoutedEventArgs e)
|
|
{
|
|
if (BrowserTabs.Count > 0)
|
|
{
|
|
var originalSource = (FrameworkElement)e.OriginalSource;
|
|
|
|
BrowserTabViewModel browserViewModel;
|
|
|
|
if (originalSource is MainWindow)
|
|
{
|
|
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
|
|
}
|
|
else
|
|
{
|
|
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
|
|
}
|
|
|
|
var dialog = new SaveFileDialog
|
|
{
|
|
DefaultExt = ".pdf",
|
|
Filter = "Pdf documents (.pdf)|*.pdf"
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
var success = await browserViewModel.WebBrowser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
|
|
{
|
|
MarginType = CefPdfPrintMarginType.Custom,
|
|
MarginBottom = 10,
|
|
MarginTop = 0,
|
|
MarginLeft = 20,
|
|
MarginRight = 10,
|
|
});
|
|
|
|
if(success)
|
|
{
|
|
MessageBox.Show("Pdf was saved to " + dialog.FileName);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Unable to save Pdf, check you have write permissions to " + dialog.FileName);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OpenTabCommandBinding(object sender, ExecutedRoutedEventArgs e)
|
|
{
|
|
var param = e.Parameter.ToString();
|
|
string url = "";
|
|
|
|
switch (param)
|
|
{
|
|
case "BindingTest":
|
|
{
|
|
url = CefExample.BindingTestUrl;
|
|
break;
|
|
}
|
|
case "ListPlugins":
|
|
{
|
|
url = CefExample.PluginsTestUrl;
|
|
break;
|
|
}
|
|
case "PopupTest":
|
|
{
|
|
url = CefExample.PopupParentUrl;
|
|
break;
|
|
}
|
|
case "FishGl":
|
|
{
|
|
url = "http://www.fishgl.com/";
|
|
break;
|
|
}
|
|
case "MsTestDrive":
|
|
{
|
|
url = "http://dev.modern.ie/testdrive/";
|
|
break;
|
|
}
|
|
case "DragDemo":
|
|
{
|
|
url = "http://html5demos.com/drag";
|
|
break;
|
|
}
|
|
case "PopupTestCustomScheme":
|
|
{
|
|
url = CefExample.PopupTestUrl;
|
|
break;
|
|
}
|
|
case "BasicSchemeTest":
|
|
{
|
|
url = CefExample.BasicSchemeTestUrl;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
throw new Exception("Please provide a valid command parameter for binding");
|
|
}
|
|
|
|
CreateNewTab(url, true);
|
|
|
|
TabControl.SelectedIndex = TabControl.Items.Count - 1;
|
|
}
|
|
|
|
private void Exit(object sender, ExecutedRoutedEventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
}
|