c#打印问题
打印时打印的图像超出打印纸张大小很多,问什么呢
class dayin
{
private DataTable dataTable = null;
private string bingrenxm = "";
public void Print(string sfbh)
{
mzms.cs.fyp getdt = new mzms.cs.fyp();
dataTable= getdt.getPrintSfmx(sfbh);
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
//设置边距
Margins margin = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = margin;
////纸张设置默认
PaperSize pageSize = new PaperSize("医药",472 , 1096);
pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
ppd.Document = pd;
if (ppd.ShowDialog() == DialogResult.OK)
{
try
{
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
}
}
}
//打印事件处理
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
。。。
string xmmx_mc = "名称";//项目明细名称
string xmmx_dw = "单位";//项目明细单位
string xmmx_dj = "单价";//项目明细单价
string xmmx_sl = "数量";//项目明细数量
string xmmx_je = "金额";//项目明细金额
if (dataTable != null)
{
mzbh = dataTable.Rows[0][4].ToString();
brxm = dataTable.Rows[0][5].ToString();
skyxm = dataTable.Rows[0][3].ToString();
fpbh = dataTable.Rows[0][1].ToString();
hj = "¥"+dataTable.Compute("sum(fee)", "").ToString();
mzms.cs.Money dx = new mzms.cs.Money(Convert.ToDouble(hj.Substring(1)));
daxie = dx.Convert();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
switch (dataTable.Rows[i]["lb"].ToString().Trim())
{
case "诊疗费":
dec_zhilf = dec_zhilf +Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "中草药":
dec_zcyf = dec_zcyf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "中成药":
dec_zchenyf = dec_zchenyf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "西药":
dec_xyf = dec_xyf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "拍片、透视费":
dec_pptsf = dec_pptsf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "化验费":
dec_hyf = dec_hyf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "手术费":
dec_ssf = dec_ssf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "输血费":
dec_sxf = dec_sxf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "检查费":
dec_jcf = dec_jcf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "治疗费":
dec_zhilf = dec_zhilf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "观察床费":
dec_gccf = dec_gccf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
case "CT费":
dec_ctf = dec_ctf + Convert.ToDecimal(dataTable.Rows[i]["fee"]);
break;
}
}
zlf = (dec_zlf == 0) ? "" : "¥" + dec_zlf.ToString();
zcyf = (dec_zcyf == 0) ? "" : "¥" + dec_zcyf.ToString();
zchenyf = (dec_zchenyf == 0) ? "" : "¥" + dec_zchenyf.ToString();
xyf = (dec_xyf == 0) ? "" : "¥" + dec_xyf.ToString();
pptsf = (dec_pptsf == 0) ? "" : "¥" + dec_pptsf.ToString();
hyf = (dec_hyf == 0) ? "" : "¥" + dec_hyf.ToString();
ssf = (dec_ssf == 0) ? "" : "¥" + dec_ssf.ToString();
sxf = (dec_sxf == 0) ? "" : "¥" + dec_sxf.ToString();
jcf = (dec_jcf == 0) ? "" : "¥" + dec_jcf.ToString();
zhilf = (dec_zhilf == 0) ? "" : "¥" + dec_zhilf.ToString();
gccf = (dec_gccf == 0) ? "" : "¥" + dec_gccf.ToString();
ctf = (dec_ctf == 0) ? "" : "¥" + dec_ctf.ToString();
}
//读取图片模板
//ResourceManager manager = new ResourceManager(typeof(mzms.Properties.Resources));
//Image temp = (Image)manager.GetObject("print");
Image temp = Image.FromFile(@ConfigurationSettings.AppSettings["printurl"]);
GetResultIntoImage(ref temp, date, mzbh, brxm, skyxm, fpbh, zlf, zcyf, xyf, zchenyf, pptsf, hyf, ssf, sxf, jcf, zhilf, gccf, ctf, hj, daxie, xmmx_mc, xmmx_dw, xmmx_dj, xmmx_sl, xmmx_je, dataTable);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = temp.Width;
int height = temp.Height;
Rectangle destRect = new Rectangle(x, y,width, height);
e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
}
/// <summary>
/// 将收费结果填充到图片模板
/// </summary>
private void GetResultIntoImage(
ref Image temp,
string date,
string mzbh,
string brxm,
string skyxm,
string fpbh,
string zlf,
string zcyf,
string zchenyf,
string xyf,
string pptsf,
string hyf,
string ssf,
string sxf,
string jcf,
string zhilf,
string gccf,
string ctf,
string hj,
string daxie,
string xmmx_mc,
string xmmx_dw,
string xmmx_dj,
string xmmx_sl,
string xmmx_je,
DataTable dt
)
{
//读取图片模板
Graphics g = Graphics.FromImage(temp);
Font f = new Font("宋体", 8);
Font mf = new Font("宋体", 6);
Brush b = new SolidBrush(Color.DarkGray);
//填充数据到图片模板(位置要在制作图片模板的时候度量好)
//一级
g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
g.DrawString(brxm, f, b, 270, 460);
g.DrawString(zlf, f, b, 460, 590);
g.DrawString(zcyf, f, b, 460, 665);
g.DrawString(zchenyf, f, b, 460, 730);
g.DrawString(xyf, f, b, 460, 805);
g.DrawString(pptsf, f, b, 460, 875);
g.DrawString(hyf, f, b, 460, 945);
g.DrawString(ssf, f, b, 460, 1015);
g.DrawString(hj, f, b, 460, 1085);
g.DrawString(daxie, f, b, 460, 1150);
;
g.DrawString(sxf, f, b, 990, 590);
g.DrawString(jcf, f, b, 990, 665);
g.DrawString(zhilf, f, b, 990, 730);
g.DrawString(gccf, f, b, 990, 805);
g.DrawString(ctf, f, b, 990, 875);
g.DrawString(skyxm, f, b, 320, 1230);
g.DrawString(date, f, b, 680, 1260);
//二级
g.DrawString("门诊号" + mzbh, f, b, 460, 1510);
g.DrawString("姓名" + brxm, f, b, 240, 1510);
g.DrawString("发票号" + fpbh, f, b, 930, 1510);
g.DrawString(xmmx_mc, f, b, 240, 1680);
g.DrawString(xmmx_dw, f, b, 760, 1680);
g.DrawString(xmmx_dj, f, b, 870, 1680);
g.DrawString(xmmx_sl, f, b, 1035, 1680);
g.DrawString(xmmx_je, f, b, 1160, 1680);
int yy = 1740;
if (dt != null)
{
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
g.DrawString(dt.Rows[i]["fymc"].ToString(), mf, b, 162, yy);
g.DrawString(dt.Rows[i]["dw"].ToString(), mf, b, 760, yy);
g.DrawString(dt.Rows[i]["dj"].ToString(), mf, b, 870, yy);
g.DrawString(dt.Rows[i]["sl"].ToString(), mf, b, 1035, yy);
g.DrawString(dt.Rows[i]["fee"].ToString(), mf, b, 1160, yy);
yy = yy + 50;
}
yy = yy + 105;
g.DrawString("--------------------------------------", mf, b, 760, yy);
g.DrawString("合计", mf, b, 870, yy + 25);
g.DrawString(hj, mf, b, 1160, yy + 25);
}
}
//g.PageScale =0.33f;
g.ScaleTransform(0.33f,0.33f);
g.Dispose();
}
}
--------------------编程问答-------------------- 顶一个,期待高手! --------------------编程问答-------------------- 控制好模板
http://topic.csdn.net/u/20100604/19/ad2e3eea-f685-4df7-a4bf-c4cf48d3c6e3.html --------------------编程问答-------------------- 参考这个!
打印工具类!
http://www.codeproject.com/KB/miscctrl/PrintUtilityUserControls.aspx --------------------编程问答-------------------- 帮顶下 --------------------编程问答-------------------- 我怎么才能使在下面生成好的图片等比例的显示在打印纸上啊 --------------------编程问答-------------------- 一般打印还可,套打及特殊打印有点烦....支持一下 --------------------编程问答-------------------- 还是格式打印问题,
数据库格式打印可以借助标签设计与打印功能。 --------------------编程问答-------------------- 和你另外一个帖子是一样的。
以前的代码,供参考
--------------------编程问答-------------------- 好长。顶了 --------------------编程问答-------------------- 参考 --------------------编程问答-------------------- 还是泰哥来,我一般直接用的水晶报表加存储过程 --------------------编程问答-------------------- 貌似好难,帮忙顶一下!
补充:.NET技术 , C#