Thursday, June 3, 2021

【Visual Studio Visual Csharp】Print

 using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; // make sure that using System.Diagnostics; is included using System.Diagnostics; using System.Drawing; // make sure that using System.Drawing.Printing; is included using System.Drawing.Printing; // make sure that using System.IO; is included using System.IO; using System.Windows.Forms; namespace PrintC { public partial class Form1 : Form { private Font Verdana11FontSize; private StreamReader Reader; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void PrintTextFileHandler(object sender, PrintPageEventArgs PrinterArgs) { Graphics g = PrinterArgs.Graphics; float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = PrinterArgs.MarginBounds.Left; float topMargin = PrinterArgs.MarginBounds.Top; string line = null; linesPerPage = PrinterArgs.MarginBounds.Height / Verdana11FontSize.GetHeight(g); while (count < linesPerPage && ((line = Reader.ReadLine()) != null)) { yPos = topMargin + (count * Verdana11FontSize.GetHeight(g)); g.DrawString(line, Verdana11FontSize, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } if (line != null) { PrinterArgs.HasMorePages = true; } else { PrinterArgs.HasMorePages = false; } } private void button2_Click(object sender, EventArgs e) { string filename = textBox1.Text.ToString(); Reader = new StreamReader(filename); Verdana11FontSize = new Font("Verdana", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler); pd.Print(); if (Reader != null) Reader.Close(); } } }

No comments:

Post a Comment

[New post] All or Nothing

...