Form1.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsPrintDocument
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  19. {
  20. e.Graphics.DrawString("蝶恋花", new Font("宋体", 15), Brushes.Black, 350, 80);
  21. e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 185, 720, 185);
  22. e.Graphics.DrawString("伫倚危楼风细细,望极春愁,黯黯生天际。", new Font("宋体", 12), Brushes.Black, 110, 195);
  23. e.Graphics.DrawString("草色烟光残照里,无言谁会凭阑意。", new Font("宋体", 12), Brushes.Black, 110, 220);
  24. e.Graphics.DrawString("拟把疏狂图一醉,对酒当歌,强乐还无味。", new Font("宋体", 12), Brushes.Black, 110, 245);
  25. e.Graphics.DrawString("衣带渐宽终不悔。为伊消得人憔悴。", new Font("宋体", 12), Brushes.Black, 110, 270);
  26. e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 300, 720, 300);
  27. }
  28. private void button1_Click(object sender, EventArgs e)
  29. {
  30. if (MessageBox.Show("是否要预览打印文档", "打印预览", MessageBoxButtons.YesNo) == DialogResult.Yes)
  31. {
  32. //开启操作系统的防锯齿功能,使预览到的文档的图形或线条更加平滑和流畅
  33. this.printPreviewDialog1.UseAntiAlias = true;
  34. //设置要预览的文档
  35. this.printPreviewDialog1.Document = this.printDocument1;
  36. printPreviewDialog1.ShowDialog();
  37. }
  38. else
  39. {
  40. //调用Print方法直接打印文档
  41. this.printDocument1.Print();
  42. }
  43. }
  44. }
  45. }