怎样将横向试卷变成竖向打印(试卷打印是横向怎么改成竖向)

怎样将横向试卷变成竖向打印(试卷打印是横向怎么改成竖向)

首页技巧更新时间:2024-07-27 21:48:17
实例说明

本实例使用C#代码实现了自定义横向或纵向打印功能。运行本实例,选择“横向打印”复选框,可以在该复选框上方看到横向打印的预览效果图,取消该复选框的选择,则可以在该复选框上方看到纵向打印的预览效果图。实例运行结果如图所示。

自定义横向或纵向打印 自定义横向或纵向打印

技术要点

本实例在设置横向或纵向打印时用到了Printdocument类的DefaultPageSettings.Landscape属性,下面对其进行详细讲解。

PrintDocument类主要用来定义Windows窗体应用程序进行打印时,将输出发送到打印机的可重用对象,其DefaultPageSettings.Landscape属性用来获取或设置一个值,该值指示是横向还是纵向打印该页,其语法格式如下:

public bool Landscape { get; set; }

属性值:如果页面横向打印,则为true;反之,则为false。默认值由打印机决定。

说明:PrintDocument类位于System.Drawing.Printing命名空间下。

例如,本实例中设置横向或纵向打印的关键代码如下:

private PrintDocument printdocument; //实例化打印文档对象 public static bool PageScape = false; //打印方向 public PrintClass(dataGridView datagrid, int PageS, bool lendscape) { …… //横向打印的设置 if (PageSheet >= 0) { if (lendscape == true) { printdocument.DefaultPageSettings.Landscape = lendscape; //横向打印 } else { printdocument.DefaultPageSettings.Landscape = lendscape; //纵向打印 } } …… } 开发步骤

(1)新建一个Windows应用程序,将其命名为PrintDirection,默认窗体为Form1。

(2)Form1窗体主要用到的控件及说明如表所示。

控件名称

属性设置

说 明

panel_Line

BorderStyle属性设置为FixedSingle

预览打印方向

CheckBox_Aspect

Text属性设置为“横向打印”

设置是否横向打印

comboBox_PageSize

选择打印纸张

button_Preview

Text属性设置为“打印预览”

执行打印预览操作

dataGridView1

显示要打印的数据

(3)主要程序代码。

Form1窗体获得焦点时,首先在Panel控件中绘制一个预览表格,代码如下:

private void Form1_Activated(object sender, EventArgs e) { Graphics g = panel_Line.CreateGraphics(); //在窗体中绘制一个预览表格 int paneW = panel_Line.Width; //设置表格的宽度 int paneH = panel_Line.Height; //设置表格的高度 g.DrawRectangle(new Pen(Color.WhiteSmoke, paneW), 0, 0, paneW, paneH); //绘制一个矩形 }

Form1窗体加载时,对DataGridView控件进行数据绑定,以显示要打印的数据,实现代码如下:

private void Form1_Load(object sender, EventArgs e) { comboBox_PageSize.SelectedIndex = 0; //设置默认纸张 //实例化SqlConnection数据库连接类对象 SqlConnection sqlcon = new SqlConnection("Data Source=(local);Database=Northwind;Uid=sa;Pwd=;"); SqlDataAdapter sqlda = new SqlDataAdapter("select * from Region", sqlcon); //实例化数据库桥接器对象 DataSet myds = new DataSet(); //实例化DataSet数据集对象 sqlda.Fill(myds); //填充DataSet数据集 dataGridView1.DataSource = myds.Tables[0]; //显示要打印的数据 }

当用户改变“横向打印”复选框的选中状态时,在Panel控件中绘制打印纸张的横向或纵向预览效果,实现代码如下:

private void checkBox_Aspect_MouseDown(object sender, MouseEventArgs e) { //改变窗体中预览表格的方向 int aspX = 0; //宽度 int aspY = 0; //高度 if (((CheckBox)sender).Checked == false) //如果不是纵向打印 { aspX = 136; //设置大小 aspY = 98; PrintClass.PageScape = true; //横向打印 } else { aspX = 100; //设置大小 aspY = 116; PrintClass.PageScape = false; //纵向打印 } panel_Line.Width = aspX; //设置控件的宽度 panel_Line.Height = aspY; //设置控件的高度 aspX = (int)((groupBox1.Width - aspX) / 2); //设置控件的Top panel_Line.Location = new Point(aspX, 90); //设置控件的位置 Form1_Activated(sender, e); //设用Activated事件 }

单击“打印预览”按钮,调用公共类PrintClass中的构造函数对打印信息进行设置,然后调用该类中的print方法打印数据。“打印预览”按钮的Click事件代码如下:

private void button_Preview_Click(object sender, EventArgs e) { //对打印信息进行设置 PrintClass dgp = new PrintClass(this.dataGridView1, comboBox_PageSize.SelectedIndex, checkBox_Aspect.Checked); MSetUp(dgp); //记录窗体中打印信息的相关设置 string[] header = new string[dataGridView1.ColumnCount]; //创建一个与数据列相等的字符串数组 for (int p = 0; p < dataGridView1.ColumnCount; p ) //记录所有列标题的名列 { header[p] = dataGridView1.Columns[p].HeaderCell.Value.ToString(); } dgp.print(); //显示打印预览窗体 }

上面的代码中用到公共类PrintClass中的构造函数和print方法,下面对它们进行详细讲解。PrintClass类的构造函数主要用来对打印信息进行初始化,它有3个参数,分别用来表示打印数据、纸张大小和是否横向打印。PrintClass类的构造函数实现代码如下:

#region 打印信息的初始化 /// <summary> /// 打印信息的初始化 /// </summary> /// <param datagrid="DataGridView">打印数据</param> /// <param PageS="int">纸张大小</param> /// <param lendscape="bool">是否横向打印</param> public PrintClass(DataGridView datagrid, int PageS, bool lendscape) { this.datagrid = datagrid; //获取打印数据 this.PageSheet = PageS; //纸张大小 printdocument = new PrintDocument(); //实例化PrintDocument类 pagesetupdialog = new PageSetupDialog(); //实例化PageSetupDialog类 pagesetupdialog.Document = printdocument; //获取当前页的设置 printpreviewdialog = new PrintPreviewDialog(); //实例化PrintPreviewDialog类 printpreviewdialog.Document = printdocument; //获取预览文档的信息 printpreviewdialog.FormBorderStyle = FormBorderStyle.Fixed3D; //设置窗体的边框样式 //横向打印的设置 if (PageSheet >= 0) { if (lendscape == true) { printdocument.DefaultPageSettings.Landscape = lendscape; //横向打印 } else { printdocument.DefaultPageSettings.Landscape = lendscape; //纵向打印 } } pagesetupdialog.Document = printdocument; //设置打印文档 printdocument.PrintPage = new PrintPageEventHandler(this.printdocument_printpage); //事件的重载 } #endregion

print方法为自定义的无返回值类型方法,该方法主要用来根据要打印的数据显示打印预览窗体。print方法实现代码如下:

#region 显示打印预览窗体 /// <summary> /// 显示打印预览窗体 /// </summary> public void print() { rowcount = 0; //记录数据的行数 string paperName = Page_Size(PageSheet); //获取当前纸张的大小 PageSettings storePageSetting = new PageSettings(); //实列化一个对PageSettings对象 foreach (PaperSize ps in printdocument.PrinterSettings.PaperSizes) //查找当前设置纸张 if (paperName == ps.PaperName) //如果找到当前纸张的名称 { storePageSetting.PaperSize = ps; //获取当前纸张的信息 } if (datagrid.DataSource.GetType().ToString() == "System.Data.DataTable") //判断数据类型 { rowcount = ((DataTable)datagrid.DataSource).Rows.Count; //获取数据的行数 } else if (datagrid.DataSource.GetType().ToString() == "System.Collections.ArrayList") //判断数据类型 { rowcount = ((ArrayList)datagrid.DataSource).Count; //获取数据的行数 } try { printdocument.DefaultPageSettings.Landscape = PageScape; //设置横向打印 pagesetupdialog.Document = printdocument; //设置打印文档 printpreviewdialog.ShowDialog(); //显示打印预览窗体 } catch (Exception e) { throw new Exception("printer error." e.Message); } } #endregion ,

大家还看了
也许喜欢
更多栏目

© 1998-2024 shitiku.com.cn,All Rights Reserved.