Comment imprime un etat crystal report en utilisant les sockets

Résolu
PascalCmoa Messages postés 239 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 17 janvier 2013 - 6 oct. 2011 à 14:59
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 - 24 avril 2019 à 13:28
Je suis en train de mettre en place les gestions d'impression d'états Crystal Report en mettant au point une classe.
Les imprimantes sont toutes sur IP fixe.
Pour imprimer les états je passe par:
...
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(Environment.CurrentDirectory + "\\Test.rpt");
cryRpt.PrintOptions.PrinterName = GetBadgePrinter();
cryRpt.PrintToPrinter(1, false, 0, 0);
cryRpt.Close();
...


Pour trouver mon imprimante par son adresse IP j'ai cette méthode:
private string GetBadgePrinter()
{
  String result = "";
  ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * from Win32_Printer");
  ManagementObjectCollection coll = search.Get();
  foreach (ManagementObject obj in coll)
     {
String myIP = "192.168.1.55";
string portName = obj["PortName"].ToString();
if (portName.Contains(myIP))
{
result = obj["Name"].ToString();
return result;
}
    }
    return result;
}


Ce code fonctionne bien, mais il faut que je passe par les adresses IP pour faire mes impressions. Malheureusement PrintOptions.PrinterName n'accepte que les String. Je pensais donc passer par un socket pour envoyer mon impression d'état CR, mais je n'obtient que des codes ascii sur 20 pages (au moins).
J'ai utilisée les méthode suivante pour réaliser cette action non concluante:
private void button4_Click(object sender, EventArgs e)
{
String Chemin = Environment.CurrentDirectory + "\";
String Fichier = "Test.rpt";
String IP = "192.168.1.55";
String[] IPs = IP.Split('.');
Byte[] bIP = new Byte[] { Byte.Parse(IPs[0]), Byte.Parse(IPs[1]), Byte.Parse(IPs[2]), Byte.Parse(IPs[3]) };

ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(Chemin + Fichier);
imprimer.Imprimer(bIP, 9100, Chemin, Fichier);
cryRpt.Close();
}

public static Boolean Imprimer(Byte[] IP, Int32 Port, String Chemin,String Fichier)
{
Byte[] buffer = ReadBinaryFile(Chemin, Fichier);
Boolean retour = true;
BufferedStream bufStream = default(BufferedStream);
try
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(new IPAddress(IP), Port));
NetworkStream netStream = new NetworkStream(clientSocket, true);
bufStream = new BufferedStream(netStream);
if (bufStream.CanWrite)
{
bufStream.Write(buffer, 0, buffer.Length);
bufStream.Flush();
}
}
catch (Exception ex)
{
retour = false;
}
finally
{
if (bufStream != null) bufStream.Close();
}
return retour;
}

private static Byte[] ReadBinaryFile(String path, String fileName)
{
if (File.Exists(path + fileName))
{
try
{
///Open and read a file。
FileStream fileStream = File.OpenRead(path + fileName);
return ConvertStreamToByteBuffer(fileStream);
}
catch (Exception ex)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}

private static Byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}


Je ne vois pas ou j'ai fait une erreur. J'envoi bien à mon imprimante un flux à imprimer mais la sortie n'est pas celle que j'attends.

Si quelqu'un peu m'aider, ce serai super.

Merci

PascalCmoa
email: PascalCmoa

3 réponses

PascalCmoa Messages postés 239 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 17 janvier 2013 5
14 oct. 2011 à 12:39
Merci pour les réponses


PascalCmoa
email: PascalCmoa
1
salut le monde
je n'arrive pas à trouver la connection de base de donnée xampp par la dataset pour une impression
0
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
24 avril 2019 à 13:28
Bonjour
Au lieu de te greffer sur une discussion vieille de 8 ans crée ta propre discussion
Merci
0
Rejoignez-nous