using System; using System.Collections.Generic; using System.Text; using System.Drawing.Printing; using System.Drawing; using System.Drawing.Drawing2D; using Microsoft.Win32; using System.IO; namespace HotelPms.Share.Windows.Report { /// /// ****************************** Description ******************************* /// ◇システム名称 ///  @CoreCom /// ◇概要 ///  施設設定画面 /// ◇履歴 ///  2007/11/02 小木 勝龍  新規作成 /// ****************************** Declarations ****************************** /// public abstract class PrintGdiPlus : ReportBase,IDisposable { #region ★★★★★ Declartions ★★★★★ public static SortedDictionary FontFileDict = null; public static string SystemFontPath = string.Empty; public delegate void FetchEventHandler(object sender, FetchEventArgs e); public event FetchEventHandler FetchItem = null; private bool m_Disposed = false; protected StringFormat m_StringFormat = new StringFormat(); protected float m_PrintRate = 1F; //印字倍率 protected static int A4PaperWidth = 827; protected static int A4PaperHeight = 1169; protected static int B5PaperWidth = 717; protected static int B5PaperHeight = 1012; #endregion #region ★★★★★ Property ★★★★★ protected List m_Items = new List(); public List Items { get { return m_Items; } } protected PrintDocument m_PrintDoc = new PrintDocument(); public PrintDocument PrintDoc { get { return m_PrintDoc; } set { m_PrintDoc = value; } } /// /// 印字範囲 /// public float PaperSizeRate { get; set; } = 0.95F; #endregion #region ★★★★★ Class Event ★★★★★ public PrintGdiPlus() { m_PrintDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); SystemFontPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); if (FontFileDict == null) { FontFileDict = ReadFontInformation(); } m_StringFormat.FormatFlags |= StringFormatFlags.NoWrap; } ~PrintGdiPlus() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (!m_Disposed) //一回だけ { if (disposing) { //Managed Resources if (m_StringFormat != null) { m_StringFormat.Dispose(); m_StringFormat = null; } } //Unmanaged resources m_Disposed = true; } } public virtual void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion #region ★★★★★ Control Event ★★★★★ #endregion #region ★★★★★ Private Function ★★★★★ #endregion #region ★★★★★ Public Function ★★★★★ public void Clear() { m_Items.Clear(); } public void Add(Cell cell) { m_Items.Add(cell); } public void PrintAll(Graphics g) { int i = 0; int count = m_Items.Count; foreach (Cell item in m_Items) { if (FetchItem != null) { FetchItem(this, new FetchEventArgs(item, i, count)); } DrawCell(g, item); i++; } } public void DrawBorder(Graphics g, Border border, Cell item) { if (!border.Visible) { return; } using (Pen pen = new Pen(Brushes.Black, border.Weight)) { pen.DashStyle = border.DashStyle; float x1 = 0F; float y1 = 0F; float x2 = 0F; float y2 = 0F; if (border.Index == Border.TypeIndex.Top) { x1 = item.Style.Left - border.Weight * 0.5F; x2 = item.Style.Left + item.Style.Width + border.Weight * 0.5F; y1 = y2 = item.Style.Top; } else if (border.Index == Border.TypeIndex.Bottom) { x1 = item.Style.Left - border.Weight * 0.5F; x2 = item.Style.Left + item.Style.Width + border.Weight * 0.5F; y1 = y2 = item.Style.Top + item.Style.Height; } else if (border.Index == Border.TypeIndex.Left) { x1 = x2 = item.Style.Left; y1 = item.Style.Top; y2 = item.Style.Top + item.Style.Height; } else if (border.Index == Border.TypeIndex.Right) { x1 = x2 = item.Style.Left + item.Style.Width; y1 = item.Style.Top; y2 = item.Style.Top + item.Style.Height; } g.DrawLine(pen, x1, y1, x2, y2); } } public void DrawCell(Graphics g, Cell item) { if (item.CellType == Cell.CellKind.Text || item.CellType == Cell.CellKind.PrintTime || item.CellType == Cell.CellKind.PageNo || item.CellType == Cell.CellKind.TimeAndPage) { //枠 foreach (Border border in item.Style.BorderList) { DrawBorder(g, border, item); } RectangleF r = new RectangleF(item.Style.Left + item.Style.Padding.Left, item.Style.Top + item.Style.Padding.Top, item.Style.Width - item.Style.Padding.Left - item.Style.Padding.Right, item.Style.Height - item.Style.Padding.Top - item.Style.Padding.Bottom); m_StringFormat.Alignment = item.Style.Alignment; m_StringFormat.LineAlignment = item.Style.LineAlignment; string text = item.Text; if(item.CellType == Cell.CellKind.PrintTime) { text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) : DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); } else if (item.CellType == Cell.CellKind.PageNo) { text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, m_PageIndex, m_PageCount) : $"{m_PageIndex} / {m_PageCount.ToString().PadLeft(5, ' ')}"; } else if (item.CellType == Cell.CellKind.TimeAndPage) { text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), m_PageIndex, m_PageCount) : $"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} {m_PageIndex} / {m_PageCount.ToString().PadLeft(5, ' ')}"; } g.DrawString(text, item.Style.Font, Brushes.Black, r, m_StringFormat); } else if (item.CellType == Cell.CellKind.DotLine || item.CellType == Cell.CellKind.Line) { using (Pen pen = new Pen(Brushes.Black, 1)) { pen.DashStyle = (item.CellType == Cell.CellKind.DotLine) ? DashStyle.Dot : DashStyle.Solid; g.DrawLine(pen, item.Style.Left, item.Style.Top, item.Style.Left + item.Style.Width, item.Style.Top + item.Style.Height); } } else if (item.CellType == Cell.CellKind.Image) { if (System.IO.File.Exists(item.Text)) { using (System.Drawing.Image img = System.Drawing.Image.FromFile(item.Text)) { DrawImage(g, img, item.Style.RectangleF); g.DrawRectangle(Pens.Black, item.Style.RectangleF.X, item.Style.RectangleF.Y, item.Style.RectangleF.Width, item.Style.RectangleF.Height); } } } } public void Output() { m_PrintDoc.Print(); } /// /// /// /// /// /// public void DrawImage(Graphics graphics, Image image, RectangleF rectangle) { if (graphics == null) throw new ArgumentNullException(); if (rectangle.Width <= 0 || rectangle.Height <= 0) throw new ArgumentOutOfRangeException(); if (image == null) return; var l = rectangle.Left; var t = rectangle.Top; var w = (float)image.Width; var h = (float)image.Height; var r = h / w; w = rectangle.Height / r; h = rectangle.Height; if (w > rectangle.Width) { w = rectangle.Width; h = rectangle.Width * r; } l += (rectangle.Width - w) / 2; t += (rectangle.Height - h) / 2; graphics.DrawImage(image, l, t, w, h); } #endregion } }