diff --git a/AIParkingApplication/AIParkingApplication.csproj b/AIParkingApplication/AIParkingApplication.csproj index 764d710..52cf459 100644 --- a/AIParkingApplication/AIParkingApplication.csproj +++ b/AIParkingApplication/AIParkingApplication.csproj @@ -141,6 +141,7 @@ + @@ -223,6 +224,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AIParkingApplication/Printer.cs b/AIParkingApplication/Printer.cs new file mode 100644 index 0000000..4bc2b94 --- /dev/null +++ b/AIParkingApplication/Printer.cs @@ -0,0 +1,72 @@ +using Microsoft.Win32; +using System; +using System.IO; +using System.Windows.Forms; + +namespace AIParkingApplication +{ + public class Printer + { + private WebBrowser webBrowser; + public Printer() + { + webBrowser = new WebBrowser(); + SetupPrinterPageSetting(); + } + + public void DoPrint(PrinterData printData) + { + webBrowser.Left = 0; + string documentText = ProcessingString("PrinterForm.html", printData); + if (!string.IsNullOrEmpty(documentText)) + { + webBrowser.DocumentText = documentText; + webBrowser.DocumentCompleted += WebBrowser_DocumentCompleted; + } + } + + private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) + { + ((WebBrowser)sender).Print(); + } + + private string ProcessingString(string path, PrinterData printerField) + { + try + { + string htmlStr = File.ReadAllText(path); + htmlStr = htmlStr.Replace("{PLATE_STRING}", printerField.PlateString) + .Replace("{TIME_PARKING_IN}", printerField.TimeParkingIn) + .Replace("{TIME_PARKING_OUT}", printerField.TimeParkingOut) + .Replace("{MONEY_AMOUNT}", printerField.MoneyAmount.ToString()); + return htmlStr; + } + catch (Exception ex) + { + Console.WriteLine($"ProcessingString\texMessage:{ex.Message}"); + return string.Empty; + } + } + + private void SetupPrinterPageSetting() + { + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\PageSetup", RegistryKeyPermissionCheck.ReadWriteSubTree); + + key.SetValue("footer", string.Empty, RegistryValueKind.String); + key.SetValue("header", string.Empty, RegistryValueKind.String); + key.SetValue("margin_top", 0); + key.SetValue("margin_right", 0.2); + key.SetValue("margin_bottom", 0.5); + key.SetValue("margin_left", 0.2); + key.Close(); + } + } + + public class PrinterData + { + public string PlateString { get; set; } + public string TimeParkingIn { get; set; } + public string TimeParkingOut { get; set; } + public int MoneyAmount { get; set; } + } +} diff --git a/AIParkingApplication/printerForm.html b/AIParkingApplication/printerForm.html new file mode 100644 index 0000000..f4a1694 --- /dev/null +++ b/AIParkingApplication/printerForm.html @@ -0,0 +1,31 @@ + + + + + + + Document + + + + +

BỆNH VIỆN BẠCH MAI

+

Biên lai thu tiền

+

Biển số xe: {PLATE_STRING}

+

Thời gian vào: {TIME_PARKING_IN}

+

Thời gian ra: {TIME_PARKING_OUT}

+

Số tiền: {MONEY_AMOUNT}

+ + + \ No newline at end of file