.net打印控件基本用法

1、在winform上加如下控件

技术分享

2、代码和用法如下:

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winform
{
    /*
     在 Windows 窗体中进行打印主要包括以下两个方面:使用 PrintDocument 组件(Windows 窗体)组件来使用户可以打印;使用 PrintPreviewDialog 控件(Windows 窗体)控件、 PrintDialog 组件(Windows 窗体)组件和 PageSetupDialog 组件(Windows 窗体)组件向了解 Windows 操作系统的用户提供熟悉的图形界面
     * printdocument使用方法:1、创建;2、在printpage事件中写每一页的打印逻辑;3、调用print方法
     * printPreviewControl用法:1、创建;2、将document属性和printdocument关联;3、在printdocument的printpage事件里写打印逻辑;4、运行程序后会自动调用printpage事件;5、如果要刷新就用InvalidatePreview函数
     */
    public partial class UsageOfControl6 : Form
    {
        string documentContents;//总打印字符
        string stringToPrint;///要打印的
        public UsageOfControl6()
        {
            InitializeComponent();
            ///考虑printPreviewControl组件在初始化时就调用了printdocument的printpage事件,在这里初始化打印数据
            using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;
        }

        private void UsageOfControl6_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;//打印预览控件
            printDialog1.Document = printDocument1;//打印设置控件
            pageSetupDialog1.Document=printDocument1;//页面设置控件
            printPreviewControl1.Document = printDocument1;
            

            using (FileStream stream = new FileStream(@"d:\redist.txt", FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;

            if (printDialog1.ShowDialog()==DialogResult.OK)///通过printDialog1来设置printdocument1的属性:打印机,打印页数,打印方向 
            {
                pageSetupDialog1.ShowDialog();///打印纸设置,边距,页眉,纸大小,也可以在printDocument1_PrintPage事件中获取e.PageSettings来设置
                this.printPreviewDialog1.ShowDialog();///打印预览
                printDocument1.Print();
                printPreviewControl1.InvalidatePreview();////刷新,会再次调用printdocument的printpage事件
            }

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;
            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            //算出stringToPrint铺满打印页时的打印行数和字符数
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page.///打印本页文字
            ///除了打印文字还可以打印线条,图形,屏幕截图等,可以在批定的位置打
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);



            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);//下一页要打的

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);///如果没有打印完就打印下一页,会自动调用这个事件来打印下一页

            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = documentContents;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewControl1.StartPage += 1;///下一页
        }

        private void button3_Click(object sender, EventArgs e)
        {
            printPreviewControl1.StartPage -= 1;///上一页
        }

        private void button4_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom += 0.1;///放大
        }

        private void button5_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Zoom -= 0.1;//缩小
        }
    }
}
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。