码迷,mamicode.com
首页 > Web开发 > 详细

.net打印控件基本用法

时间:2015-05-05 14:18:10      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

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

 

.net打印控件基本用法

标签:

原文地址:http://www.cnblogs.com/shengyu-kmust/p/4478810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!