Skip to content

Instantly share code, notes, and snippets.

@Mahno74
Last active July 6, 2021 10:37
Show Gist options
  • Save Mahno74/f4af48bff08c4c5c8baea8c77d2ab1f0 to your computer and use it in GitHub Desktop.
Save Mahno74/f4af48bff08c4c5c8baea8c77d2ab1f0 to your computer and use it in GitHub Desktop.
Печать текста
private void печатьToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
Reader = new System.IO.StreamReader(openFileDialog1.FileName, System.Text.Encoding.GetEncoding(1251));
try { printDocument1.Print(); }
finally { Reader.Close(); }
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Событие вывода на печать страницы
Single СтрокНаСтранице = 0.0F; Single Y = 0;
var LeftBorder = e.MarginBounds.Left; var ВерхнийКрай = e.MarginBounds.Top;
var Line = String.Empty; var Шрифт = new Font("Times New Roman", 12.0F);
// Вычисляем колчество строк на одной сранице
СтрокНаСтранице = e.MarginBounds.Height / Шрифт.GetHeight(e.Graphics);
// Печатаем каждую строчку файла
var i = 0; // Счет строк
while (i < СтрокНаСтранице)
{
Line = Reader.ReadLine();
if (Line == null) break; //выход из цикла
Y = ВерхнийКрай + i * Шрифт.GetHeight(e.Graphics);
// Печать строки
e.Graphics.DrawString(Line, Шрифт, Brushes.Black, LeftBorder, Y, new StringFormat());
i = i + 1; // или i += 1 счет строк
}
// Печать следеющей страницы если еще есть строки в файле
if (Line != null) e.HasMorePages = true; else e.HasMorePages = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment