Kaydol:
Kayıt Yorumları
(
Atom
)
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.Drawing.Printing; namespace C_Sharp_Print_Listbox_Item { public partial class Form1 : Form { PrintPreviewDialog printPreviewDialog; PrintDocument printDocument; Font arialFont; int counter = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Create print document object printDocument = new PrintDocument(); //Create print priview dialog object printPreviewDialog = new PrintPreviewDialog(); //Add PrintPage event handler printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint); printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint); //Create font object arialFont = new Font("Arial", 12); //fill listbox with numbers for (int i = 0; i < 1000; i++) { listBox1.Items.Add(i); } } private void btnPrint_Click(object sender, EventArgs e) { counter = 0; printPreviewDialog.Document = printDocument; printPreviewDialog.ShowDialog(); } private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { this.Text = "The document is printed"; } private void printDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { this.Text = "Printing is completed"; } private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs printpageevent) { //Get the Graphics object from the print page event Graphics g = printpageevent.Graphics; float linesPerPage = 0; float yposition = 0; int count = 0; float leftMargin = printpageevent.MarginBounds.Left; float topMargin = printpageevent.MarginBounds.Top; string line = null; //Calculate the lines per page on the basis of the height of the page and the height of the font linesPerPage = printpageevent.MarginBounds.Height / arialFont.GetHeight(g); //Now read lines one by one, using StreamReader while (count < linesPerPage && counter < listBox1.Items.Count ) { //get the line in listbox line = listBox1.Items[counter++].ToString(); //Calculate the starting position yposition = topMargin + (count * arialFont.GetHeight(g)); //Draw text g.DrawString(line, arialFont, Brushes.Black, leftMargin, yposition, new StringFormat()); //Move to next line count++; } //If PrintPageEventArgs has more pages to print if (line != null) { printpageevent.HasMorePages = true; } else { printpageevent.HasMorePages = false; } } } }
Hiç yorum yok :
Yorum Gönder