c#winform打印多页(可以多给分)

pengqhi 2009-09-19 08:10:52
namespace prt4
{
public partial class Form1 : Form
{
public int prpage=2;
private int p = 0;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
print();
}

private void button2_Click(object sender, EventArgs e)
{
print2();
}
private void print()
{
PrintDocument prndoc = new PrintDocument();
PrintPreviewDialog preddlg = new PrintPreviewDialog();
preddlg.Document = prndoc;
prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
preddlg.ShowDialog();
}
private void print2()
{
PrintDocument prndoc = new PrintDocument();
PrintDialog prndlg = new PrintDialog();
prndlg.Document = prndoc;
prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
prndlg.ShowDialog();
}

private void OnPrintPage(object sender, PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
ppea.HasMorePages = false;
while (prpage >= 1)
{
painttable(grfx, p, ppea);
p++;
if (prpage == 1)
{
ppea.HasMorePages = false;
prpage--;
return;
}
else if (prpage > 1)
{
ppea.HasMorePages = true;
prpage--;
return;
}
}
}
private void painttable(Graphics grfx, int k, PrintPageEventArgs ppea)
{
Pen pen = new Pen(Color.Black, 1);
Pen pen2 = new Pen(Color.Black, 2);
Rectangle rect = new Rectangle(mtm(15), mtm(40), mtm(181), mtm(228));
grfx.DrawRectangle(pen2, rect);
Font font2 = new Font("宋体", 14);
Brush brush = Brushes.Black;
PointF point2 = new PointF();
fillcaption(grfx, k, ppea);
point2.X = mtm(17);
point2.Y = mtm(42);
string str = k.ToString();
grfx.DrawStr1ng(str, font2, brush, point2);
}
private void fillcaption(Graphics grfx, int a, PrintPageEventArgs ppea)
{
Font font = new Font("楷体_GB2312", 22);
Pen pen = new Pen(Color.Black, 2);
Brush brush = Brushes.Black;
string str = "学生花名册";
SizeF sizef = grfx.MeasureString(str, font);
grfx.DrawString(str, font, brush, (grfx.VisibleCl1pBounds.Width - sizef.Width) / 2, mtm(12));
}
private int mtm(double mm)
{
double foot = 304.8; //1 foot equal 304.8 mm
double inch = foot / 12f;
double atomUnit = inch / 100;
return Convert.ToInt32(mm / atomUnit);
}
为什么上面和程序什么也打印不出来,
预览时,有两页,单击预览对话框左上角的打印,只打印一张空白纸出来,明明预览看到有内容啊,这是什么么?
帮帮我修改一下,可以多给分哈
...全文
535 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuan_kang 2009-09-27
  • 打赏
  • 举报
回复
asd
特别 2009-09-20
  • 打赏
  • 举报
回复

namespace Steel
{
public partial class Form1 : Form
{
public int prpage = 2;
private int p = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender, EventArgs e )
{
print();
}

private void button2_Click( object sender, EventArgs e )
{
print2();
}

private void print()
{
PrintDocument prndoc = new PrintDocument();
PrintPreviewDialog preddlg = new PrintPreviewDialog();
preddlg.Document = prndoc;
prndoc.PrintPage += new PrintPageEventHandler( OnPrintPage );

//每次打印完成后要重置这两个变量,以备下次打印时使用
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prndoc.EndPrint += new PrintEventHandler( prndoc_EndPrint );

preddlg.ShowDialog();

}

void prndoc_EndPrint( object sender, PrintEventArgs e )
{
//每次打印完成后要重置这两个变量,以备下次打印时使用
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prpage = 2;
p = 0;
}

private void print2()
{
PrintDocument prndoc = new PrintDocument();
PrintDialog prndlg = new PrintDialog();
prndlg.Document = prndoc;
prndoc.PrintPage += new PrintPageEventHandler( OnPrintPage );

//每次打印完成后要重置这两个变量,以备下次打印时使用
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prndoc.EndPrint += new PrintEventHandler( prndoc_EndPrint );

prndlg.ShowDialog();
prndoc.Print();
}

private void OnPrintPage( object sender, PrintPageEventArgs ppea )
{
Graphics grfx = ppea.Graphics;
ppea.HasMorePages = false;
while ( prpage >= 1 )
{
painttable( grfx, p, ppea );
p++;
if ( prpage == 1 )
{
ppea.HasMorePages = false;
prpage--;
return;
}
else if ( prpage > 1 )
{
ppea.HasMorePages = true;
prpage--;
return;
}
}
}
private void painttable( Graphics grfx, int k, PrintPageEventArgs ppea )
{
Pen pen = new Pen( Color.Black, 1 );
Pen pen2 = new Pen( Color.Black, 2 );
Rectangle rect = new Rectangle( mtm( 15 ), mtm( 40 ), mtm( 181 ), mtm( 228 ) );
grfx.DrawRectangle( pen2, rect );
Font font2 = new Font( "宋体", 14 );
Brush brush = Brushes.Black;
PointF point2 = new PointF();
fillcaption( grfx, k, ppea );
point2.X = mtm( 17 );
point2.Y = mtm( 42 );
string str = k.ToString();
grfx.DrawString( str, font2, brush, point2 );
}
private void fillcaption( Graphics grfx, int a, PrintPageEventArgs ppea )
{
Font font = new Font( "楷体_GB2312", 22 );
Pen pen = new Pen( Color.Black, 2 );
Brush brush = Brushes.Black;
string str = "学生花名册";
SizeF sizef = grfx.MeasureString( str, font );
grfx.DrawString( str, font, brush, ( grfx.VisibleClipBounds.Width - sizef.Width ) / 2, mtm( 12 ) );

}
private int mtm( double mm )
{
double foot = 304.8; //1 foot equal 304.8 mm
double inch = foot / 12f;
double atomUnit = inch / 100;
return Convert.ToInt32( mm / atomUnit );
}
}
}

楼主这样写法有点不对劲
建议将 PrintDocument、PrintDialog等打印对象的初始化放到构造函数中去。

freeboy827 2009-09-20
  • 打赏
  • 举报
回复


public void Print()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
this.printdoc.Print();
}
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
printFont = new Font("Trebuchet MS", 7);
string date =datetime.now.ToString("yyMM");
DrawString(e, date, GetPoint(0.55m + (0.8m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));

}
private void DrawString(System.Drawing.Printing.PrintPageEventArgs e, string s, Point point)
{
e.Graphics.DrawString(s, printFont, brush, GetRect(point));
}

这个是没有打印预览窗体的打印,你试试
pengqhi 2009-09-20
  • 打赏
  • 举报
回复
pengqhi 2009-09-20
  • 打赏
  • 举报
回复
我设置不了页面呢,
再求设置页面代码,谢谢
pengqhi 2009-09-19
  • 打赏
  • 举报
回复
在线等,帮帮我吧

110,568

社区成员

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

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

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