C# 中打印、预览、打印机设置和打印属性的方法
生活随笔
收集整理的這篇文章主要介紹了
C# 中打印、预览、打印机设置和打印属性的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
private void Form1_Load(object sender, System.EventArgs e)
??{
???//獲取或設置一個值,該值指示是否發送到文件或端口
???printDocument1.PrinterSettings.PrintToFile = true;
???//設置打印時橫向還是縱向
???printDocument1.DefaultPageSettings.Landscape = true;
??}
??private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
??{
???OpenFile();
??}
??private void OpenFile()
??{
???openFileDialog1.Filter = "Text Files (*.txt)|*.txt";//打開文本的類型
???//獲取文件對話框的初始目錄(StartupPath)獲得bin文件下的文件
???openFileDialog1.InitialDirectory = System.Windows.Forms.Application.StartupPath;
???DialogResult userResponse = openFileDialog1.ShowDialog();
???//MessageBox.Show(userResponse.ToString());
???if (userResponse==DialogResult.OK)
???{
????filePath = openFileDialog1.FileName.ToString();//轉換文件路徑
???}
??}
??private void MyPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
???//充分利用e
??{
???
???int topMargin = printDocument1.DefaultPageSettings.Margins.Top;//上邊距
???int leftMargin = printDocument1.DefaultPageSettings.Margins.Left;//左邊距
???float linesPerPage = 0;//頁面行號
???float verticalPosition = 0;//繪制字符串的縱向位置
???float horizontalPosition=leftMargin;//左邊距
???string textLine = null;//行字符串
???currentLine = 0;//行計數器
//???float Xline=0;
???//int line=0;
???// Calculate the number of lines per page.
???linesPerPage = e.MarginBounds.Height / myFont.GetHeight(e.Graphics);
//???Xline=e.MarginBounds.Width/myFont.GetHeight();
???
??????????? // for each text line that will fit on the page, read a new line from the document
???while (currentLine < linesPerPage )
???{
????textLine = streamToPrint.ReadLine();
????if(textLine == null)
????{
?????break;
????}
????// 求出已經打印的范圍
????
????verticalPosition = topMargin + currentLine * myFont.GetHeight(e.Graphics);
????// 設置頁面的屬性
????e.Graphics.DrawString(textLine, myFont, myBrush, horizontalPosition, verticalPosition);
????// 增加行數
????currentLine ++;
????
???}
???// If more lines of text exist in the file, print another page.
???if (textLine != null)
???{
????e.HasMorePages = true;
???}
???else
???{
????e.HasMorePages = false;
???}
??}
??private void printPreviewButton_Click(object sender, System.EventArgs e)
??{
???try
???{
????streamToPrint = new StreamReader(filePath);
????try
????{
?????PrintPreview();
????}
??
??{
???//獲取或設置一個值,該值指示是否發送到文件或端口
???printDocument1.PrinterSettings.PrintToFile = true;
???//設置打印時橫向還是縱向
???printDocument1.DefaultPageSettings.Landscape = true;
??}
??private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
??{
???OpenFile();
??}
??private void OpenFile()
??{
???openFileDialog1.Filter = "Text Files (*.txt)|*.txt";//打開文本的類型
???//獲取文件對話框的初始目錄(StartupPath)獲得bin文件下的文件
???openFileDialog1.InitialDirectory = System.Windows.Forms.Application.StartupPath;
???DialogResult userResponse = openFileDialog1.ShowDialog();
???//MessageBox.Show(userResponse.ToString());
???if (userResponse==DialogResult.OK)
???{
????filePath = openFileDialog1.FileName.ToString();//轉換文件路徑
???}
??}
??private void MyPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
???//充分利用e
??{
???
???int topMargin = printDocument1.DefaultPageSettings.Margins.Top;//上邊距
???int leftMargin = printDocument1.DefaultPageSettings.Margins.Left;//左邊距
???float linesPerPage = 0;//頁面行號
???float verticalPosition = 0;//繪制字符串的縱向位置
???float horizontalPosition=leftMargin;//左邊距
???string textLine = null;//行字符串
???currentLine = 0;//行計數器
//???float Xline=0;
???//int line=0;
???// Calculate the number of lines per page.
???linesPerPage = e.MarginBounds.Height / myFont.GetHeight(e.Graphics);
//???Xline=e.MarginBounds.Width/myFont.GetHeight();
???
??????????? // for each text line that will fit on the page, read a new line from the document
???while (currentLine < linesPerPage )
???{
????textLine = streamToPrint.ReadLine();
????if(textLine == null)
????{
?????break;
????}
????// 求出已經打印的范圍
????
????verticalPosition = topMargin + currentLine * myFont.GetHeight(e.Graphics);
????// 設置頁面的屬性
????e.Graphics.DrawString(textLine, myFont, myBrush, horizontalPosition, verticalPosition);
????// 增加行數
????currentLine ++;
????
???}
???// If more lines of text exist in the file, print another page.
???if (textLine != null)
???{
????e.HasMorePages = true;
???}
???else
???{
????e.HasMorePages = false;
???}
??}
??private void printPreviewButton_Click(object sender, System.EventArgs e)
??{
???try
???{
????streamToPrint = new StreamReader(filePath);
????try
????{
?????PrintPreview();
????}
??
轉載于:https://www.cnblogs.com/zengjiliang/archive/2011/10/08/2202091.html
總結
以上是生活随笔為你收集整理的C# 中打印、预览、打印机设置和打印属性的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# winform 窗体接收命令行参数
- 下一篇: C#备份数据和还原数据