http://chs.gotdotnet.com/quickstart/winforms/doc/WinFormsPrinting.aspx
解决方案 »
- 怎样通过跨页面删除GridView列表某一行记录。高手求救....
- 怎么取某行某列的值.注ds.Tables[0].Rows[0][0]取不到第一行第一列,
- 如果将 这个数据文件中的 0xd2 转为 0x00,是用Replace吗?
- 怎样禁止某个页面只有同时打一个??
- 怎么样在C#的代码里面改变水晶报表的标题.急...........
- 《MSDN WebCast网络广播系列讲座2005年全集 更新完成》(MSDN WebCast)[ISO]
- 文件被关闭了,在程序中如何能够知道??
- 浏览器页面回退的问题
- socket服务器端处理客户端发来的数据问题
- 出错处理的问题,谢谢
- 为什么这个Bitmap不能保存?
- 不明白!HTML和WEB窗体都有image及其他相同的控件,问他们能实现相同的功能吗?有区别吗?
能否改单位?
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/_gdiplus_types_of_coordinate_systems_about.htm
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemDrawingGraphicsUnitClassTopic.htm
而是打印每页的。
例程:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO; // <doc>
// <desc>
// TextFilePrintDocument 将流输出到打印机
//
// 注意:为了避免在关闭文件时出问题,
// 如果发生异常,此类仅使用一个流
// 并将它留给调用方,以打开该文件
// 进行打印
//
// </desc>
// </doc>
public class TextPrintDocument : PrintDocument { private Font printFont = null;
private StringReader streamToPrint = null;
private string overflowText = null; public TextPrintDocument(StringReader streamToPrint, Font printFont) : base () {
this.streamToPrint = streamToPrint ;
this.printFont = printFont;
} //重写 OnBeginPrint 以设置将要使用的字体
protected override void OnBeginPrint(PrintEventArgs ev) {
base.OnBeginPrint(ev) ; overflowText = null;
} //重写 OnPrintPage 以为文档提供打印逻辑
protected override void OnPrintPage(PrintPageEventArgs ev) { base.OnPrintPage(ev) ; float lpp = 0 ;
float yPos = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null; //算出每页的行数
//在事件上使用 MarginBounds 以达到此目的
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ; //如果有上页的溢出文本,则先处理它
while (count < lpp && overflowText != null) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
int linesPrinted = PrintLine(ev,overflowText, yPos);
count += linesPrinted;
} //现在,在文件上重复此操作以输出每行
//首先检查行数,以便看不到不打印的行
while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
int linesPrinted = PrintLine(ev,line, yPos);
count += linesPrinted;
} //如果有多行,则另外打印一页
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}
private int PrintLine(PrintPageEventArgs e, string text, float yStartPos) {
Graphics graphics = e.Graphics;
Margins margins = e.PageSettings.Margins;
StringFormat format = new StringFormat();
int lines;
int characters;
RectangleF rectangle = new RectangleF(margins.Left,
yStartPos,
e.MarginBounds.Width,
e.MarginBounds.Height); graphics.MeasureString(text, printFont, rectangle.Size, format, out characters, out lines); //如果字符数少于 string.length,则无法适合该页上完整的段落,
//因此打印可以看到的内容,将剩余的内容
//保存在下一页
if (characters < text.Length) {
overflowText = text.Substring(characters);
} else {
overflowText = null;
} graphics.DrawString(text, printFont, Brushes.Black, rectangle, format);
//处理空行
if (lines == 0 )
lines = 1; return lines;
} }
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Millimeter;
g.SmoothingMode = SmoothingMode.AntiAlias; //提高字体的质量
leftMargin = e.MarginBounds.Left;
topMargin = e.MarginBounds.Top;
rightMargin = e.MarginBounds.Right;
bottomMargin = e.MarginBounds.Bottom;可是为什么我取得的边距非常大?
得到的边距=默认单位取得的边距 * 毫米/默认单位
为什么?
为什么取边距时,不能用毫米做单位来取?
[C#]
public Rectangle MarginBounds {get;}属性值
表示边距以内的页面部分的矩形区域。尺寸数量级为百分之一英寸。