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 printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 60;//x坐标
int y = 50;//y坐标
Font fontNormalContent = new System.Drawing.Font("华文楷体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
Pen pen = new Pen(Color.Black, 1);
for (int i = 0; i < dt.Rows.Count; i++)
{
string wpmc = dt.Rows[i]["MC"].ToString();//名称
string wpgg = dt.Rows[i]["GG"].ToString();//规格
string wpdw = dt.Rows[i]["DW"].ToString();//单位
string wpsl = dt.Rows[i]["SL"].ToString();//数量
string wpdj = dt.Rows[i]["DJ"].ToString();//单价
string wpje = dt.Rows[i]["JE"].ToString();//金额
string sjxx = DateTime .Parse( dt.Rows[i]["DJSJ"].ToString()).ToShortDateString ();//时间
e.Graphics.DrawString(wpmc + " " + wpgg, fontNormalContent, Brushes.Black, x, y);
e.Graphics.DrawString(wpdw, fontNormalContent, Brushes.Black, x + 80, y);
e.Graphics.DrawString(wpsl, fontNormalContent, Brushes.Black, x + 115, y);
e.Graphics.DrawString(wpdj, fontNormalContent, Brushes.Black, x + 160, y);
//写入时间
if (i == 0)
{
string year = sjxx.Substring(2, 2);
string month = sjxx.Substring(5, 2);
string day = sjxx.Substring(8, 2);
e.Graphics.DrawString(year, fontNormalContent, Brushes.Blue, 150, 25);
e.Graphics.DrawString(month, fontNormalContent, Brushes.Blue, 170, 25);
e.Graphics.DrawString(day,fontNormalContent,Brushes.Blue ,190,25);
}
y += 30;
}