c#打印边距1
来源:互联网 发布:万捷网络验证 编辑:程序博客网 时间:2024/06/06 03:31
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Printing;
- using System.Drawing.Drawing2D;
- namespace WindowsApplication3
- {
- public partial class Form6 : Form
- {
- string filename = "myfile.txt";
- Font printerfont = null;
- public Form6()
- {
- InitializeComponent();
- }
- static Rectangle GetRealPageBounds(PrintPageEventArgs e, bool preview)
- {
- if (preview) return e.PageBounds;
- RectangleF vpb = e.Graphics.VisibleClipBounds;
- PointF[]bottomRight={new PointF(vpb.Size.Width,vpb.Size.Height)};
- e.Graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.Page, bottomRight);
- float dpix = e.Graphics.DpiX;
- float dpiy = e.Graphics.DpiY;
- return new Rectangle(0, 0, (int)(bottomRight[0].X * 100 / dpix), (int)(bottomRight[0].Y * 100 / dpiy));
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.printDocument1.DocumentName = this.filename;
- this.printDocument1.Print();
- }
- private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
- {
- Graphics g = e.Graphics;
- bool preview=false;
- Rectangle realPageBounds = GetRealPageBounds(e, preview);
- g.DrawString("header", printerfont, Brushes.Red, realPageBounds);
- StringFormat farFormat = new StringFormat();
- farFormat.Alignment = StringAlignment.Far;
- farFormat.LineAlignment = StringAlignment.Far;
- g.DrawString("footer", printerfont, Brushes.Black, realPageBounds, farFormat);
- }
- private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
- {
- printerfont = new Font("Lucida Console", 72);
- }
- private void printDocument1_EndPrint(object sender, PrintEventArgs e)
- {
- printerfont.Dispose();
- printerfont = null;
- }
- }
- }