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 = ;
int linesPerPage = ;
// 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 > );///如果没有打印完就打印下一页,会自动调用这个事件来打印下一页 // 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 += ;///下一页
} private void button3_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage -= ;///上一页
} private void button4_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom += 0.1;///放大
} private void button5_Click(object sender, EventArgs e)
{
printPreviewControl1.Zoom -= 0.1;//缩小
}
}
}

.net打印控件基本用法的更多相关文章

  1. 比较好用的web打印控件——Lodop

    前一段时间公司一项目比较特殊,客户要求打印单必须是淘宝上卖的那种三联打印单.如果还是使用原来系统自带的打印的话,就会造成无法打印出来理想的效果,于是找了下相关的打印控件,比较网络上比较流行的几款插件, ...

  2. Web打印控件Lodop实现表格物流单的打印

    Web打印控件Lodop实现表格物流单的打印 一.lodop打印预览效果图 LODOP.PRINT_SETUP();打印维护效果图 LODOP.PREVIEW();打印预览图 二.写在前面 最近项目用 ...

  3. 基于MVC4+EasyUI的Web开发框架经验总结(16)--使用云打印控件C-Lodop打印页面或套打报关运单信息

    在最新的MVC4+EasyUI的Web开发框架里面,我整合了关于网购运单处理的一个模块,其中整合了客户导单.运单合并.到货扫描.扣仓.出仓.查询等各个模块的操作,里面涉及到一些运单套打的操作,不过由于 ...

  4. winform对话框控件、打印控件

    对话框控件: ColorDialog:颜色选择对话框,让用户自行选择一种颜色,使用方法类似FontDialog FontDialog:字体选择对话框,让用户自行选择一种字体(也可以选择字体颜色,需要在 ...

  5. winform 对话框,保存,另存为,还有打印控件

    学习的对话框的种类: 1.打开文件对话框(OpenFileDialog) 2.保存文件对话框(SaveFileDialog) 3.字体对话框(FontDialog) 4.颜色对话框(ColorDial ...

  6. [2014.01.27]wfPrintOcx 票据打印控件 v5.3

    支持选择打印机,并自动记忆选择的打印机. 适合打印各类票据格式. 支持画正方形.长方形.圆形.椭圆.横线.竖线.HTTP图片. 支持数字金额转换成中文大写金额. 可以设置固定宽度的文字自动换行. 组件 ...

  7. 网页WEB打印控件制作-开放源码

    在WEB系统中,打印的确是比较烦人的问题,如果我们能制作一个属于自己的自定义的打印插件,那么我们在后续自定义打印的时候能随心所欲的控制打印,这样的效果对于程序员来说是非常开心的一件事件,本文将自己开发 ...

  8. lodop打印控件一点记录

    今天初步接触了下打印控件 LODOP实现了自动分页,高度宽度都可以自己设定来分页. 页码,使用LODOP.SET_PRINT_STYLE("ItemType", 2); LODOP ...

  9. Windows 打印控件

    Windows窗体的PrintDocument组件用于设置一些属性,这些属性说明,在基于Windows的应用程序中要打印说明内容以及打印文档的能力,可将它与PrintDialog组件一起使用来控制文档 ...

随机推荐

  1. PHP运算符优先级 运算符分类

    运算符 运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西. 运算符可按照其能接受几个值来分组.一元运算符只能接受一个值,例如 !(逻辑取反运 ...

  2. 小程序使用wx.chooseAddress获取用户手机号码,微信chooseAddress接口获取用户收货信息

    通常用户在商城购买产品后,需要填写他的收货信息,方便我们发货,但是在手机上写字非常不方便,一个客户的收货信息包括:姓名,地址和手机号码这些内容全部填写的话,至少要写20个字. 地址 所以有些客户在手机 ...

  3. 【Jsp】JSP自己定义标签与MODEL1、MODEL2标准

    在JSP2.0之后支持自己定义标签,如今一般都是jsp2.4的版本号了,所以无须考虑版本号的问题. 直接使用就能够了.尽管一般开发的过程中,非常少会自己定义JSP标签.可是通过一个JSP自己定义标签的 ...

  4. 一、Spring Cloud介绍

    springcloud 介绍 研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Clou ...

  5. idea的快捷键

    idea的快捷键大全 Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+ ...

  6. 【java】java.util.Arrays类常用方法

    package Arrays类; import java.util.Arrays; public class TestArrays { public static void main(String[] ...

  7. 【java】打印一个对象即打印出该对象toString()返回值

    public class TestToString { public static void main(String[] args){ Node node1=new Node("东邪&quo ...

  8. OC学习14——谓词

    一.谓词的基本概念与使用 1.谓词(NSPredicate)用于定义一个逻辑条件,通过该条件可执行搜索或内存中的过滤操作.上一篇文章中介绍的集合都提供了使用谓词对集合进行过滤的方法.OC中的谓词操作是 ...

  9. iOS中self与_的区别

    同时我们发现在我们访问我们声明的变量时,会有self. 和 以"_"开头的访问方式,那么这两种方式到底有什么样的区别呢? 我们来一起看一下: @property (retain, ...

  10. java.lang.StringBuilder和java.lang.StringBuffer (JDK1.8)

    这两个类都是继承自AbstractStringBuilder,AbstractStringBuilder有两个成员属性 char[] value; int count; 前者用于存储字符串,后者用于统 ...