Bonjour,

J'ai écrit un programme de test pour imprimer des fichiers en affichant la fenêtre d'impression de manière à régler les paramètres d'impression.
Voilà le code complet de mon programme-test :

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
 
 
 
namespace PrintPreview
{
    public class frmPrincipal : Form
    {
        // Gestion des pages à imprimées
        private StreamReader streamToPrint;
        private System.Windows.Forms.Button btnImprimer;
                private Font printFont;
 
 
        public frmPrincipal()
        {
            InitializeComponent();
        }
 
 
 
private void btnImprimer_Click(object sender, EventArgs e)
        {
            try
            {
                //FileStream fs = new FileStream(@"C:\test1.txt", FileMode.Open, FileAccess.Read);
                //streamToPrint = new StreamReader(fs);
                streamToPrint = new StreamReader(@"C:\test1.txt");
                try
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    PrintDialog printDialog = new PrintDialog();
                    printDialog.AllowCurrentPage = true;
                    printDialog.AllowSelection = true;
                    printDialog.AllowSomePages = true;
                    printDialog.ShowNetwork = true;
                    if (printDialog.ShowDialog() == DialogResult.OK)
                    {
                        pd.PrinterSettings = printDialog.PrinterSettings;
                        pd.Print();
                    }
 
                }
                finally
                {
                    streamToPrint.Close();
                }
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void InitializeComponent()
        {
            this.btnImprimer = new Button();
            this.SuspendLayout();
            // 
            // btnImprimer
            // 
            this.btnImprimer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.btnImprimer.Location = new System.Drawing.Point(12, 12);
            this.btnImprimer.Name = "btnImprimer";
            this.btnImprimer.Size = new System.Drawing.Size(293, 58);
            this.btnImprimer.TabIndex = 0;
            this.btnImprimer.Text = "PrintPreview personnalisé";
            this.btnImprimer.UseVisualStyleBackColor = true;
            this.btnImprimer.Click += new System.EventHandler(this.btnImprimer_Click);
            // 
            // printDocument
            // 
 
            // 
            // frmPrincipal
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(317, 82);
            this.Controls.Add(this.btnImprimer);
            this.Name = "frmPrincipal";
            this.Text = "PrintPreview personnalisé";
            this.ResumeLayout(false);
 
        }
 
        /// <summary>
        /// Variable nécessaire au concepteur.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Nettoyage des ressources utilisées.
        /// </summary>
        /// <param name="disposing">true si les ressources managées doivent être supprimées*; sinon, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
    }
}

Le programme s'exécute sans erreur mais il m'imprime une page blanche au lieu de mon fichier test. Apparament les instructions suivantes :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
try
            {
                //FileStream fs = new FileStream(@"C:\test1.txt", FileMode.Open, FileAccess.Read);
                //streamToPrint = new StreamReader(fs);
                streamToPrint = new StreamReader(@"C:\test1.txt");
                try
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    ...
                    pd.Print()
ne me permettent pas de récupérer mon fichier test1.txt à l'instanciation du PrintDocument. A la place, on dirait qu'il m'instancie un document vide.
Comme vous pouvez le voir j'ai essayé de passer par l'intermédiaire d'un FileStream, mais cela ne change rien.

Quelqu'un aurait une idée ?
Merci