c# printdocument问题

zestsnake 2018-08-29 11:13:00
根据客户要求 要打印一批收据,连续纸,每次打印的张数不定。从EXCEL中读取数据。
已经自定义了纸张。但每打印一张都会跳空一些距离,没法打印在自定义格式上


private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("请先导入数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

//先测试下预览
//一英寸=2.54厘米,这里是百分之一英寸,目前的纸张是长(width)21厘米,高10.16(lenth)厘米 826.7 , 400
PrintDocument printDocument3 = new PrintDocument();
printDocument3.DefaultPageSettings.PaperSize = new PaperSize("pj", _width, _length); //354,472
printDocument3.OriginAtMargins = true;
printDocument3.DefaultPageSettings.Margins.Left = _left;
printDocument3.DefaultPageSettings.Margins.Top = _top;
printDocument3.DefaultPageSettings.Margins.Bottom = _bottom;
printDocument3.PrintPage += new PrintPageEventHandler(this.my_print);

printPreviewDialog1.Document = printDocument3;


DialogResult result = printPreviewDialog1.ShowDialog();
}



private void my_print(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int aa = 0;

int jls;
//int a1 = 1, a2 = 14, a3 = 14, a4 = 26;
int zys; //总页数
int ys = 1; //当前页数
//

//string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
// XmlDocument xml = new XmlDocument();

// xml.Load(path+ "\\config.xml");

//string path = System.IO.Directory.GetCurrentDirectory();
//string path = System.IO.Directory.GetCurrentDirectory();
string path = System.Windows.Forms.Application.StartupPath;
XmlDocument xml = new XmlDocument();

xml.Load(path + "\\config.xml");


XmlNode paper_size = xml.SelectSingleNode("config"); //指向根接点

XmlNode z_a1 = paper_size.SelectSingleNode("a1");
XmlNode z_a2 = paper_size.SelectSingleNode("a2");
XmlNode z_a3 = paper_size.SelectSingleNode("a3");
XmlNode z_a4 = paper_size.SelectSingleNode("a4");

XmlNode z_a5 = paper_size.SelectSingleNode("a5");
XmlNode z_a6 = paper_size.SelectSingleNode("a6");
XmlNode z_a7 = paper_size.SelectSingleNode("a7");


int a1 = Convert.ToInt16(z_a1.InnerText.ToString().Trim());
int a2 = Convert.ToInt16(z_a2.InnerText.ToString().Trim());
int a3 = Convert.ToInt16(z_a3.InnerText.ToString().Trim());
int a4 = Convert.ToInt16(z_a4.InnerText.ToString().Trim());
int a5 = Convert.ToInt16(z_a5.InnerText.ToString().Trim());
int a6 = Convert.ToInt16(z_a6.InnerText.ToString().Trim());
int a7 = Convert.ToInt16(z_a7.InnerText.ToString().Trim());


jls = this.dataGridView1.Rows.Count;
zys = Convert.ToInt16(Math.Ceiling(Convert.ToDouble(jls) / 1.0)); //算下总页数


for (int m = 1; m <= 1; m++)
{

if (i == jls)
{
i = 0;
j = 0;

e.HasMorePages = false;
return; //这个是在循环体内,发现记录超了,强制出来
}



string s_name = this.dataGridView1.Rows[i].Cells[2].Value.ToString().Trim(); //姓名
string s_class = this.dataGridView1.Rows[i].Cells[7].Value.ToString().Trim(); //班级
string s_classyear = this.dataGridView1.Rows[i].Cells[5].Value.ToString().Trim(); //学年
string s_num = this.dataGridView1.Rows[i].Cells[20].Value.ToString().Trim(); //学号

string s_xf = "¥" + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[15].Value).ToString("N").Trim(); //学费

string s_zsf = "¥" + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[13].Value).ToString("N").Trim(); //住宿费
string s_dbf = "¥" + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[8].Value).ToString("N").Trim(); //代办费

string s_xj = "¥" + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[17].Value).ToString("N").Trim(); //合计

//string s_dx = NumGetStr(Convert.ToDouble(this.dataGridView1.Rows[i].Cells[17].Value.ToString().Trim())); //大写

string s_dx = CmycurD(Convert.ToDecimal(this.dataGridView1.Rows[i].Cells[17].Value.ToString().Trim())); //大写
e.Graphics.DrawString(s_year, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _year , a1);
e.Graphics.DrawString(s_month , new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black,_month , a1);
e.Graphics.DrawString(s_date, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black,_date , a1);
e.Graphics.DrawString(s_name, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black,_name, a2);
e.Graphics.DrawString(s_class, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black,_class, a2); //(左右,上下)
e.Graphics.DrawString(s_classyear, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _classyear, a2);
e.Graphics.DrawString(s_num, new Font(new FontFamily("宋体"),11), System.Drawing.Brushes.Black,_num, a2);
e.Graphics.DrawString(s_xf, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _xf, a3);
e.Graphics.DrawString(s_zsf, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _zsf, a4);
e.Graphics.DrawString(s_dbf, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _dbf, a5);
e.Graphics.DrawString(s_xj, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _xj, a6);
e.Graphics.DrawString(s_dx, new Font(new FontFamily("宋体"), 11), System.Drawing.Brushes.Black, _dx, a7);







j = j + 1;
i = i + 1;

/*
a1 = a1 + _everyadd;
a2 = a2 + _everyadd;
a3 = a3 + _everyadd;
a4 = a4 + _everyadd;
*/




}

if (i == jls)
{
i = 0;

j = 0; ///////////////77777777777777777777777
e.HasMorePages = false;
return;
}




if (j == 1)
{
j = 0;

e.HasMorePages = true;

}





}

...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
打印机里面定义一下纸张先
zestsnake 2018-08-30
  • 打赏
  • 举报
回复
引用 1 楼 xomix 的回复:
打印机里面定义一下纸张先
换了台打印机好了,原先是用pr70,换了台实达的1900好了

110,500

社区成员

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

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

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