public Stream Printers() { var printers = RawPrinterHelper.GetPrinters(); var jss = new JavaScriptSerializer(); string jsonClient = jss.Serialize(printers); WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; return(new MemoryStream(Encoding.UTF8.GetBytes(jsonClient))); }
private bool IsExistingPrinter(string printer) { var printers = RawPrinterHelper.GetPrinters(); bool response = false; foreach (Dictionary <string, object> p in printers) { if (p["Name"].ToString() == printer) { response = true; break; } } return(response); }
public Stream Print(System.IO.Stream pStream) { var jss = new JavaScriptSerializer(); string value = ""; string response = ""; string dataType = "RAW"; string defaultDataType = "RAW"; string printer = ""; string name = ""; string data = ""; using (var reader = new StreamReader(pStream, Encoding.UTF8)) { value = reader.ReadToEnd(); } var printData = jss.Deserialize <Dictionary <string, string> >(value); if (printData.ContainsKey("Printer") && printData.ContainsKey("Name") && printData.ContainsKey("Data")) { printer = printData["Printer"]; name = printData["Name"]; data = printData["Data"]; //Check for Printer Name validity and DataType default if (printData.ContainsKey("DataType")) { dataType = printData["DataType"].Trim(); } var printers = RawPrinterHelper.GetPrinters(); bool isValidPrinter = false; foreach (Dictionary <string, object> p in printers) { if (p["Name"].ToString() == printer) { defaultDataType = p["PrintJobDataType"].ToString(); isValidPrinter = true; break; } } if (isValidPrinter) { if (!IsValidDataType(dataType)) { dataType = defaultDataType; } bool successPrint = RawPrinterHelper.SendStringToPrinter(printer, name, data, dataType); if (successPrint) { response = "Print request sent to " + printer + " as " + dataType + " data."; } else { response = "An error occured during print request: " + Marshal.GetLastWin32Error(); } } else { response = printer + " is not found in the Cameleon Printer Service server."; } } else { response = "Print requests requires a 'Printer', 'Name' and 'Data' parameters."; } return(new MemoryStream(Encoding.UTF8.GetBytes(@"{""Response"" : " + @"""" + response + @"""}"))); }