关于C#打印的问题,打印预览和实际打印结果不一样。

lierenzhifeng 2013-06-14 05:20:54
领导让写个打印程序,自己测试了好久,预览的效果和实际打印的效果总是有很大的差异。
预览是距离打印的纸张没有什么距离
而实际打印的时候 距离纸张左侧有大概4厘米左右的距离。右侧也是一样。
求解!求帮助!
下面是跟网上找的代码改的 效果跟自己写的一样。

private void button1_Click(object sender, EventArgs e)
{


this.printDocument1.OriginAtMargins = true;
this.printDocument1.DefaultPageSettings.PaperSize =
new System.Drawing.Printing.PaperSize("Custom", PaperWidth, PaperHeight);
this.printDocument1.DefaultPageSettings.PaperSize.RawKind = 300;

this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(15, 15,15,15);

this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

//将写好的格式给打印预览控件以便预览
printPreviewDialog1.Document = printDocument1;
//显示预览
DialogResult result = printPreviewDialog1.ShowDialog();
if (result == DialogResult.OK)
{
this.printDocument1.Print();
}

}

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//throw new NotImplementedException();

//test
ArrayList textList= new ArrayList();
textList.Add("收费单");
textList.Add("2009-04-01");
textList.Add("开始时间:12:00 ");
textList.Add("结束时间:13:00 ");
textList.Add("时间单位:4 ");
textList.Add("单位时间价格:20.00元 ");
textList.Add("消费价格:80.00元 ");
textList.Add("------------------------------------------------------------- ");
textList.Add("2009-04-01 ");
textList.Add("开始时间:12:20 ");
textList.Add("开始时间:12:20 ");
MyPrinter mp = new MyPrinter(this.printDocument1, TitleSubFont, TitleMFont, Color.Black, textList);
mp.DrawDocument(e.Graphics);

}



namespace TestPrint
{
class MyPrinter
{

private PrintDocument ThePrintDocument;
private Font titleFont;
private Font theFont;
private Color FontColor;
private float CurrentY;
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private ArrayList textList = new ArrayList();
private int currentIndex = 0;
public MyPrinter(PrintDocument _thePrintDocument, Font _theFont, Font _titleFont, Color _FontColor, ArrayList _textList)
{
ThePrintDocument = _thePrintDocument;
theFont = _theFont;
titleFont = _titleFont;
FontColor = _FontColor;
PageNumber = 0;
textList = _textList;
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
}
public bool DrawDocument(Graphics g)
{
try
{
DrawHeader(g);
bool bContinue = DrawItems(g);
g.Dispose();
return bContinue;
}
catch (Exception ex)
{
MessageBox.Show("失败" + ex.Message.ToString(), " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
g.Dispose();
return false;
}
}
public void DrawHeader(Graphics g)
{
CurrentY = (float)TopMargin;
PageNumber++;
string PageString = "第" + PageNumber + "页";

StringFormat PageStringFormat = new StringFormat();
PageStringFormat.Trimming = StringTrimming.Word;
PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
PageStringFormat.Alignment = StringAlignment.Far;

RectangleF PageStringRectangle =
new RectangleF((float)LeftMargin, CurrentY,
(float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, theFont).Height);

g.DrawString(PageString, theFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);

CurrentY += g.MeasureString(PageString, theFont).Height;

StringFormat TitleFormat = new StringFormat();
TitleFormat.Trimming = StringTrimming.Word;
TitleFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
TitleFormat.Alignment = StringAlignment.Center;

RectangleF TitleRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(" 收费单", titleFont).Height);

g.DrawString("收费单", titleFont, new SolidBrush(FontColor), TitleRectangle, TitleFormat);

CurrentY += g.MeasureString("收费单", titleFont).Height;
}
public bool DrawItems(Graphics g)
{
StringFormat TextFormat = new StringFormat();
TextFormat.Trimming = StringTrimming.Word;
TextFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
TextFormat.Alignment = StringAlignment.Near;
for (int i = currentIndex; i < textList.Count; i++)
{
string var = textList[i].ToString();
RectangleF TextRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(" 收费单", titleFont).Height);
g.DrawString(var, theFont, new SolidBrush(FontColor), TextRectangle, TextFormat);
CurrentY = CurrentY + g.MeasureString(var, theFont).Height;
if ((int)CurrentY > (PageHeight - TopMargin - BottomMargin))
{
currentIndex = i + 1;
return true;
}
}
return false;
}

}
}


...全文
857 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zijiang001 2014-04-23
  • 打赏
  • 举报
回复
打印机设置有左右边距 设成0看看
kxycq 2014-04-23
  • 打赏
  • 举报
回复
你这个打印的问题解决了吗?
lierenzhifeng 2013-06-27
  • 打赏
  • 举报
回复
引用 7 楼 wonderfuly 的回复:
this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0,0,0); 试试
这个也试过了不行
whrspsoft3723 2013-06-27
  • 打赏
  • 举报
回复
TitleFormat.Alignment = StringAlignment.Center;?确认没有 问题?画图的代码一部分看不到,调用了吗?
whrspsoft3723 2013-06-27
  • 打赏
  • 举报
回复
TitleFormat.Alignment = StringAlignment.Center;
whrspsoft3723 2013-06-27
  • 打赏
  • 举报
回复
TitleFormat.Alignment = StringAlignment.Center;
本拉灯 2013-06-27
  • 打赏
  • 举报
回复
引用 2 楼 lierenzhifeng 的回复:
实际打印: 预览:
什么打印机?这跟打印机边距有关系,要到打印机里面设一下对应参数
whrspsoft3723 2013-06-27
  • 打赏
  • 举报
回复
把页面除了margins之外的部分(内部),画成一个矩形显示在打印纸上,看上有没有偏差。
游戏人间 2013-06-17
  • 打赏
  • 举报
回复
this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0,0,0); 试试
lierenzhifeng 2013-06-16
  • 打赏
  • 举报
回复
引用 5 楼 tcmakebest 的回复:
因为屏幕和打印机的分辨率不一样,同样的坐标表示的位置是不同的, 可以用下面的语句将坐标单位进行统一,具体用哪个单位你可以多试几下, 然后所有的坐标和尺寸要重新计算: e.Graphics.PageUnit = GraphicsUnit.Document;
我加上这个了,但是没起作用,是加在打印事件里面么?
tcmakebest 2013-06-14
  • 打赏
  • 举报
回复
因为屏幕和打印机的分辨率不一样,同样的坐标表示的位置是不同的, 可以用下面的语句将坐标单位进行统一,具体用哪个单位你可以多试几下, 然后所有的坐标和尺寸要重新计算: e.Graphics.PageUnit = GraphicsUnit.Document;
lierenzhifeng 2013-06-14
  • 打赏
  • 举报
回复
引用 1 楼 devmiao 的回复:
这个要看什么型号的打印机。而且不同的系统也不同。
怎么能减少实际打印的时候左侧、右侧的的距离呢?
lierenzhifeng 2013-06-14
  • 打赏
  • 举报
回复
我的是win7 64位系统 我在xp 32位系统上也不行 怎么能让实际打印的时候左侧,右侧不留那么大的边距呢?
lierenzhifeng 2013-06-14
  • 打赏
  • 举报
回复
实际打印:

预览:
devmiao 2013-06-14
  • 打赏
  • 举报
回复
这个要看什么型号的打印机。而且不同的系统也不同。

110,539

社区成员

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

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

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