C# printdocument 打印问题

fsh1985 2011-07-16 11:03:22
我做的是标签打印,数据库只有100条数据的话,第一页打满的话,要实现接着下一页打。
我下面,这样是的话,变死循环且,都是打印第一页的
strCon = "server=192.168.1.57;uid=sa;pwd=123;database=LonkingJiXie";
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = strCon;
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand("select * from TestTM", sqlCon);

SqlDataReader sqlDR = sqlCom.ExecuteReader();
Graphics g = e.Graphics;
Font drawLGFont = new Font("方正艺黑简体", 20);
Font drawFont = new Font("宋体", 14);
SolidBrush drawBrush = new SolidBrush(Color.Black);

int Y = 0;

while (sqlDR.Read())//循环读取每一行
{
WLcode = sqlDR[0].ToString();
WLname = sqlDR[1].ToString();
TMcode = sqlDR[2].ToString();

e.Graphics.Clip = new Region(new Rectangle(10, 2, 200, 500));
g.PageUnit = GraphicsUnit.Millimeter;


g.DrawString("标签打印测试", drawLGFont, drawBrush, 10, 5+Y);

SizeF FZ = g.MeasureString("标签打印测试", drawFont, e.MarginBounds.Width);

g.DrawString(WLname + ":", drawFont, drawBrush, 10, 10 + FZ.Height+Y);
SizeF SZ = g.MeasureString(WLname + ":", drawFont, e.MarginBounds.Width);

g.DrawString(WLcode, drawFont, drawBrush, 10 + SZ.Width, 10+ SZ.Height+Y);

SizeF TZ = g.MeasureString(WLcode + ":", drawFont, e.MarginBounds.Width);

//Pen penLine = new Pen(new SolidBrush(Color.Black), 0.2f);
//g.DrawLine(penLine, 10, 10, 190, 10);
//g.DrawLine(penLine, new PointF(20.0f, Convert.ToSingle(20.0f + dd.Height)), new PointF(Convert.ToSingle(20.0f + 2 * dd.Width), Convert.ToSingle(20.0f + dd.Height)));
//g.DrawRectangle(penLine, 10,10, 190, 50); //左,上,右,下 坐标,高、宽


Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 10);

System.Drawing.Image _CodeImage = _Code39.GetCodeImage(TMcode, Code39.Code39Model.Code39Normal, true);
g.DrawImage(_CodeImage, 10, 20 + TZ.Height+Y);
Y = Y + 40;



}


e.HasMorePages = true; //这边这样加的话变,死循环,预览时,都是第一页的内容



sqlDR.Dispose();
sqlCom.Dispose();
sqlCon.Dispose();
sqlCon.Close();
...全文
390 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fsh1985 2011-07-21
  • 打赏
  • 举报
回复
对了,还有个问题请教你,.net web上要打印的话要怎么实现,我试了还像
打印预览,和打印设置 这些类都没有
这个你清楚不》?还是在哪个命名空间下
fsh1985 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 mjp1234airen4385 的回复:]
这个要通过总的记录数和每页的记录数,算出总共有多少页。
rowcount = ds.Tables[0].Rows.Count;
pagecount = 10;
//count就是总页数
count = rowcount / pagecount;
if(rowcount % pagecount > 0) count++;

if (pageindex < count)e.HasMor……
[/Quote]

兄弟,你太牛了,谢谢
sdl2005lyx 2011-07-21
  • 打赏
  • 举报
回复
学习!
mjp1234airen4385 2011-07-21
  • 打赏
  • 举报
回复
这个要通过总的记录数和每页的记录数,算出总共有多少页。
rowcount = ds.Tables[0].Rows.Count;
pagecount = 10;
//count就是总页数
count = rowcount / pagecount;
if(rowcount % pagecount > 0) count++;

if (pageindex < count)e.HasMorePages = true;
else e.HasMorePages = false;

pageindex += 1;
mjp1234airen4385 2011-07-20
  • 打赏
  • 举报
回复
int pagecount = 10;//每页10条记录。
int pageindex = 0;//这是第0页.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

//循环输出每一页的数据
for (int i = pageindex * 10; i < (pageindex + 1) * 10; i++)
{
string s = DSTemp.Tables[0].Rows[i][0].ToString();
e.Graphics.DrawString(s, this.Font,
new SolidBrush(this.ForeColor), new PointF(10, 10 + i * 20));
}
if ((pageindex + 1)* pagecount < DSTemp.Tables[0].Rows.Count) e.HasMorePages = true;
else e.HasMorePages = false;

pageindex++;
}



int pagecount = 10;//每页10条记录。
int pageindex = 0;//这是第0页.

这两句要放在print事件的外部,是全局变量。
mjp1234airen4385 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fsh1985 的回复:]
我觉得应该是通过
e.MarginBounds.Left;
e.MarginBounds.Top;
e.MarginBounds.Height;
e.MarginBounds.Width;
e.PageBounds.Width;
e.PageBounds.Height;
e.PageBounds.Left;
e.PageBounds.Top;
……
[/Quote]

这些是控制页边距的和页面尺寸的。
fsh1985 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 mjp1234airen4385 的回复:]
引用 8 楼 fsh1985 的回复:
我觉得应该是通过
e.MarginBounds.Left;
e.MarginBounds.Top;
e.MarginBounds.Height;
e.MarginBounds.Width;
e.PageBounds.Width;
e.PageBounds.Height;
e.PageBounds.Left;
e.PageBounds.Top……
[/Quote]

兄弟,谢了,我现在更了解,还有那个循环里要加 if (i > (ds.Tables[0].Rows.Count - 1))
break;
这样就不会出错了,不过还有个问题就是 会多一页空白页。 这个能要怎么个调整下
fsh1985 2011-07-20
  • 打赏
  • 举报
回复
private void prtDoc_PrintPage(object sender, PrintPageEventArgs e)
{

Graphics g = e.Graphics;
Font drawLGFont = new Font("方正艺黑简体", 20);
Font drawFont = new Font("宋体", 14);
SolidBrush drawBrush = new SolidBrush(Color.Black);


//循环输出每一页的数据
int Y = 0;


//(int i = pageindex * 10; i < (pageindex + 1) * 10; i++)

for (int i = pageindex * 10; i < (pageindex + 1) * 10; i++)
{




e.Graphics.Clip = new Region(new Rectangle(10, 2, 200, 500));
g.PageUnit = GraphicsUnit.Millimeter;


g.DrawString("Lonking 龙工", drawLGFont, drawBrush, 10, 5 + Y);

SizeF FZ = g.MeasureString("Lonking 龙工", drawFont, e.MarginBounds.Width);

g.DrawString(ds.Tables[0].Rows[pageindex]["ItemName"].ToString() + ":", drawFont, drawBrush, 10, 10 + FZ.Height + Y);
SizeF SZ = g.MeasureString(ds.Tables[0].Rows[pageindex]["ItemName"].ToString() + ":", drawFont, e.MarginBounds.Width);

g.DrawString(ds.Tables[0].Rows[pageindex]["Itemcode"].ToString(), drawFont, drawBrush, 10 + SZ.Width, 10 + SZ.Height + Y);

SizeF TZ = g.MeasureString(ds.Tables[0].Rows[pageindex]["Itemcode"].ToString() + ":", drawFont, e.MarginBounds.Width);

//Pen penLine = new Pen(new SolidBrush(Color.Black), 0.2f);
//g.DrawLine(penLine, 10, 10, 190, 10);
//g.DrawLine(penLine, new PointF(20.0f, Convert.ToSingle(20.0f + dd.Height)), new PointF(Convert.ToSingle(20.0f + 2 * dd.Width), Convert.ToSingle(20.0f + dd.Height)));
//g.DrawRectangle(penLine, 10,10, 190, 50); //左,上,右,下 坐标,高、宽


Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 10);

System.Drawing.Image _CodeImage = _Code39.GetCodeImage(ds.Tables[0].Rows[i]["TM"].ToString(), Code39.Code39Model.Code39Normal, true);
g.DrawImage(_CodeImage, 10, 20 + TZ.Height + Y);
Y = Y + 40;

}


if (pageindex * pagecount < ds.Tables[0].Rows.Count)
e.HasMorePages = true;
else e.HasMorePages = false;

pageindex += 1;





}


报错提示: 在位置 108 处没有任何行。 这个是表里的最后一行
fsh1985 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 mjp1234airen4385 的回复:]
int pagecount = 10;//每页10条记录。
int pageindex = 0;//这是第0页.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

//循环输出每一页的数据
for (in……
[/Quote]

------------------------这样写,还是有问题,最后一行是会报错
提示:在位置 108 处没有任何行。


oneatree 2011-07-19
  • 打赏
  • 举报
回复
mark
fsh1985 2011-07-19
  • 打赏
  • 举报
回复
我觉得应该是通过
e.MarginBounds.Left;
e.MarginBounds.Top;
e.MarginBounds.Height;
e.MarginBounds.Width;
e.PageBounds.Width;
e.PageBounds.Height;
e.PageBounds.Left;
e.PageBounds.Top;
这些属性怎么控制的吧》?
ZOUWENT1 2011-07-19
  • 打赏
  • 举报
回复
的顶顶顶顶顶顶顶顶顶顶
fsh1985 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fsh1985 的回复:]
这个怎么控制,我对这些类不熟
以前没用过,
[/Quote]
Graphics g = e.Graphics;
Font drawLGFont = new Font("方正艺黑简体", 20);
Font drawFont = new Font("宋体", 14);
SolidBrush drawBrush = new SolidBrush(Color.Black);

int pagecount = 10;//每页10条记录。
int pageindex = 0;//这是第0页.
//循环输出每一页的数据


for (int i = pageindex * 10; i < (pageindex + 1) * 10; i++)
{

int Y=0;

e.Graphics.Clip = new Region(new Rectangle(10, 2, 200, 500));
g.PageUnit = GraphicsUnit.Millimeter;


g.DrawString("Lonking 龙工", drawLGFont, drawBrush, 10, 5 + Y);

SizeF FZ = g.MeasureString("Lonking 龙工", drawFont, e.MarginBounds.Width);

g.DrawString(ds.Tables[0].Rows[i]["ItemName"].ToString() + ":", drawFont, drawBrush, 10, 10 + FZ.Height + Y);
SizeF SZ = g.MeasureString(ds.Tables[0].Rows[i]["ItemName"].ToString() + ":", drawFont, e.MarginBounds.Width);

g.DrawString(ds.Tables[0].Rows[i]["Itemcode"].ToString(), drawFont, drawBrush, 10 + SZ.Width, 10 + SZ.Height + Y);

SizeF TZ = g.MeasureString(ds.Tables[0].Rows[i]["Itemcode"].ToString() + ":", drawFont, e.MarginBounds.Width);

//Pen penLine = new Pen(new SolidBrush(Color.Black), 0.2f);
//g.DrawLine(penLine, 10, 10, 190, 10);
//g.DrawLine(penLine, new PointF(20.0f, Convert.ToSingle(20.0f + dd.Height)), new PointF(Convert.ToSingle(20.0f + 2 * dd.Width), Convert.ToSingle(20.0f + dd.Height)));
//g.DrawRectangle(penLine, 10,10, 190, 50); //左,上,右,下 坐标,高、宽


Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 10);

System.Drawing.Image _CodeImage = _Code39.GetCodeImage(ds.Tables[0].Rows[i]["Itemcode"].ToString(), Code39.Code39Model.Code39Normal, true);
g.DrawImage(_CodeImage, 10, 20 + TZ.Height + Y);
Y = Y + 40;


}
if (pageindex * pagecount < ds.Tables[0].Rows.Count)
e.HasMorePages = true;
else e.HasMorePages = false;

pageindex++;


改成这样还是有问题的,死循环。兄弟可以帮我看看?


你有没案例发个给我看看,邮箱fsh1985@163.com
fsh1985 2011-07-19
  • 打赏
  • 举报
回复
谢谢,4楼兄弟,我试试看
mjp1234airen4385 2011-07-18
  • 打赏
  • 举报
回复
比如你取得100条数据,这个最好在打印代码外面完成。
不要把取数据的过程每次都在打印代码里执行,浪费。
每一页你要打印10条pagecount,那么,你要记下,
当前是第几页pageindex,等打印下一页的时候,
就从这个记录开始,而不是从第一条记录开始。
等你打印完100条数据了,你要设置 e.HasMorePages = false;
每次都设置为true,当然是死循环了。
例如:
DataSet DS = new DataSet(); //这里是100条数据.不要放在打印事件里。打印之前必须取得数据
int pagecount = 10;//每页10条记录。
int pageindex = 0;//这是第0页.
//循环输出每一页的数据
for(int i = pageindex * 10; i < (pageindex + 1) * 10; i++)
{
e.Graphics.drawstring(DS.tables[0].rows[i]["shuju"].tostring());
}
if(pageindex * pagecount < ds.tables[0].rows.count) e.HasMorePages = true;
else e.HasMorePages = false;

pageindex++;

fsh1985 2011-07-16
  • 打赏
  • 举报
回复
这个怎么控制,我对这些类不熟
以前没用过,
sugarbelle 2011-07-16
  • 打赏
  • 举报
回复
打印前,设置一下PrintController
printDocument.PrintController = new System.Drawing.Printing.StandardPrintController();

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧