简单文本打印,PrintDocument使用
68 阅读
0 评论
45 点赞
概述
Source Codeusing System; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.IO; namespace TextPrinter { public class TextPrinter { private string m_Title; private string m_Context; private Font m_Font; private Font m_TitleFont; private SizeF m_WordSize; private int m_CurrentPage; private Rectangle m_PrintableArea; private string m_LeftContext; private int m_RowPadding; private TitleType m_TitleType; private FooterType m_FooterType; //private int m_TotalPage; //private bool m_Prepare; private string m_FooterFormat; private PrintDocument printDoc; public TextPrinter() { printDoc = new PrintDocument(); printDoc.PrintPage += new PrintPageEventHandler(InternalPrintPage); m_Font = SystemFonts.DefaultFont; m_TitleFont = SystemFonts.CaptionFont; m_TitleType = TitleType.None; m_FooterType = FooterType.None; FooterFormat = "第{0}页"; } ////// Report Title /// public string Title { get { return m_Title; } set { m_Title = value; } } ////// Report Context string /// public string Context { get { return m_Context; } set { m_Context = value; } } ////// Report Context Font /// public Font Font { get { return m_Font; } set { m_Font = value; } } ////// Report Title Font /// public Font TitleFont { get { return m_TitleFont; } set { m_TitleFont = value; } } ////// Report Title Type /// public TitleType TitleType { get { return m_TitleType; } set { m_TitleType = value; } } ////// Report Footer Type /// public FooterType FooterType { get { return m_FooterType; } set { m_FooterType = value; } } ////// Report Footer Format. etc "第{0}页" /// public string FooterFormat { get { return m_FooterFormat; } set { m_FooterFormat = value; } } //public int TotalPage //{ // get { return m_TotalPage; } //} ////// Report row padding. 行间距 /// public int RowPadding { get { return m_RowPadding; } set { m_RowPadding = value; } } ////// 初始化打印 /// private void InitPrint() { m_CurrentPage = 1; } ////// 打印机设置 /// public void PrintSetting() { PrintDialog pd = new PrintDialog(); pd.Document = printDoc; pd.ShowDialog(); } ////// 纸张设置 /// public void PageSetting() { PageSetupDialog psd = new PageSetupDialog(); psd.Document = printDoc; psd.ShowDialog(); } ////// 预览报表 /// public void Preview() { InitPrint(); PrintPreviewDialog pdlg = new PrintPreviewDialog(); pdlg.Document = printDoc; pdlg.WindowState = FormWindowState.Maximized; pdlg.PrintPreviewControl.Zoom = 1; //printDoc.Print(); //m_Prepare = true; pdlg.ShowDialog(); } ////// 打印报表 /// public void Print() { InitPrint(); printDoc.Print(); } ////// Read a line from buffer, /// /// /// private string GetLine() { string tmp = String.Empty; try { SizeF size; for (int i = 0; i < m_LeftContext.Length; i++) { size = TextRenderer.MeasureText(m_LeftContext.Substring(0, i), Font); if (size.Width > m_PrintableArea.Width) { tmp = m_LeftContext.Substring(0, i - 1); m_LeftContext = m_LeftContext.Substring(i - 1); return tmp; } if (m_LeftContext.Substring(0, i).IndexOf("/n", 0) >= 0) { tmp = m_LeftContext.Substring(0, i); m_LeftContext = m_LeftContext.Substring(i); return tmp; } } tmp = m_LeftContext; m_LeftContext = String.Empty; return tmp; } finally { tmp = tmp.Replace("/n", ""); } } ////// internal print method /// /// /// private void InternalPrintPage(object sender, PrintPageEventArgs e) { int top = 0; if (m_CurrentPage == 1) { m_LeftContext = Context; m_LeftContext = m_LeftContext.Replace("/r", ""); m_PrintableArea = e.MarginBounds; m_WordSize = TextRenderer.MeasureText("W", Font); if (TitleType == TitleType.OnlyFirstPage) top += DrawTitle(e.Graphics); } if (TitleType == TitleType.AllPage) top += DrawTitle(e.Graphics); while (top < m_PrintableArea.Height - (int)Math.Floor(m_WordSize.Height)) { string tmp = GetLine(); e.Graphics.DrawString(tmp, Font, SystemBrushes.MenuText, e.MarginBounds.X, e.MarginBounds.Y + top); top += (int)Math.Floor(m_WordSize.Height) + RowPadding; } if (FooterType != FooterType.None) DrawFooter(e.Graphics); e.HasMorePages = m_LeftContext != string.Empty; m_CurrentPage++; } ////// Print Report Title /// /// /// private int DrawTitle(Graphics g) { SizeF size = g.MeasureString(Title, TitleFont); DrawString(g, Title, TitleFont, SystemBrushes.WindowText, m_PrintableArea, StringAlignment.Center, StringAlignment.Near); return (int)Math.Floor(size.Height); } ////// Print Report Footer /// /// private void DrawFooter(Graphics g) { string tmp = string.Empty; if (FooterType == FooterType.OnlyPageNumber) tmp = string.Format(FooterFormat, m_CurrentPage); //else if (FooterType == FooterType.PageNumOfTotal) // tmp = string.Format("{0} of {1}", m_CurrentPage, 0); SizeF size = g.MeasureString(tmp, SystemFonts.DefaultFont); DrawString(g, tmp, SystemFonts.DefaultFont, SystemBrushes.WindowText, new Rectangle(m_PrintableArea.X, m_PrintableArea.Bottom - (int)size.Height, m_PrintableArea.Width, (int)size.Height), StringAlignment.Far, StringAlignment.Center); } ////// Draw a string with alignment parameter /// /// /// /// /// /// /// /// private void DrawString(Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment) { StringFormat sf = new StringFormat(); sf.Alignment = alignment; sf.LineAlignment = lineAlignment; g.DrawString(s, font, brush, rect, sf); } public static void Preview(string pStr) { TextPrinter textPrinter = new TextPrinter(); textPrinter.Context = pStr; textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel); textPrinter.Title = "打印测试页"; textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel); textPrinter.TitleType = TitleType.AllPage; textPrinter.FooterType = FooterType.OnlyPageNumber; textPrinter.FooterFormat = "Page {0}"; //textPrinter.PrintSetting(); //textPrinter.PageSetting(); textPrinter.Preview(); //textPrinter.Print(); } public static void Print(string pStr) { TextPrinter textPrinter = new TextPrinter(); textPrinter.Context = pStr; textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel); textPrinter.Title = "打印测试页"; textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel); textPrinter.TitleType = TitleType.AllPage; textPrinter.FooterType = FooterType.OnlyPageNumber; textPrinter.FooterFormat = "Page {0}"; //textPrinter.PrintSetting(); //textPrinter.PageSetting(); //textPrinter.Preview(); textPrinter.Print(); } } public enum TitleType { None, OnlyFirstPage, AllPage } public enum FooterType { None, OnlyPageNumber //PageNumOfTotal } }
最后
以上就是瘦瘦鼠标为你收集整理的简单文本打印,PrintDocument使用的全部内容,希望文章能够帮你解决简单文本打印,PrintDocument使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
- 本文分类:string
- 浏览次数:68 次浏览
- 发布日期:2023-09-15 18:00:45
- 本文链接:https://www.kaopuke.com/article/k-p-k_14_uzo_6_f5_12__23__2_3.html
发表评论 取消回复