Réduire toutDévelopper tout Code : Tous Code : Multiple Code : Visual Basic Cod

Réduire toutDévelopper tout Code : Tous Code : Multiple Code : Visual Basic Code : C# Code : Visual C++ Code : J# Code : JScript Visual Basic C# Visual C++ J# JScript Programmation Windows Forms Comment : imprimer un fichier texte composé de plusieurs pages dans les Windows Forms Exemple Voir aussi Commentaires Il est courant d'imprimer du texte à partir des applications Windows. La classe Graphics propose des méthodes pour dessiner des objets (graphismes ou texte) sur un périphérique, comme un écran ou une imprimante. Remarque : Les méthodes DrawText de TextRenderer ne sont pas prises en charge pour l'impression. Vous devez toujours utiliser les méthodes DrawString de Graphics, comme indiqué dans l'exemple de code suivant, pour dessiner un texte destiné à être imprimé. Pour imprimer du texte 1. Ajoutez un composant PrintDocument et une chaîne à votre formulaire. Visual Basic Copier le code Private printDocument1 As New PrintDocument() Private stringToPrint As String C# Copier le code private PrintDocument printDocument1 = new PrintDocument(); private string stringToPrint; 2. Lors de l'impression d'un document, définissez la propriété DocumentName en fonction du document que vous souhaitez imprimer, puis ouvrez et lisez le contenu du document jusqu'à la chaîne que vous avez ajoutée précédemment. Visual Basic Copier le code Dim docName As String = "testPage.txt" Dim docPath As String = "c:\" printDocument1.DocumentName = docName Dim stream As New FileStream(docPath + docName, FileMode.Open) Try Dim reader As New StreamReader(stream) Try stringToPrint = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try C# Copier le code string docName = "testPage.txt"; string docPath = @"c:\"; printDocument1.DocumentName = docName; using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { stringToPrint = reader.ReadToEnd(); } 3. Dans le gestionnaire d'événements PrintPage, utilisez la propriété Graphics de la classe PrintPageEventArgs et le contenu du document pour calculer la longueur de ligne et le nombre de lignes par page. Après l'impression de chaque page, vérifiez s'il s'agit de la dernière et définissez la propriété HasMorePages de la classe PrintPageEventArgs en conséquence. L'événement PrintPage est déclenché jusqu'à ce que la propriété HasMorePages ait la valeur false. Assurez-vous également que l'événement PrintPage est associé à sa méthode de gestion d'événements. Dans l'exemple de code suivant, le gestionnaire d'événements est utilisé pour imprimer le contenu du fichier "testPage.txt" dans la même police que celle utilisée sur le formulaire. Visual Basic Copier le code Private Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs) Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic) ' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage) ' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 End Sub C# Copier le code private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { int charactersOnPage = 0; int linesPerPage = 0; // Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic); // Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage); // Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); } 4. Appelez la méthode Print pour déclencher l'événement PrintPage. Visual Basic Copier le code printDocument1.Print() C# Copier le code printDocument1.Print(); Exemple Visual Basic Copier le code Imports System Imports System.Drawing Imports System.IO Imports System.Drawing.Printing Imports System.Windows.Forms Public Class Form1 Inherits Form Private printButton As Button Private printDocument1 As New PrintDocument() Private stringToPrint As String Public Sub New() Me.printButton = New System.Windows.Forms.Button() Me.printButton.Location = New System.Drawing.Point(12, 51) Me.printButton.Size = New System.Drawing.Size(75, 23) Me.printButton.Text = "Print" Me.ClientSize = New System.Drawing.Size(292, 266) End Sub Private Sub ReadFile() Dim docName As String = "testPage.txt" Dim docPath As String = "c:\" printDocument1.DocumentName = docName Dim stream As New FileStream(docPath + docName, FileMode.Open) Try Dim reader As New StreamReader(stream) Try stringToPrint = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try End Sub Private Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs) Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic) ' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage) ' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 End Sub Private Sub printButton_Click(ByVal sender As Object, ByVal e As EventArgs) ReadFile() printDocument1.Print() End Sub <STAThread()> _ Shared Sub Main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault (False) Application.Run(New Form1()) End Sub End Class C# Copier le code using System; using System.Drawing; using System.IO; using System.Drawing.Printing; using System.Windows.Forms; namespace PrintApp { public class Form1 : Form { private Button printButton; private PrintDocument printDocument1 = new PrintDocument(); private string stringToPrint; public Form1() { this.printButton = new System.Windows.Forms.Button(); this.printButton.Location = new System.Drawing.Point(12, 51); this.printButton.Size = new System.Drawing.Size(75, 23); this.printButton.Text = "Print"; this.printButton.Click += new System.EventHandler(this.printButton_Click); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.printButton); // Associate the PrintPage event handler with the PrintPage event. printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); } private void ReadFile() { string docName = "testPage.txt"; string docPath = @"c:\"; printDocument1.DocumentName = docName; using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { stringToPrint = reader.ReadToEnd(); } } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { int charactersOnPage = 0; int linesPerPage = 0; // Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic); // Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage); // Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); } private void printButton_Click(object sender, EventArgs e) { ReadFile(); printDocument1.Print(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDef ault(false); Application.Run(new Form1()); } } } Compilation du code Cet exemple nécessite les actions ou les éléments suivants : • Références aux assemblys System, System.Windows.Forms, System.Drawing. • Pour plus d'informations sur la génération de cet exemple à partir de la ligne de commande pour Visual Basic ou Visual C#, consultez Génération à partir de la ligne de commande (Visual Basic) ou Génération à partir de la ligne de commande. Vous pouvez aussi générer cet exemple dans Visual Studio en collant le code dans un nouveau projet. Pour plus d'informations, consultez Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio. Voir aussi Concepts Prise en charge de l'impression dans les Windows Forms Référence Graphics Brush Envoyer des commentaires à propos de cette rubrique à Microsoft. uploads/s1/ imprimer-un-fichier-texte-compose-de-plusieurs-pages-dans-les-windows-forms.pdf

  • 175
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager
  • Détails
  • Publié le Sep 17, 2021
  • Catégorie Administration
  • Langue French
  • Taille du fichier 0.1187MB