如何控制C#中printDocument组件打印?

goodbegin 2005-12-10 07:37:49
我用printDocument组件打印票据,调用DrawString方法时,e.Graphics.DrawString(drawString, drawFont, drawBrush,x, y, drawFormat)
此处的x,y值能否以毫米为计量单位?如果可以,该如何设置?如果不可以,怎样才能实现以毫米为单位的坐标并打印?谢谢
...全文
838 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangguochen 2005-12-11
  • 打赏
  • 举报
回复
可以的,设置一下e.Graphics.PageUnit属性再转换一下就可以了
xjaifly 2005-12-11
  • 打赏
  • 举报
回复
另外 你还要将 你设置好的label位置 保存 下次调用就不用修改!
xjaifly 2005-12-11
  • 打赏
  • 举报
回复
我的处理是这样做UseControl 在上面放pictureBox
将你的要打印的事物 加入其中
再在图片上放Label
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
double topMargin = e.MarginBounds.Top;

// 获取图片的分辨率(像素/英寸)
double HResolution = this.pictureBox.Image.HorizontalResolution; // 水平分辨率
double VResolution = this.pictureBox.Image.VerticalResolution; // 垂直分辨率

// 设置打印单位为毫米
e.Graphics.PageUnit = GraphicsUnit.Millimeter;

// 底版图片大小(1/100英寸为单位)
int imageWidth = (int)(this.pictureBox.Image.Width * 100 / HResolution);
int imageHeight = (int)(this.pictureBox.Image.Height * 100 / VResolution);

// 要打印的内容单位也转为毫米
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType() == typeof(Label))
{
float left = (float)((ctrl.Left -this.pictureBox.Left) * 25.4 / HResolution + this.adjustX);
float top = (float)((ctrl.Top -this.pictureBox.Top) * 25.4 / VResolution + this.adjustY + topMargin * 25.4 / 100);
float width = (float)(ctrl.Width * 25.4 / this.dpiX);
float height = (float)(ctrl.Height * 25.4 / this.dpiY);

e.Graphics.DrawString(ctrl.Text, ctrl.Font, this.drawBrush, new RectangleF(left, top, width, height));
}
}
}
goodbegin 2005-12-11
  • 打赏
  • 举报
回复
up

110,545

社区成员

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

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

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