110,539
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}