PrintingTextFile.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Simple text file printing sample
  3. //
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Drawing.Printing;
  8. public class PrintingTextFile
  9. {
  10. private static StreamReader stream;
  11. static private void PrintPageEvent (object sender, PrintPageEventArgs e)
  12. {
  13. float lines_page, y;
  14. int count = 0;
  15. float left = e.MarginBounds.Left;
  16. float top = e.MarginBounds.Top;
  17. String line = null;
  18. Font font = new Font ("Arial", 10);
  19. float font_height = font.GetHeight (e.Graphics);
  20. lines_page = e.MarginBounds.Height / font_height;
  21. while (count < lines_page) {
  22. line = stream.ReadLine ();
  23. if (line == null)
  24. break;
  25. y = top + (count * font_height);
  26. e.Graphics.DrawString (line, font, Brushes.Black, left, y, new StringFormat());
  27. count++;
  28. }
  29. if (line != null)
  30. e.HasMorePages = true;
  31. else
  32. e.HasMorePages = false;
  33. }
  34. public static void Main (string[] args)
  35. {
  36. stream = new StreamReader ("PrintMe.txt");
  37. PrintDocument p = new PrintDocument ();
  38. p.PrintPage += new PrintPageEventHandler (PrintPageEvent);
  39. p.Print ();
  40. stream.Close();
  41. }
  42. }