Skip to content

Commit c17720a

Browse files
Merge pull request #355 from x-xx-o/print
added print capability
2 parents 274552f + a3c452e commit c17720a

File tree

6 files changed

+213
-13
lines changed

6 files changed

+213
-13
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Print dpi
5+
/// </summary>
6+
public class PrintDpi
7+
{
8+
/// <summary>
9+
/// The horizontal dpi
10+
/// </summary>
11+
public float Horizontal { get; set; }
12+
13+
/// <summary>
14+
/// The vertical dpi
15+
/// </summary>
16+
public float Vertical { get; set; }
17+
}
18+
19+
/// <summary>
20+
/// The page range to print
21+
/// </summary>
22+
public class PrintPageRange
23+
{
24+
/// <summary>
25+
/// From
26+
/// </summary>
27+
public int From { get; set; }
28+
29+
/// <summary>
30+
/// To
31+
/// </summary>
32+
public int To { get; set; }
33+
}
34+
35+
/// <summary>
36+
/// Print options
37+
/// </summary>
38+
public class PrintOptions
39+
{
40+
/// <summary>
41+
/// Don't ask user for print settings
42+
/// </summary>
43+
public bool Silent { get; set; }
44+
45+
/// <summary>
46+
/// Prints the background color and image of the web page
47+
/// </summary>
48+
public bool PrintBackground { get; set; }
49+
50+
/// <summary>
51+
/// Set the printer device name to use
52+
/// </summary>
53+
public string DeviceName { get; set; }
54+
55+
/// <summary>
56+
/// Set whether the printed web page will be in color or grayscale
57+
/// </summary>
58+
public bool Color { get; set; }
59+
60+
/// <summary>
61+
/// Specifies the type of margins to use. Uses 0 for default margin, 1 for no
62+
/// margin, and 2 for minimum margin.
63+
/// </summary>
64+
public int MarginsType { get; set; }
65+
66+
/// <summary>
67+
/// true for landscape, false for portrait.
68+
/// </summary>
69+
public bool Landscape { get; set; }
70+
71+
/// <summary>
72+
/// The scale factor of the web page
73+
/// </summary>
74+
public float ScaleFactor { get; set; }
75+
76+
/// <summary>
77+
/// The number of pages to print per page sheet
78+
/// </summary>
79+
public int PagesPerSheet { get; set; }
80+
81+
/// <summary>
82+
/// The number of copies of the web page to print
83+
/// </summary>
84+
public bool Copies { get; set; }
85+
86+
/// <summary>
87+
/// Whether the web page should be collated
88+
/// </summary>
89+
public bool Collate { get; set; }
90+
91+
/// <summary>
92+
/// The page range to print
93+
/// </summary>
94+
public PrintPageRange PageRanges { get; set; }
95+
96+
/// <summary>
97+
/// Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.
98+
/// </summary>
99+
public string DuplexMode { get; set; }
100+
101+
/// <summary>
102+
/// Dpi
103+
/// </summary>
104+
public PrintDpi Dpi { get; set; }
105+
106+
}
107+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Printer info
5+
/// </summary>
6+
public class PrinterInfo
7+
{
8+
/// <summary>
9+
/// Name
10+
/// </summary>
11+
public string Name { get; set; }
12+
13+
/// <summary>
14+
/// Name
15+
/// </summary>
16+
public string Description { get; set; }
17+
18+
/// <summary>
19+
/// Status
20+
/// </summary>
21+
public int Status { get; set; }
22+
23+
/// <summary>
24+
/// Is default
25+
/// </summary>
26+
public bool IsDefault { get; set; }
27+
28+
}
29+
}

ElectronNET.API/WebContents.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,53 @@ public void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)
107107
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id, JObject.FromObject(openDevToolsOptions, _jsonSerializer));
108108
}
109109

110+
/// <summary>
111+
/// Get system printers.
112+
/// </summary>
113+
/// <returns>printers</returns>
114+
public Task<PrinterInfo[]> GetPrintersAsync()
115+
{
116+
var taskCompletionSource = new TaskCompletionSource<PrinterInfo[]>();
117+
118+
BridgeConnector.Socket.On("webContents-getPrinters-completed", (printers) =>
119+
{
120+
BridgeConnector.Socket.Off("webContents-getPrinters-completed");
121+
122+
taskCompletionSource.SetResult(((Newtonsoft.Json.Linq.JArray)printers).ToObject<PrinterInfo[]>());
123+
});
124+
125+
BridgeConnector.Socket.Emit("webContents-getPrinters", Id);
126+
127+
return taskCompletionSource.Task;
128+
}
129+
130+
/// <summary>
131+
/// Prints window's web page.
132+
/// </summary>
133+
/// <param name="options"></param>
134+
/// <returns>success</returns>
135+
public Task<bool> PrintAsync(PrintOptions options = null)
136+
{
137+
var taskCompletionSource = new TaskCompletionSource<bool>();
138+
139+
BridgeConnector.Socket.On("webContents-print-completed", (success) =>
140+
{
141+
BridgeConnector.Socket.Off("webContents-print-completed");
142+
taskCompletionSource.SetResult((bool)success);
143+
});
144+
145+
if(options == null)
146+
{
147+
BridgeConnector.Socket.Emit("webContents-print", Id, "");
148+
}
149+
else
150+
{
151+
BridgeConnector.Socket.Emit("webContents-print", Id, JObject.FromObject(options, _jsonSerializer));
152+
}
153+
154+
return taskCompletionSource.Task;
155+
}
156+
110157
/// <summary>
111158
/// Prints window's web page as PDF with Chromium's preview printing custom
112159
/// settings.The landscape will be ignored if @page CSS at-rule is used in the web page.

ElectronNET.Host/api/webContents.js

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)