public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { ////设置字体样式 //Font f = new System.Drawing.Font("微软雅黑", 16); //f = richTextBox1.Font; ////设置字体颜色 //Brush b = new SolidBrush(richTextBox1.ForeColor); ////e.绘制.画字符串(要打印的文本,文本格式,画刷-颜色和纹理,位置坐标) //e.Graphics.DrawString(richTextBox1.Text, f, b, 20, 10); Image img = Image.FromFile(@"C:UserskylewDesktop est.docx"); //MemoryStream ms = new MemoryStream(); //byte[] imagedata = null; //img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //imagedata = ms.GetBuffer(); //Stream stream = new MemoryStream(imagedata); //Metafile metafile = new Metafile(stream); e.Graphics.DrawImage(img, new Rectangle(10, 10, 1000, 1000)); } private void button1_Click(object sender, EventArgs e) { pageSetupDialog1.Document = printDocument1; pageSetupDialog1.ShowDialog(); } private void button4_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); if (dr == DialogResult.OK) { richTextBox1.Font = fontDialog1.Font; richTextBox1.ForeColor = fontDialog1.Color; } } private void button2_Click(object sender, EventArgs e) { printPreviewControl1.Document = printDocument1; //printPreviewDialog1.Document = printDocument1; //printPreviewDialog1.ShowDialog(); } private void button3_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; DialogResult dr = printDialog1.ShowDialog(); if (dr == DialogResult.OK) { printDocument1.Print(); } } private void button5_Click(object sender, EventArgs e) { printPreviewDialog1.Document = printDocument1; DialogResult dr = printPreviewDialog1.ShowDialog(); } }
分页:
int ii = 0; public void PrintReport(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (ii < 5) //如果将 if 改成 for 语句, 会将几页张合在一张纸上, 而达不到要求 { Font font = new Font("宋休", 12 + 30 * ii); e.Graphics.DrawString(ii.ToString(), font, Brushes.Black, new Point(10, 10)); ii++; } if (ii < 4) //以此数 4 为准而不是上面的5,即出现4次 { e.HasMorePages = true; } else if (ii == 5) { e.HasMorePages = false; } } int ii = 0; public void PrintReport(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font font = new Font("宋休", 12 + 30 * ii); e.Graphics.DrawString(ii.ToString(), font, Brushes.Black, new Point(10, 10)); ii++; if (ii < 5) //如果把上面的语句放在 if 里, 会多产生一张空白页 { e.HasMorePages = true; } else if (ii == 5) { e.HasMorePages = false; } }