打印机设置(PrintDialog)、页⾯设置(PageSetupDialog)及RDLC。。。
如果⼀台电脑同时连接多个打印机,⽽且每个打印机使⽤的纸张⼤⼩各不相同(⽐如:票据打印钱⽤的⼩票专⽤张,办公打印机⽤的是A4标准纸),在处理打印类的需求时,如果不⽤代码⼲预,⽤户必须每次打印时,都必须在弹出窗⼝⾥,⼿动选择打印机(还有可能需要设置纸张类型),这样⽤起来会⽐较烦。
幸好,.Net提供了“⼆”个很有⽤的类:PrintDialog(打印机设置对话框)、PageSetupDialog(页⾯设置对话框) (注:之所以给“⼆”加引号,是因为"打印机设置"其实已经完全包含
了“页⾯设置”的所有功能,⽽且“ PageSetupDialog”类在实际使⽤中发现还有⼀个容易误导的地⽅)
⼀、打印机设置(PrintDialog)
⽰例代码:
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PrintDemo
{printform
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPrintSet_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == DialogResult.OK) {
ShowInfo(dlg.PrinterSettings);
}
}
private void ShowInfo(PrinterSettings printSettings) {
var pageSettings = printSettings.DefaultPageSettings;
var pageSize = pageSettings.PaperSize;
label1.Text = string.Format("打印机:{1}{0}纸张类型:{2}{0}纸张⼤⼩:{3}cm * {4}cm{0}纸张布局:{5}",
Environment.NewLine + Environment.NewLine,
printSettings.PrinterName,
pageSize.PaperName,
InchToCm(pageSize.Width / 100.0M).ToString("F2"),
InchToCm(pageSize.Height / 100.0M).ToString("F2"),
pageSettings.Landscape ? "横向" : "纵向");
}
const decimal K = 2.54M;
private decimal InchToCm(decimal inch)
{
return inch * K;
}
private decimal CmToInch(decimal cm)
{
return cm / K;
}
}
}
"打印机设置"对话框的截图如下:
"打印机设置"对话框调⽤完成后,可以取回⼀些有⽤的信息,下⾯是⼀些关键信息的展⽰截图:
可以看到,打印机设置界⾯⾥,已经包含了关于纸张尺⼨、纸张布局(纵向、横向)的设置,所以在我看来,系统没有必要再单独提供"页⾯设置(PageSetupDialog)"
⼆、页⾯设置(PageSetupDialog)
⽰例代码:(在刚才的代码基础上加点东西)
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PrintDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 打印机设置
/
// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintSet_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == DialogResult.OK) {
ShowInfo(dlg.PrinterSettings);
}
}
/// <summary>
/
// 页⾯设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPageSet_Click(object sender, EventArgs e)
{
PageSetupDialog dlg = new PageSetupDialog();
dlg.PageSettings = new PageSettings();
if (dlg.ShowDialog() == DialogResult.OK)
{
ShowInfo(dlg.PageSettings);
}
}
private void ShowInfo(PrinterSettings printSettings) {
var pageSettings = printSettings.DefaultPageSettings;
var pageSize = pageSettings.PaperSize;
label1.Text = string.Format("打印机:{1}{0}纸张类型:{2}{0}纸张⼤⼩:{3}cm * {4}cm{0}纸张布局:{5}",
Environment.NewLine + Environment.NewLine,
printSettings.PrinterName,
pageSize.PaperName,
HundredthsInchToCM(pageSize.Width).ToString("F1"),
HundredthsInchToCM(pageSize.Height).ToString("F1"),
pageSettings.Landscape ? "横向" : "纵向");
}
private void ShowInfo(PageSettings pageSettings)
{
var pageSize = pageSettings.PaperSize;
label1.Text = string.Format("纸张类型:{1}{0}纸张⼤⼩:{2}cm * {3}cm{0}纸张布局:{4}{0}页边距:{5}",
Environment.NewLine + Environment.NewLine,
pageSize.PaperName,
HundredthsInchToCM(pageSize.Width).ToString("F1"),
HundredthsInchToCM(pageSize.Height).ToString("F1"),
pageSettings.Landscape ? "横向" : "纵向",
MarginToString(pageSettings.Margins));
}
private string MarginToString(Margins margins) {
return string.Format("左:{0}mm,上:{1}mm,右:{2}mm,下:{3}mm", HundredthsInchToMM(margins.Left), HundredthsInchToMM(margins.Top), HundredthsInchToMM(margins.Right), HundredthsInchToMM(margins.Bottom)); }
const decimal K = 2.54M;
/// <summary>
/// (百分之⼀)英⼨制,转毫⽶
/// </summary>
/// <returns></returns>
private int HundredthsInchToMM(decimal d) {
return (int)Math.Round((d / 100.00M) * K * 10);
}
/// <summary>
/// (百分之⼀)英⼨制,转厘⽶
/// </summary>
private decimal HundredthsInchToCM(decimal d)
{
return (d / 100.00M) * K;
}
}
}
这⾥有⼀个坑爹的地⽅,不管你如何选择Pager Size,最后返回的PageSettings⾥,PageSize 始终是A4纸的⼤⼩(如下图),这也是我强烈推荐⼤家尽量避免使⽤PageSettings的
理由
还有⼀个问题,如何在弹出这⼆个对话框时,默认就选中⼀些特定的值呢?这个容易,参见下⾯的代码:
/// <summary>
/// 打印机设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintSet_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.PrinterSettings = new PrinterSettings()
{
PrinterName = "pdfFactory Pro"
};
if (dlg.ShowDialog() == DialogResult.OK) {
ShowInfo(dlg.PrinterSettings);
}
}
这样,在弹出打印机设置界⾯时,就默认选中了"pdfFactory Pro"打印机(当然,这个名字必须确实是你本机安装的打印机之⼀,如果这个打印机名字不存在,系统仍将选中默认
打印机)
/// <summary>
/// 页⾯设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPageSet_Click(object sender, EventArgs e)
{
PageSetupDialog dlg = new PageSetupDialog();
dlg.PageSettings = new PageSettings()
{
PaperSize = new PaperSize()
{
PaperName = "A5",//其实这⾥不管设置什么名称,都没啥⽤
//⼀旦设置初始尺⼨⼤⼩后,在页⾯设置对话框⾥,不管选什么纸张Size,
//对话框关闭时,返回值的PageSize⾥,始终就是这个⼤⼩,不会随⽤户选择⽽改变
Height = 827,
Width = 583
},
Landscape = false,
Margins = new Margins() { Left = 10, Top = 20, Bottom = 30, Right = 40 }
};
if (dlg.ShowDialog() == DialogResult.OK)
{
ShowInfo(dlg.PageSettings);
}
}
这样,页⾯设置对话框弹出时,就默认选中了布局⽅向为“纵向”,同时设置了边距,⽽且纸张⼤⼩为A5的⼤⼩(注意:PageSize的设置,在界⾯上看不出效果,但是关闭对话框
后,返回值的PageSize⾥会起作⽤)
三、RDLC报表⽤代码指定打印机、纸张⼤⼩、打印份数
rdlc报表的预览控制ReportViewer⾥,已经对外暴露了PrinterSettings属性
//
// Summary:
// Gets or sets a System.Drawing.Printing.PrinterSettings object that contains
// the settings for the default printer and print options that are used to initialize
// the Print dialog and the printer's Preferences dialog.
//
// Returns:
// A System.Drawing.Printing.PrinterSettings object.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public PrinterSettings PrinterSettings { get; set; }
以及PageSettings的set/get⽅法
//
// Summary:
/
/ Returns the page settings that are used to print the current report in the
// ReportViewer control or display it in print layout mode.
//
// Returns:
// A System.Drawing.Printing.PageSettings object that contains the page settings
// that are used to print the current report in the ReportViewer control or
// display it in print layout mode, or null if the ReportViewer control has
// not yet processed a report.
public PageSettings GetPageSettings();
//
// Summary:
/
/ Sets the page settings that are used to print the current report in the ReportViewer
// control or display it in print layout mode.
//
// Parameters:
// pageSettings:
// A System.Drawing.Printing.PageSettings object that contains the new page
// settings.This parameter must not be null.
public void SetPageSettings(PageSettings pageSettings);
利⽤这些信息,就⾜够控制RDLC的打印机和纸张⼤⼩了,⽐如要指定某台打印机打印,可以这么做:
{
PrinterName = "pdfFactory Pro"
};
要指定RDLC打印时,采⽤特定的纸张⼤⼩,可以参考下⾯的代码:
var pageSettings = portViewer1.GetPageSettings();
pageSettings.PaperSize = new PaperSize()
{
Height = 100,
Width = 50
};
pageSettings.Landscape = true;
打印份数的控制很简单:PrinterSettings类有⼀个Copies属性,直接给它赋值⼀个short型数字就⾏了
最后再回到本⽂最开头提出的问题,知道如何⽤代码影响打印机、纸张⼤⼩后,最后可以将PageSettings以及PrinterSettings中的关键信息,持久化存储起来(⽐如:⽤xml或DB),下次再进⼊该业务界⾯时,根据存储的配置信息还原相关的设置,这样⽤户只要设置⼀次,以后就不⽤每次⼿动切换打印机或纸张类型了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论