zoukankan      html  css  js  c++  java
  • .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:
    edist.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:
    edist.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
  • 相关阅读:
    C语言实现二叉堆BuildHeap操作
    Java大浮点数
    线索二叉树
    二叉树的层次遍历
    CS231n Lecture3-Loss Functions and Optimization学习笔记
    Python函数式编程(高阶函数、map/reduce、filter、sorted、匿名函数、返回函数)-3
    CS231n Lecture2-Image Classification学习笔记
    Python高级特性(生成器、迭代器、列表生成式)-2
    Python函数(定义、参数、递归)-1
    Numpy中的广播(Broadcast)讲解简单易懂,困扰已久,终于想通了
  • 原文地址:https://www.cnblogs.com/shengyu-kmust/p/4478810.html
Copyright © 2011-2022 走看看