- 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 Form10 : Form
- {
- int totalPage = 13;
- int page;
- string filename = "myfile.txt";
- Font printerfont = null;
- public Form10()
- {
- InitializeComponent();
- }
- static Rectangle GetRealPageBounds(PrintPageEventArgs e, bool preview)
- {
- if (preview) return e.MarginBounds;
- float cx = e.PageSettings.HardMarginX;
- float cy = e.PageSettings.HardMarginY;
- float dpix = e.Graphics.DpiX;
- float dpiy = e.Graphics.DpiY;
- Rectangle marginBounds = e.MarginBounds;
- marginBounds.Offset((int)(-cx * 100 / dpix), (int)(-cy * 100 / dpiy));
- return marginBounds;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.printDocument1.DocumentName = this.filename;
- this.pageSetupDialog1.Document = this.printDocument1;
- this.printDocument1.PrinterSettings.FromPage = 1;
- this.printDocument1.PrinterSettings.ToPage = totalPage;
- this.printDocument1.PrinterSettings.MinimumPage = 1;
- this.printDocument1.PrinterSettings.MaximumPage = totalPage;
- this.printDialog1.AllowSomePages = true;
- this.pageSetupDialog1.ShowDialog();
- this.printDialog1.Document = this.printDocument1;
- if (this.printDialog1.ShowDialog() == DialogResult.OK)
- {
- this.printDocument1.Print();
- }
- }
- private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
- {
- Graphics g = e.Graphics;
- bool preview = false;
- RectangleF realMarginBounds = GetRealPageBounds(e, preview);
- g.DrawString("Content", printerfont, Brushes.Red, realMarginBounds);
- }
- private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
- {
- printerfont.Dispose();
- printerfont = null;
- }
- private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
- {
- printerfont = new Font("Lucida Console", 72);
- }
- }
- }
c#打印范围1
最新推荐文章于 2019-09-24 15:10:16 发布