using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; using System.Diagnostics; using Microsoft.Win32; namespace imprime { public partial class Form1 : Form { private string fileName; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Properties. //LoadEstados(); if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.url)) url.Text = imprimeFacturas.Properties.Configuraciones.Default.url; if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.idInicio)) desde.Text = imprimeFacturas.Properties.Configuraciones.Default.idInicio; if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.idFin)) hasta.Text = imprimeFacturas.Properties.Configuraciones.Default.idFin; if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe)) ruta.Text = imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe; if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.accionDescarga)) comboBox1.SelectedIndex = Int32.Parse(imprimeFacturas.Properties.Configuraciones.Default.accionDescarga); comenzarTimer(); muestro(); } private void Form1_Resize(object sender, EventArgs e) { if (FormWindowState.Minimized == this.WindowState) { notifyIcon1.Visible = true; notifyIcon1.BalloonTipText = "La aplicacion sigue en abierta"; notifyIcon1.BalloonTipTitle = "Atencion"; //notifyIcon1.Ba notifyIcon1.ShowBalloonTip(500); this.Hide(); } else if (FormWindowState.Normal == this.WindowState) { notifyIcon1.Visible = false; } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.F1) { MessageBox.Show("v 1.1"); return true; // indicate that you handled this keystroke } // Call the base class return base.ProcessCmdKey(ref msg, keyData); } private void button1_Click(object sender, EventArgs e) { imprimeFacturas.Properties.Configuraciones.Default.url = url.Text ; imprimeFacturas.Properties.Configuraciones.Default.idInicio = desde.Text; imprimeFacturas.Properties.Configuraciones.Default.idFin = hasta.Text; imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString(); imprimeFacturas.Properties.Configuraciones.Default.Save(); int inicio = int.Parse(desde.Text); int fin = int.Parse(hasta.Text); for (int i = inicio; i <= fin; i++) { string remoteUri = url.Text + "?action=dameFactura&idPedido=" + i; string myStringWebResource = null; fileName = "pdf_" + i + ".pdf"; WebClient myWebClient = new WebClient(); myStringWebResource = remoteUri + fileName; Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); myWebClient.DownloadFile(myStringWebResource, fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource); Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath); imprimirPdf(fileName); } } protected void imprimirPdf(string fileName) { try { long length = new System.IO.FileInfo(fileName).Length; string nomFile = Application.StartupPath + "\\" + fileName; if (length == 0) { System.IO.File.Delete(nomFile); File.Delete(nomFile); logAcciones.Text = logAcciones.Text + "Descarga incorrecta: " + fileName + " en ......." + Application.StartupPath + "\n"; } else { logAcciones.Text = logAcciones.Text + "Descarga correcta: " + fileName + " en ......." + Application.StartupPath + "\n"; int opcion = comboBox1.SelectedIndex; if (opcion == 0) { Process proc = new Process(); proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.StartInfo.Verb = "print"; String ruta = @""; //Define location of adobe reader/command line //switches to launch adobe in "print" mode proc.StartInfo.FileName = ruta; proc.StartInfo.Arguments = String.Format(@"/p /h /s {0}", nomFile); proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = true; proc.Start(); proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; if (proc.HasExited == false) { proc.WaitForExit(10000); } proc.EnableRaisingEvents = true; proc.Close(); KillAdobe("AcroRd32"); logAcciones.Text = logAcciones.Text + "Documento Impreso: " + fileName ; notifyIcon1.BalloonTipText = "Fichero" + fileName + " Impreso"; notifyIcon1.BalloonTipTitle = "Atencion"; notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(100); } } } catch (Exception fallo) { //hacerLog.escribirLog("fallido", "imprimirPdf", archivo, "error:", fallo.Message, ""); MessageBox.Show("No se ha podid imprimir el archivo: " + fileName + " con error:" + fallo.Message); } } protected bool KillAdobe(string name) { foreach (Process clsProcess in Process.GetProcesses().Where( clsProcess => clsProcess.ProcessName.StartsWith(name))) { clsProcess.Kill(); return true; } return false; } private void comenzarTimer() { int opcion = comboBox1.SelectedIndex; Console.WriteLine("Viendo timer" + opcion); if (opcion == 2) { Console.WriteLine("Activo timer" + opcion); timer1.Enabled = true; timer1.Start(); Console.WriteLine("enciendo Timer"); } else { Console.WriteLine("Activo timer" + opcion); timer1.Stop(); timer1.Enabled = false; Console.WriteLine("Paro Timer"); } } private void LoadEstados() { /* var request = (HttpWebRequest)WebRequest.Create("http://obi.dropwp.com/modules/comunicacionesbyimax/comunicacionesbyimaxAjax.php?action=getEstados"); var response = (HttpWebResponse)request.GetResponse(); string content = string.Empty; using (var stream = response.GetResponseStream()) { using (var sr = new StreamReader(stream)) { content = sr.ReadToEnd(); List o = JsonConvert.DeserializeObject>(content); foreach (var objeto in o) { if(objeto.invoice == "1") { ComboboxItem item = new ComboboxItem(); item.Text = objeto.name; item.Value = objeto.id_order_state; comboBox1.Items.Add(item); } } comboBox1.SelectedIndex = 0; //MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString()); } }*/ } private void button2_Click(object sender, EventArgs e) { imprimeFacturas.Properties.Configuraciones.Default.url = url.Text; imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe = ruta.Text; imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString(); imprimeFacturas.Properties.Configuraciones.Default.Save(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString(); imprimeFacturas.Properties.Configuraciones.Default.Save(); comenzarTimer(); muestro(); } private void muestro() { int opcion = comboBox1.SelectedIndex; Console.WriteLine("Viendo timer" + opcion); if (opcion == 2) { label1.Hide(); label2.Hide(); desde.Hide(); hasta.Hide(); } else { label1.Show(); label2.Show(); desde.Show(); hasta.Show(); } } private void timer1_Tick(object sender, EventArgs e) { string urlTexto = url.Text; if (!String.IsNullOrEmpty(urlTexto)) { string remoteUri = url.Text + "?action=dameFacturasPP&down=0"; string i = "0"; using (WebClient wc = new System.Net.WebClient()) { var json = wc.DownloadString(remoteUri); IPdf pdf = JsonConvert.DeserializeObject(json); i = pdf.idPedido.ToString(); } if (i != "false") { remoteUri = url.Text + "?action=dameFacturasPP&down=1"; string myStringWebResource = null; fileName = "pdf_" + i + ".pdf"; WebClient myWebClient = new WebClient(); myStringWebResource = remoteUri + fileName; Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); myWebClient.DownloadFile(myStringWebResource, fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource); Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath); imprimirPdf(fileName); notifyIcon1.BalloonTipText = "Fichero" + fileName + " Guardado"; notifyIcon1.BalloonTipTitle = "Atencion"; notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(100); this.Hide(); } } else { MessageBox.Show("Tiene que existir la url"); } } private void autorun_CheckedChanged(object sender, EventArgs e) { if (autorun.Checked) { RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (!IsStartupItem()) // Add the value in the registry so that the application runs at startup rkApp.SetValue("My app's name", Application.ExecutablePath.ToString()); Console.WriteLine("Regisrando APP en Inicio" ); } else { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (IsStartupItem()) // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("My app's name", false); Console.WriteLine("Borrando APP en Inicio"); } } private bool IsStartupItem() { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rkApp.GetValue("My app's name") == null) // The value doesn't exist, the application is not set to run at startup return false; else // The value exists, the application is set to run at startup return true; } private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { } private void menuAbrir_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Normal; } private void menuSalir_Click(object sender, EventArgs e) { System.Windows.Forms.Application.Exit(); } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.WindowState = FormWindowState.Normal; } } public class ComboboxItem { public string Text { get; set; } public object Value { get; set; } public override string ToString() { return Text; } } public class IPdf { public string idPedido { get; set; } } }