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
{printform
e.HasMorePages = false;
}
private void printPreviewButton_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint = new StreamReader(filePath);
try
{
PrintPreview();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void runtimeDialogButton_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint = new StreamReader(filePath);
try
{
RuntimeDialog();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void printPreviewControlButton_Click(object sender, System.EventArgs e)  {
try
{
streamToPrint = new StreamReader(filePath);
try
{
PrintPreviewControl();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void RuntimeDialog()
{
PrintPreviewDialog pPDlg;
pPDlg = new PrintPreviewDialog();
pPDlg.Document = pDoc;
pPDlg.WindowState = FormWindowState.Maximized;
pPDlg.PrintPreviewControl.Columns = 2;
pPDlg.ShowDialog();
pPDlg.Dispose();
private void PrintPreviewControl()
{
Form formPreview = new Form();
PrintPreviewControl previewControl = new PrintPreviewControl();
previewControl.Document = printDocument1;
previewControl.StartPage = 2;
formPreview.WindowState = FormWindowState.Maximized;
formPreview.Controls.Add(previewControl);
formPreview.Controls[0].Dock = DockStyle.Fill;
formPreview.ShowDialog();
formPreview.Dispose();
}
private void PrintPreview()
{
//设置页⾯的预览的页码
//设置显⽰页⾯显⽰的⼤⼩(也就是原页⾯的倍数)
printPreviewDialog1.PrintPreviewControl.StartPage = 0;
printPreviewDialog1.PrintPreviewControl.Zoom =1.0;
//设置或返回窗⼝状态,即该窗⼝是最⼩化、正常⼤⼩还是其他状态。
printPreviewDialog1.WindowState = FormWindowState.Maximized;            //设置和获取需要预览的⽂档
//将窗体显⽰为指定者的模式对话框
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void PrintDoc()
{
printDialog1.Document = printDocument1;
DialogResult userResPonse= printDialog1.ShowDialog();
if(userResPonse==DialogResult.OK)
{
printDocument1.Print();
}
}
//获取打印机的设置和打印的属性
private void button1_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint=new StreamReader(filePath);
try
{
PrintDoc();
}
catch{}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
streamToPrint.Close();
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。