• winforms打印


    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;
        }
    }
  • 相关阅读:
    第二章作业
    算法第五章作业及学期总结
    71 最大子列和问题 | 采用二分法+递归
    算法第一章作业
    算法第四章作业
    算法第三章作业
    Windows charles安装、配置及Android手机ca证书
    git出现fatal: Authentication failed for 'http:xxxx.git/''错误的解决办法
    如何用c#将十六进制转换成字符串?
    系统配置win7(64位),vs2010,没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10401256.html
Copyright © 2020-2023  润新知