Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
Implement Javascript call the C# method to print content.
Browse files Browse the repository at this point in the history
  • Loading branch information
liangminhua committed Dec 23, 2016
1 parent 8ccd54e commit 8813d77
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 20 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 liangminhua

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 12 additions & 6 deletions PrintBrowser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintBrowser", "PrintBrowse
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|Any CPU.Build.0 = Release|Any CPU
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|x64.ActiveCfg = Debug|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|x64.Build.0 = Debug|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|x86.ActiveCfg = Debug|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Debug|x86.Build.0 = Debug|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|x64.ActiveCfg = Release|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|x64.Build.0 = Release|x64
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|x86.ActiveCfg = Release|x86
{D710CFE3-0CF6-43F7-8512-0FFE30FFC077}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 8 additions & 1 deletion PrintBrowser/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CefSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
Expand All @@ -13,5 +14,11 @@ namespace PrintBrowser
/// </summary>
public partial class App : Application
{
App()
{
var settings = new CefSettings();
//Perform dependency check to make sure all relevant resources are in our output directory.
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
}
}
}
26 changes: 26 additions & 0 deletions PrintBrowser/Internal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;

namespace PrintBrowser
{
class Internal
{
[STAThread]
public void print(string printerName, string xaml)
{
Print.PrintXaml(printerName, xaml);
}

public string[] GetPrinters()
{
PrintQueueCollection pqc = Print.GetPrintQueues();
var Printers = from queues in pqc
select queues.FullName;
return Printers.ToArray();
}
}
}
5 changes: 3 additions & 2 deletions PrintBrowser/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:local="clr-namespace:PrintBrowser"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
Title="PrintBrowser">
<Grid>

<cefSharp:ChromiumWebBrowser Name="webBrowser" Address="file:///./index.html" />
</Grid>
</Window>
4 changes: 4 additions & 0 deletions PrintBrowser/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp.Wpf;

namespace PrintBrowser
{
Expand All @@ -23,6 +24,9 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
MenuHandler menuHandler = new MenuHandler();
webBrowser.MenuHandler = menuHandler;
webBrowser.RegisterJsObject("Internal", new Internal());
}
}
}
53 changes: 53 additions & 0 deletions PrintBrowser/MenuHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrintBrowser
{
internal class MenuHandler : IContextMenuHandler
{
private const int ShowDevTools = 26501;
private const int CloseDevTools = 26502;
private const int ReloadNoCache = 103;

void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
//To disable the menu then call clear
// model.Clear();

//Removing existing menu item
//bool removed = model.Remove(CefMenuCommand.ViewSource); // Remove "View Source" option

//Add new custom menu items
model.Clear();
model.AddItem((CefMenuCommand)ReloadNoCache, "Refresh");
model.AddItem((CefMenuCommand)ShowDevTools, "Show DevTools");
model.AddItem((CefMenuCommand)CloseDevTools, "Close DevTools");
}

bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if ((int)commandId == ShowDevTools)
{
browser.ShowDevTools();
}
if ((int)commandId == CloseDevTools)
{
browser.CloseDevTools();
}
return false;
}

void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{

}

bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
}
73 changes: 62 additions & 11 deletions PrintBrowser/PrintBrowser.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.props" Condition="Exists('..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.props')" />
<Import Project="..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.props" Condition="Exists('..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -13,29 +15,54 @@
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="ReachFramework" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Printing" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand All @@ -62,12 +89,15 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Internal.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="MenuHandler.cs" />
<Compile Include="Print.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
Expand All @@ -85,6 +115,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -93,5 +124,25 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<None Include="index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\cef.redist.x64.3.2785.1486\build\cef.redist.x64.targets" Condition="Exists('..\packages\cef.redist.x64.3.2785.1486\build\cef.redist.x64.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>此项目引用这台计算机上缺少的 NuGet 程序包。使用 NuGet 程序包还原可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\cef.redist.x64.3.2785.1486\build\cef.redist.x64.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\cef.redist.x64.3.2785.1486\build\cef.redist.x64.targets'))" />
<Error Condition="!Exists('..\packages\cef.redist.x86.3.2785.1486\build\cef.redist.x86.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\cef.redist.x86.3.2785.1486\build\cef.redist.x86.targets'))" />
<Error Condition="!Exists('..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.props'))" />
<Error Condition="!Exists('..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.targets'))" />
<Error Condition="!Exists('..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.props'))" />
<Error Condition="!Exists('..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.targets'))" />
</Target>
<Import Project="..\packages\cef.redist.x86.3.2785.1486\build\cef.redist.x86.targets" Condition="Exists('..\packages\cef.redist.x86.3.2785.1486\build\cef.redist.x86.targets')" />
<Import Project="..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.targets" Condition="Exists('..\packages\CefSharp.Common.53.0.1\build\CefSharp.Common.targets')" />
<Import Project="..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.targets" Condition="Exists('..\packages\CefSharp.Wpf.53.0.1\build\CefSharp.Wpf.targets')" />
</Project>
37 changes: 37 additions & 0 deletions PrintBrowser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>

<head>
<title></title>
</head>

<body>
<h1>PrintTest</h1>
<div id="printers"></div>
<button onclick="print()">PrintSomething</button>
</body>
<script>
var printers = Internal.getPrinters();
for (var i = 0; i < printers.length; ++i) {
var radio = "<input name='currentPrinter' type='radio' value='" + printers[i] + "'>" + printers[i] + "</input><br>";
document.getElementById('printers').innerHTML += radio;
}
function print() {
var currentPrinter= null;
var radios = document.getElementsByTagName('input');
for (var i = 0; i < radios.length; ++i) {
if (radios[i].checked) {
console.log(radios[i].value);
currentPrinter =radios[i].value;
}
}
var template = ' \
<FlowDocument PagePadding="0" \
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> \
<Paragraph TextAlignment="Center">Print Test</Paragraph> \
</FlowDocument> \
';
Internal.print(currentPrinter,template);
}
</script>

</html>
7 changes: 7 additions & 0 deletions PrintBrowser/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="cef.redist.x64" version="3.2785.1486" targetFramework="net452" />
<package id="cef.redist.x86" version="3.2785.1486" targetFramework="net452" />
<package id="CefSharp.Common" version="53.0.1" targetFramework="net452" />
<package id="CefSharp.Wpf" version="53.0.1" targetFramework="net452" />
</packages>
Loading

0 comments on commit 8813d77

Please sign in to comment.