Bonjour à tous,
J’ai besoin d’aide pour réussir à afficher du texte dans un PrintPreviewControl comme expliqué dans le titre.
Voici la composition du form1 (RichTextBox1, button1, printDialog1 et printDocument1) :
Voici la composition du form2 (PrintPreviewControl) :
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 using System; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; namespace Impression_Richebox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void printDocument1_BeginPrint(object sender, PrintEventArgs e) { char[] param = { '\n' }; if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection) { lines = richTextBox1.SelectedText.Split(param); } else { lines = richTextBox1.Text.Split(param); } int i = 0; char[] trimParam = { '\r' }; foreach (string s in lines) { lines[i++] = s.TrimEnd(trimParam); } } public int linesPrinted; public string[] lines; public void OnPrintPage(object sender, PrintPageEventArgs e) { int x = e.MarginBounds.Left; int y = e.MarginBounds.Top; Brush brush = new SolidBrush(richTextBox1.ForeColor); while (linesPrinted < lines.Length) { e.Graphics.DrawString(lines[linesPrinted++], richTextBox1.Font, brush, x, y); y += 15; if (y >= e.MarginBounds.Bottom) { e.HasMorePages = true; return; } } linesPrinted = 0; e.HasMorePages = false; } private void button1_Click_1(object sender, EventArgs e) { Form2 Form2 = new Form2(); Form2.Show(); } } }
Voici le message d'erreur :
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 using System; using System.Drawing.Printing; using System.Windows.Forms; namespace Impression_Richebox { public partial class Form2 : Form { Form1 Form1 = new Form1(); public Form2() { InitializeComponent(); } public PrintDocument printDocument1 = new PrintDocument(); public void CreatePrintPreviewControl() { ControleApercu_Av_Impression.Document = printDocument1; ControleApercu_Av_Impression.Zoom = 0.75; ControleApercu_Av_Impression.UseAntiAlias = true; // Ajoute PrintPreviewControl au formulaire Controls.Add(ControleApercu_Av_Impression); // Ajoute un gestionnaire d'événements PrintDocument PrintPage printDocument1.PrintPage += new PrintPageEventHandler(Form1.OnPrintPage); } private void Form2_Load(object sender, EventArgs e) { CreatePrintPreviewControl(); } } }
Voici le message d'erreur :
Ce code issu du web fonctionne avec une zone de texte, si j'écris ce type de code :
J'arrive bien à visualiser avant d'imprimer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 Font police = new Font("Arial", 12); TextBox1.Multiline = true; string[] arr = new string[6]; arr[0] = "000000"; arr[1] = "111111"; arr[2] = "22222"; arr[3] = "3333333"; TextBox1.Lines = arr; e.Graphics.DrawString(TextBox1.Text, police, Brushes.Black, 20, 25); // Horizontal / Vertical
Qui peut m'aider pour ma richtextbox1 ?
Merci à vous
Partager