IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Impression avec formulaire


Sujet :

C#

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 10
    Points : 6
    Points
    6
    Par défaut Impression avec formulaire
    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

  2. #2
    Membre régulier
    Homme Profil pro
    Informaticien
    Inscrit en
    Février 2011
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Suisse

    Informations professionnelles :
    Activité : Informaticien

    Informations forums :
    Inscription : Février 2011
    Messages : 53
    Points : 83
    Points
    83
    Par défaut
    Bonjour,

    tu as crée un stream que tu as nommé streamToPrint, par contre tu ne le donne jamais comme paramètre pour l'impression, comment est-ce qu'il doit savoir
    qu'est-ce qu'il doit imprimer?

    regarde du coté de

    http://ondotnet.com/pub/a/dotnet/200.../printing.html


Discussions similaires

  1. Réponses: 2
    Dernier message: 23/01/2010, 18h05
  2. [VB6] Formulaire d'impression avec CR
    Par jerzy59 dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 27/04/2006, 14h50
  3. Impression avec le composant TRvSystem
    Par tarbala dans le forum Composants VCL
    Réponses: 3
    Dernier message: 02/10/2004, 17h03
  4. impression avec quickreport
    Par k_boy dans le forum Bases de données
    Réponses: 4
    Dernier message: 14/06/2004, 09h06
  5. Probleme d'impression avec la méthode TForm->Print()
    Par Kid Icarus dans le forum C++Builder
    Réponses: 13
    Dernier message: 31/07/2002, 14h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo