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:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
...
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:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
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:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
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