Good afternoon, has anyone managed to use the SDK to print? I followed the examples in the documentation and managed to record the magnetic strip. I generated a bitmap with what I want to print on the front of the card. However, the printer does not print. The code returns OK.
I tested the printing on Evolis Premium Suite 2 and CardPress, and the printing works without problems on both.
Check the class that does the printing...
C# code:
using Evolis;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text; // Adicionado para Path.GetFullPath
namespace EvolisPrinter
{
public class CardImpre
{
public static bool ImpressorasOk()
{
var devices = Evolis.Evolis.GetDevices();
var selected = -1;
for (int i = 0; i < devices.Count; ++i)
{
if (selected == -1 || (!devices[selected].isOnline && devices[i].isOnline))
{
selected = i;
}
}
if (selected >= 0)
{
return devices[selected].isOnline;
}
return false;
}
public static string GeraVerso(string nome, string numero)
{
numero = AdicionarEspacos(numero);
// Defina o tamanho da imagem
int largura = 800;
int altura = 600;
// Crie um novo bitmap com o tamanho definido
Bitmap bitmap = new Bitmap(largura, altura);
// Crie um objeto Graphics para desenhar no bitmap
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// Defina a cor de fundo (branco)
graphics.Clear(Color.White);
// Defina a fonte e a cor do texto
Font fonte = new Font("Courier New", 42);
Font fonteNegrito = new Font("Courier New", 44, FontStyle.Bold);
Brush corTexto = new SolidBrush(Color.Black);
// Calcule a posição para o nome (alinhado à esquerda, acima do número)
SizeF tamanhoNome = graphics.MeasureString(numero, fonte);
float posicaoXNome = 25; // Alinhado à esquerda com margem de 20 pixels
float posicaoYNome = (altura * 3 / 4) - tamanhoNome.Height / 2; // Metade inferior
// Desenhe o nome no bitmap
graphics.DrawString(nome, fonte, corTexto, posicaoXNome, posicaoYNome);
// Calcule a posição para o número (metade inferior, alinhado à esquerda)
SizeF tamanhoNumero = graphics.MeasureString(nome, fonteNegrito);
float posicaoXNumero = 25; // Alinhado à esquerda com margem de 20 pixels
float posicaoYNumero = posicaoYNome - tamanhoNome.Height - 5; // Acima do número com espaçamento de 5 pixels
// Desenhe o número no bitmap
graphics.DrawString(numero, fonte, corTexto, posicaoXNumero, posicaoYNumero);
}
// Salve o bitmap como um arquivo .bmp
string caminhoArquivo = "imagem_gerada.bmp";
bitmap.Save(caminhoArquivo, ImageFormat.Bmp);
return Path.GetFullPath(caminhoArquivo);
}
public static string GeraFront(string nome, string numero)
{
numero = AdicionarEspacos(numero);
// Defina o tamanho da imagem
int largura = 800;
int altura = 600;
// Crie um novo bitmap com o tamanho definido
Bitmap bitmap = new Bitmap(largura, altura);
// Crie um objeto Graphics para desenhar no bitmap
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// Defina a cor de fundo (branco)
graphics.Clear(Color.White);
// Defina a fonte e a cor do texto
Font fonte = new Font("Arial Black", 42);
Font fonteNegrito = new Font("Arial Black", 46, FontStyle.Bold);
Brush corTexto = new SolidBrush(Color.Black);
// Calcule a posição para o nome (alinhado à esquerda, acima do número)
SizeF tamanhoNome = graphics.MeasureString(numero, fonte);
float posicaoXNome = 25; // Alinhado à esquerda com margem de 20 pixels
float posicaoYNome = (altura * 3 / 4) - tamanhoNome.Height / 2; // Metade inferior
// Desenhe o nome no bitmap
graphics.DrawString(nome, fonte, corTexto, posicaoXNome, posicaoYNome);
// Calcule a posição para o número (metade inferior, alinhado à esquerda)
SizeF tamanhoNumero = graphics.MeasureString(nome, fonteNegrito);
float posicaoXNumero = 25; // Alinhado à esquerda com margem de 20 pixels
float posicaoYNumero = posicaoYNome - tamanhoNome.Height - 5; // Acima do número com espaçamento de 5 pixels
// Desenhe o número no bitmap
graphics.DrawString(numero, fonte, corTexto, posicaoXNumero, posicaoYNumero);
}
// Salve o bitmap como um arquivo .bmp
string caminhoArquivo = "imagem_gerada.bmp";
bitmap.Save(caminhoArquivo, ImageFormat.Bmp);
return Path.GetFullPath(caminhoArquivo);
}
public static bool ImprimeCartoes(List<Card> cartores)
{
if (!cartores.Any()) return false;
var devices = Evolis.Evolis.GetDevices();
if (devices.Any())
{
Connection co = new Connection(devices.Find(x => x.link == ConverterStringParaLink("USB")).name);
if (co.GetInfo(out PrinterInfo pi))
{
Console.WriteLine("> Printer info:");
Console.WriteLine("- Name: {0}", pi.name);
Console.WriteLine("- Model: {0}", pi.model);
Console.WriteLine("- Serial number: {0}", pi.serialNumber);
Console.WriteLine("- Duplex printer: {0}", pi.hasFlip ? "yes" : "no");
Console.WriteLine("- ...");
}
else
{
Console.WriteLine("> Error: GetInfo() failed with error: {0}", co.GetLastError());
}
if (co.GetRibbonInfo(out RibbonInfo rii))
{
Console.WriteLine("> Ribbon info:");
Console.WriteLine("- Name: {0}", rii.description);
Console.WriteLine("- Type: {0}", rii.type);
Console.WriteLine("- Remaining capacity: {0} over {0}", rii.remaining, rii.capacity);
Console.WriteLine("- ...");
}
else
{
Console.WriteLine("> Error: GetRibbonInfo() failed with error: {0}", co.GetLastError());
}
if (co.GetCleaningInfo(out CleaningInfo ci))
{
Console.WriteLine("> Cleaning info:");
Console.WriteLine("- Number of card before next cleaning: {0}", ci.cardCountBeforeWarning);
Console.WriteLine("- Print head under warranty: {0}", (ci.printHeadUnderWarranty) ? "yes" : "no");
Console.WriteLine("- Number of regular cleaning: {0}", ci.regularCleaningCount);
Console.WriteLine("- Number of advanced cleaning: {0}", ci.advancedCleaningCount);
Console.WriteLine("- ...");
}
else
{
Console.WriteLine("> Error: GetCleaningInfo() failed with error: {0}", co.GetLastError());
}
if (co.GetStatus(out Status status))
{
// Check, for example, if there are cards in the feeder:
if (status.IsOn(Status.Flag.WAR_FEEDER_EMPTY))
Console.WriteLine("> No cards in the feeder.");
// Check if printer cover is open:
if (status.IsOn(Status.Flag.WAR_COVER_OPEN))
Console.WriteLine("> Printer cover is open, please close it.");
}
else
{
Console.WriteLine("> Error: GetStatus() failed with error: {0}", co.GetLastError());
}
if (co.GetState(out State.MajorState mas, out State.MinorState mis))
{
if (mas == State.MajorState.READY)
Console.WriteLine("> Printer is READY.");
else
Console.WriteLine("> Printer state is {0}:{1}", mas, mis);
}
else
{
Console.WriteLine("> Error: GetState() failed with error: {0}", co.GetLastError());
}
State state;
if (!co.GetState(out state) || state.GetMajorState() != State.MajorState.READY)
{
Console.WriteLine("> Error: Printer {0} is not READY ! Current state: {1} / {2}.", devices.FirstOrDefault().name,
state.GetMajorState(), state.GetMinorState());
}
else
{
foreach (var card in cartores)
{
MagSession ms = new MagSession(ref co);
ReturnCode rc;
// Write magnetic tracks:
ms.SetCoercivity(MagCoercivity.AUTO);
if (card.Tracks is not null)
{
if (card.Tracks.Length <= 3)
{
foreach (var track in card.Tracks)
{
ms.SetTrack(0, MagFormat.ISO1, track);
}
}
}
if ((rc = ms.Write()) == ReturnCode.OK)
{
Console.WriteLine("> Write done.");
}
else
{
Console.WriteLine("> Write error: {0}.", rc);
}
if (co.GetRibbonInfo(out RibbonInfo ri))
{
PrintSession ps = new PrintSession(ref co, ConverterStringParaRibbonType(ri.type.ToString()));
string front = GeraFront(card.NomeFrente, card.NumeroFrente);
ps.SetImage(CardFace.FRONT, front);
//here the impression occurs
ReturnCode r = ps.Print();
if (r != ReturnCode.OK)
{
Console.WriteLine("> Error: Print failed");
if (!co.GetState(out state))
{
Console.WriteLine("> Error: failed to get state");
}
else
{
Console.WriteLine("> State: major={0}, minor={1}", state.GetMajorState(), state.GetMinorState());
}
}
}
}
}
}
return true;
}
public static RibbonType ConverterStringParaRibbonType(string riTypeString)
{
RibbonType ribbonType;
if (Enum.TryParse(riTypeString, true, out ribbonType))
{
return ribbonType;
}
else
{
return RibbonType.UNKNOWN;
}
}
public static Link ConverterStringParaLink(string riLinkString)
{
Link link;
if (Enum.TryParse(riLinkString, true, out link))
{
return link;
}
else
{
return Link.INVALID;
}
}
private static string AdicionarEspacos(string numero)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numero.Length; i++)
{
if (i > 0 && i % 4 == 0)
{
sb.Append(" ");
}
sb.Append(numero[i]);
}
return sb.ToString();
}
public static void ShowActiveFlags(Status status)
{
Console.WriteLine("> Active flags: " + status.ToString());
Console.WriteLine("> Flags on:");
foreach (Evolis.Status.Flag v in Enum.GetValues(typeof(Evolis.Status.Flag)))
{
if (status.IsOn(v))
Console.WriteLine(" {0}", v);
}
}
}
}