关于C#打印的问题,打印预览和实际打印结果不一样。
领导让写个打印程序,自己测试了好久,预览的效果和实际打印的效果总是有很大的差异。预览是距离打印的纸张没有什么距离
而实际打印的时候 距离纸张左侧有大概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;
}
}
}
C# --------------------编程问答-------------------- 这个要看什么型号的打印机。而且不同的系统也不同。 --------------------编程问答-------------------- 实际打印:
预览:
--------------------编程问答-------------------- 我的是win7 64位系统
我在xp 32位系统上也不行
怎么能让实际打印的时候左侧,右侧不留那么大的边距呢? --------------------编程问答--------------------
怎么能减少实际打印的时候左侧、右侧的的距离呢? --------------------编程问答-------------------- 因为屏幕和打印机的分辨率不一样,同样的坐标表示的位置是不同的,
可以用下面的语句将坐标单位进行统一,具体用哪个单位你可以多试几下,
然后所有的坐标和尺寸要重新计算:
e.Graphics.PageUnit = GraphicsUnit.Document; --------------------编程问答--------------------
我加上这个了,但是没起作用,是加在打印事件里面么? --------------------编程问答-------------------- this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0,0,0);
试试 --------------------编程问答--------------------
这个也试过了不行 --------------------编程问答-------------------- 把页面除了margins之外的部分(内部),画成一个矩形显示在打印纸上,看上有没有偏差。
--------------------编程问答--------------------
什么打印机?这跟打印机边距有关系,要到打印机里面设一下对应参数 --------------------编程问答-------------------- TitleFormat.Alignment = StringAlignment.Center; --------------------编程问答-------------------- TitleFormat.Alignment = StringAlignment.Center; --------------------编程问答-------------------- TitleFormat.Alignment = StringAlignment.Center;?确认没有 问题?画图的代码一部分看不到,调用了吗?
补充:.NET技术 , C#