Mostrando entradas con la etiqueta COM. Mostrar todas las entradas
Mostrando entradas con la etiqueta COM. Mostrar todas las entradas

Invocar metodos de un objeto COM desde una página web con javascript - con ejemplo de impresion (call simple ActiveX object installed on the client)

Para compilarlo:

Ir a prompt de visual studio 2013 (abrirlo como administrador)

Si no hay una strong key, generarla con:
      sn -k keyImpresora.snk

cls
call "%VS120COMNTOOLS%..\Tools\vsvars32.bat"
csc /t:library Impresora.cs /keyfile:keyImpresora.snk
regasm Impresora.dll /tlb:ImpresoraNet.dll /codebase


Impresora.cs

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections.Generic;

namespace X
{
public enum Estilo
{
Normal,
Bold,
Enlarged,
Condensed
}
public interface IImpresora
{
string Imprimir(string texto, string estilo);
void FormFeed();
void Log(string texto);
string Inicializar(string usuario);
void Agregar(string linea);
void Listar();
}

public class Linea
{
public string Texto {get; set;}
public string Estilo {get; set;}
}

[ClassInterface(ClassInterfaceType.AutoDual)]
public class Impresora : IImpresora
{
bool _fin = false;
string _pathImpresora = "";
List<Linea> lineas;

public void Agregar(string linea)
{
var l = new Linea();

l.Texto = linea;

lineas.Add(l);
}

public void Listar()
{
var estilo = "normal";
var textoOk = "";

File.Delete("c:\\temp\\outLog.txt");

foreach (var l in lineas)
{
var xx = "";

xx = l.Texto;

xx = xx.Replace((char)13 + "" + (char)10, "");

if (xx.Contains(Convert.ToChar(27) + "(s3B") || xx.Contains(Convert.ToChar(27) + "(s0B"))
{
//evito estas lineas
} else
{
// var xx = l.Texto.Replace( Convert.ToChar(27) + "(s3B", "");
// xx = xx.Replace( Convert.ToChar(27) + "(s0B", "");
xx = xx.Replace( Convert.ToChar(27) + "E" + "", (char)27 + "" + (char)69 + "");
}

Log(xx);
}

EjecutarComando("type c:\\temp\\outLog.txt > " + _pathImpresora);
/*
foreach (var l in lineas)
{
textoOk = "";

for (int i=0; i<l.Texto.Length; i++)
{
if ((int)l.Texto[i] == 12)
{
textoOk += "[FORM FEED]";
i++;
}

if ((int)l.Texto[i] == 27)
{
i++;

if ((int)l.Texto[i] == 69)
{
estilo = "bold";
i++;
} else if ((int)l.Texto[i] == 70)
{
estilo = "normal";
i++;
}
}

textoOk += l.Texto[i];
}

//Log(textoOk + " - " + estilo );
}
*/
}

public string Imprimir(string texto, string estilo)
{
if (estilo == "normal")
ImprimirLinea(texto, Estilo.Normal);

if (estilo == "bold")
ImprimirLinea(texto, Estilo.Bold);

if (estilo == "enlarged")
ImprimirLinea(texto, Estilo.Enlarged);

if (estilo == "condensed")
ImprimirLinea(texto, Estilo.Condensed);

return texto;
}

public void FormFeed()
{
//para que funcione el form feed hay que poner algun caracter antes y despues (en este caso, el signo de menos)
EjecutarComando("echo -" + (char)12 + "- > " + _pathImpresora);
}

public string Inicializar(string usuario)
{
var archivo = @"c:\X_imp\" + usuario + @"\framePrinter.properties";
var host = "";
var nombreImp = "";


if (File.Exists(archivo))
{
var f = new StreamReader(archivo);
while (!f.EndOfStream)
{
var linea = f.ReadLine();

if (linea.Contains("frameConnectorService.PrinterDriver.drivers"))
{
var v = linea.Split('=');

host = v[1];
host = host.Substring(0, host.IndexOf(","));

}

if (host != "")
{
if (linea.IndexOf("frameConnectorService.PrinterDriver." + host + ".port", StringComparison.OrdinalIgnoreCase) >= 0)
{
var v = linea.Split('=');
nombreImp = v[1].Replace("\\", "");
}
}
}
} else
Log("NO EXISTE ARCHIVO DE CONFIG");

var ret = "";

if (host != "" && nombreImp != "")
{
_pathImpresora = @"\\" + host + @"\" + nombreImp;
ret = "OK";
}
else
{
_pathImpresora = "";
ret = "ERROR";
}

if (ret == "OK")
lineas = new List<Linea>();

return ret;
}

private void ImprimirLinea(string texto, Estilo estilo)
{

//(char)27 + (char)15 ---------------- condensado on
//(char)18 --------------------------- condensado off
//(char)27 + (char)87 + (char)49 ----- enlarged on
//(char)27 + (char)87 + (char)48 ----- enlarged off
//(char)27 + (char)69 ---------------- bold on
//(char)27 + (char)70 ---------------- bold off


if (texto.Trim() == "")
{
texto = "(";
estilo = Estilo.Normal;
}
else
texto = " " + texto;

switch (estilo)
{
case Estilo.Normal:
EjecutarComando("echo" + texto + " > " + _pathImpresora);
break;
case Estilo.Bold:
EjecutarComando("echo " + (char)27 + (char)69 + texto + (char)27 + (char)70 + " > " + _pathImpresora);
break;
case Estilo.Enlarged:
EjecutarComando("echo " + (char)27 + (char)87 + (char)49 + texto + (char)27 + (char)87 + (char)48 + " > " + _pathImpresora);
break;
case Estilo.Condensed:
EjecutarComando("echo " + (char)27 + (char)15 + texto + (char)18 + " > " + _pathImpresora);
break;
}
}

private void EjecutarComando(string comando) {
ProcessStartInfo processInfo;
Process proc;
_fin = false;
processInfo = new ProcessStartInfo("cmd.exe", (Convert.ToString("/c ") + comando));
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.Verb = "runas";
proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo = processInfo;
proc.OutputDataReceived += OnOutputDataReceived;
proc.Exited += OnProcessExited;
proc.Start();
proc.BeginOutputReadLine();
while (!_fin) {
Thread.Sleep(1);
}

proc.Close();
}
 
private void OnProcessExited(object sender, EventArgs e) {
_fin = true;
}

private void OnOutputDataReceived(object sender, DataReceivedEventArgs e) {
}

public void Log(string texto)
{
var f = new StreamWriter("c:\\temp\\outLog.txt", true);

f.WriteLine(texto);

f.Close();
}
}
}


Version anterior:

Impresora.cs

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace X

{
public enum Estilo
{
Normal,
Bold,
Enlarged,
Condensed
}
public interface IImpresora
{
string Imprimir(string texto, string estilo);
void FormFeed();
void Log(string texto);
}

[ClassInterface(ClassInterfaceType.AutoDual)]

public class Impresora : IImpresora
{
bool _fin = false;

public string Imprimir(string texto, string estilo)
{
if (estilo == "normal")
ImprimirLinea(texto, Estilo.Normal);

if (estilo == "bold")

ImprimirLinea(texto, Estilo.Bold);

if (estilo == "enlarged")
ImprimirLinea(texto, Estilo.Enlarged);

if (estilo == "condensed")
ImprimirLinea(texto, Estilo.Condensed);

return texto;
}

public void FormFeed()
{
EjecutarComando("echo " + "ABC" + (char)12 + "ABC" + " > \\\\X00762\\EPSON1");
}

private void ImprimirLinea(string texto, Estilo estilo)
{

//(char)27 + (char)15 ---------------- condensado on
//(char)18 --------------------------- condensado off
//(char)27 + (char)87 + (char)49 ----- enlarged on
//(char)27 + (char)87 + (char)48 ----- enlarged off
//(char)27 + (char)69 ---------------- bold on
//(char)27 + (char)70 ---------------- bold off


if (texto.Trim() == "")
{
texto = "(";
estilo = Estilo.Normal;
}
else
texto = " " + texto;

switch (estilo)
{
case Estilo.Normal: 
EjecutarComando("echo" + texto + " > \\\\X00762\\EPSON1");
break;
case Estilo.Bold: 
EjecutarComando("echo " + (char)27 + (char)69 + texto + (char)27 + (char)70 + " > \\\\X00762\\EPSON1");
break;
case Estilo.Enlarged:
EjecutarComando("echo " + (char)27 + (char)87 + (char)49 + texto + (char)27 + (char)87 + (char)48 + " > \\\\X00762\\EPSON1");
break;
case Estilo.Condensed:
EjecutarComando("echo " + (char)27 + (char)15 + texto + (char)18 + " > \\\\X00762\\EPSON1");
break;
}
}

private void EjecutarComando(string comando) {
ProcessStartInfo processInfo;
Process proc;
_fin = false;
processInfo = new ProcessStartInfo("cmd.exe", (Convert.ToString("/c ") + comando));
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.Verb = "runas";
proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo = processInfo;
proc.OutputDataReceived += OnOutputDataReceived;
proc.Exited += OnProcessExited;
proc.Start();
proc.BeginOutputReadLine();
while (!_fin) {
Thread.Sleep(1);
}

proc.Close();
}
  
private void OnProcessExited(object sender, EventArgs e) {
_fin = true;
}

private void OnOutputDataReceived(object sender, DataReceivedEventArgs e) {
}

public void Log(string texto)
{
var f = new StreamWriter("c:\\temp\\outLog.txt", true);

if (texto.IndexOf('\f') >= 0)
f.WriteLine("FORM FEEED!!!!!!");
else
f.WriteLine(texto);

f.Close();
}
}
}

Tester.htm

<html>
<head>
<script language=JavaScript>
var imp = new ActiveXObject('X.Impresora');
alert(imp.Imprimir("Texto PRUEBA"));
</script>
</head>
 <body>
  <h1>Tester.htm</h1>
 </body>
</html>



Fuente: http://forums.asp.net/post/2204437.aspx