110,534
社区成员
发帖
与我相关
我的任务
分享
public class PrintBonus//打印分红
{
private System.Drawing.Printing.PrintDocument printdoc;
private decimal decX = 0.4m;
private decimal decY = 0.4m; //修正
System.Drawing.Font printFont = new Font("Trebuchet MS", 7);//默认的数字字体
System.Drawing.SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
FormShareHolderManage frm = new FormShareHolderManage();
//输入厘米转换成像素
private Point GetPoint(Decimal x, Decimal y)
{
return new Point((int)(x * 0.3937m * 100), (int)(y * 0.3937m * 100));
}
private RectangleF GetRect(Point point)
{
return new RectangleF(point.X, point.Y, (float)(15 * 0.3937m * 100), printFont.Height + 1);
}
private void DrawString(System.Drawing.Printing.PrintPageEventArgs e, string s, Point point)
{
e.Graphics.DrawString(s, printFont, brush, GetRect(point));
}
int page = 0;//开始打印的页数
int line = 0;//开始打印的行数
DataTable printData = null;
decimal offset = 0;
public PrintBonus(int page, int line, DataTable pd , decimal os)
{
this.page = page;
this.line = line;
this.printData = pd;
offset = os;
}
public void PrintPreview()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
PrintPreviewDialog cppd = new PrintPreviewDialog();
cppd.Document = printdoc;
cppd.ShowDialog();
}
public void Print()
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
this.printdoc.Print();
}
int cLine = 0;//计数器
int count = 0;
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 && printData.Rows.Count > cLine)
{
decimal d = 9.35m + Convert.ToDecimal((count + line) * 0.38);
if (page % 2 == 0)
{
d = 1.9m + Convert.ToDecimal((count + line) * 0.38);
}
//string date = Convert.ToDateTime(printData.Rows[count]["日期"]).ToString("yyMM");//居中对齐
//DrawString(e, date, GetPoint(0.9m + (1.1m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
//string amount = printData.Rows[count]["股份数量"].ToString();//右对齐
//DrawString(e, amount, GetPoint(3.6m - frm.GetFontWidth(printFont, amount) - decX, d - decY));
//string bonus = printData.Rows[count]["每股分红"].ToString();
//DrawString(e, bonus, GetPoint(3.7m + (1.6m - frm.GetFontWidth(printFont, bonus)) / 2 - decX, d - decY));
//string total = printData.Rows[count]["分红总额"].ToString();
//DrawString(e, total, GetPoint(6.9m - frm.GetFontWidth(printFont, total) - decX, d - decY));
string tax = printData.Rows[count]["扣税"].ToString();
if (tax == "0")
{
DrawString(e, "", GetPoint(7.5m - decX, d - decY));
}
else
{
DrawString(e, tax, GetPoint(8.5m - frm.GetFontWidth(printFont, tax) - decX, d - decY + offset));
}
string aTax = printData.Rows[count]["税后分红"].ToString();
// DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY));
DrawString(e, aTax, GetPoint(9.3m - frm.GetFontWidth(printFont, aTax) - decX, d - decY + offset));
//DrawString(e, printData.Rows[count]["备注"].ToString(), GetPoint(10.35m - decX, d - decY));
count++;
cLine++;
}
if (printData.Rows.Count > cLine)
{
e.HasMorePages = true;
line = 1;
page++;
}
else
{
e.HasMorePages = false;
}
}
}
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
while (count + line - 1 < 12 && printData.Rows.Count > cLine)
{
decimal d = 9.25m + Convert.ToDecimal((count + line) * 0.38);
if (page != 1)
{
if (page % 2 == 0)
{
d = 1.9m + Convert.ToDecimal((count + line) * 0.38);
}
else
{
d = 9.35m + Convert.ToDecimal((count + line) * 0.38);
}
}
printFont = new Font("Trebuchet MS", 7);
string date =Convert.ToDateTime(printData.Rows[count]["日期"]).ToString("yyMM");
DrawString(e, date, GetPoint(0.55m + (0.8m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
string optionA = printData.Rows[count]["***"].ToString();//右对齐
DrawString(e, optionA, GetPoint(2.4m - frm.GetFontWidth(printFont, optionA) - decX, d - decY));
string optionL = printData.Rows[count]["***"].ToString();
DrawString(e, optionL, GetPoint(3.5m - frm.GetFontWidth(printFont, optionL) - decX, d - decY));
string gpA = printData.Rows[count]["***"].ToString();
DrawString(e, gpA, GetPoint(4.65m - frm.GetFontWidth(printFont, gpA) - decX, d - decY));
string stockP = printData.Rows[count]["***"].ToString();
DrawString(e, stockP, GetPoint(4.7m + (1.1m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));
string sAmount = printData.Rows[count]["***"].ToString();
DrawString(e, sAmount, GetPoint(6.9m - frm.GetFontWidth(printFont, sAmount) - decX, d - decY));
string gpL = printData.Rows[count]["***"].ToString();
DrawString(e, gpL, GetPoint(8.4m - frm.GetFontWidth(printFont, gpL) - decX, d - decY));
string optoinS = printData.Rows[count]["***"].ToString();
DrawString(e, optoinS, GetPoint(9.9m - frm.GetFontWidth(printFont, optoinS) - decX, d - decY));
printFont = new Font("宋体", 7);
DrawString(e, printData.Rows[count]["***"].ToString(), GetPoint(10.05m - decX, d - decY));
count++;
cLine++;
}
if (printData.Rows.Count > cLine)
{
e.HasMorePages = true;
line = 1;
page++;
}
else
{
e.HasMorePages = false;
}
}
DialogResult result = printDialog1.ShowDialog();
// 打印
if (result == DialogResult.OK)
{
printDocument1.Print();
}