Hier habe ich ein kleines Codebeispiel
==>
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:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Printing;
private void drucken2_Load(object sender, PrintPageEventArgs ppeArgs) { Graphics g = ppeArgs.Graphics; float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ppeArgs.MarginBounds.Left; float topMargin = ppeArgs.MarginBounds.Top; string line = null; linesPerPage = ppeArgs.MarginBounds.Height / verdana10Font.GetHeight(g); while (count < linesPerPage && ((line = reader.ReadLine()) != null)) { yPos = topMargin + (count * verdana10Font.GetHeight(g)); g.DrawString(line, verdana10Font, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; }
if (line != null) { ppeArgs.HasMorePages = true; }
else { ppeArgs.HasMorePages = false; } }
private void print_Click(object sender, EventArgs e) { try { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Titel - Druck"; fdlg.InitialDirectory = @"C:\ "; fdlg.Filter ="Text files (*.txt | .txt | All files (*.*) | *.*"; fdlg.FilterIndex = 2; fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK) { textBoxX1.Text = fdlg.FileName; }
string filename = textBoxX1.Text.ToString(); reader = new StreamReader(filename); verdana10Font = new Font("Verdana", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(this.drucken2_Load); pd.PrinterSettings.PrinterName = printersList.Text;
pd.Print(); if (reader != null) reader.Close(); }
catch(Exception a) { MessageBox.Show("Bitte geben Sie den Drucker an mit dem das Dokument gedruckt werden soll."); } } |
Hilfreich ist der Code allerdings nur wenn du eine ComboBox in deinem Formular hast, sowie wie einen Druck-Button.
PS: Diese Lösung druckt nur txtDateien aus.
Falls du noch Fragen hast dann frag nochmal nach. Ich hoffe meine kommentare helfen für verständniss.
mfg
reichenberg-reloaded