C# 自定义打印
C# 自定义打印类,可以实现打印文本、线条、图片等,以下仅仅是举例代码:
public class PrintInfo { public string PortName { get; set; } public string DepartureTime { get; set; } public string AMPM { get; set; } PrintDocument document = new PrintDocument(); private int pageWidth = 302; private int pageHeight = 530; public PrintInfo() {
PortName=“Test Port”;
DepartureTime="11:30";
AMPM="上午"; document.BeginPrint += new PrintEventHandler(document_BeginPrint); document.PrintPage += new PrintPageEventHandler(document_PrintPage); document.EndPrint += new PrintEventHandler(document_EndPrint); } private void document_EndPrint(object sender, PrintEventArgs e) { } private void document_BeginPrint(object sender, PrintEventArgs e) { } private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (DepartureTime != null) { //Logo Bitmap imageLogo = new Bitmap(System.AppDomain.CurrentDomain.BaseDirectory + @"\Resource\logo.bmp"); e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.DrawImage(imageLogo, 5, 10, 292, 131); //Line e.Graphics.DrawLine(new Pen(Brushes.Black, 3), 12, 166, 291, 166); //Port Name Font strFont = new Font("宋体", 18); e.Graphics.DrawString(PortName, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(PortName, strFont).Width / 2 + 1, 198); //Departure Time strFont = new Font("宋体", 50, FontStyle.Bold); e.Graphics.DrawString(DepartureTime, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(DepartureTime, strFont).Width / 2, 248); //AMPM e.Graphics.DrawString(AMPM, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(AMPM, strFont).Width / 2, 320); //Line e.Graphics.DrawLine(new Pen(Brushes.Black, 3), 12, 419, 291, 419); //Thankyou and print time strFont = new Font("宋体", 13, FontStyle.Bold); printTxt = "Thank You"; e.Graphics.DrawString(printTxt, strFont, Brushes.Black, 12, 481); strFont = new Font("宋体", 8); printTxt = DateTime.Now.ToString("MM/dd/yyyy hh:mmt\\M"); e.Graphics.DrawString(printTxt, strFont, Brushes.Black, 184, 487); } } public void PrintDocument() { document.DefaultPageSettings.PaperSize = new PaperSize("Custum", pageWidth, pageHeight); PrintController printController = new StandardPrintController(); document.PrintController = printController; //System.Windows.Forms.PrintPreviewDialog printPreview = new System.Windows.Forms.PrintPreviewDialog(); //printPreview.Document = document; //printPreview.ShowDialog(); document.Print(); }