Files
BusinessMgr-2019/SaBusMgrCoreProcs/Procs/Reports/SbmPrint_Footer.cs
T
2021-07-08 20:50:46 +01:00

122 lines
4.4 KiB
C#

using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sarand.BusinessMgr.Net.Core.Procs.Reports
{
//==========================================================================
public class SbmPrinter_Footer : SacsDisposable
{
//----------------------------------------------------------------------
// Configuration.
public class Config
{
public Graphics Graphics { get; set; }
public Rectangle PrintArea { get; set; }
public int PageNumber { get; set; } = 0;
public int PageCount { get; set; } = 0;
public float PosY { get; set; } = 0.0f;
}
//----------------------------------------------------------------------
// Data members.
private String footerLeft = "";
// Drawing tools.
private Font fontFooter;
private Brush brushFooter = new SolidBrush(SbmReport.PrintColor);
//----------------------------------------------------------------------
// Construction.
public SbmPrinter_Footer ()
{
}
protected override void Dispose (bool disposing)
{
if (disposing)
{
// Data members.
// Drawing tools.
if (fontFooter != null) fontFooter.Dispose();
if (brushFooter != null) brushFooter.Dispose();
}
}
//----------------------------------------------------------------------
// Initialise tools.
private void InitialiseTools (Config config)
{
// Font proprtional to width of page.
float fontFooterHeight = 12.0f * (config.PrintArea.Width * 0.00080f);
if (fontFooter == null || fontFooter.Size != fontFooterHeight)
{
if (fontFooter != null) fontFooter.Dispose();
fontFooter = new Font(SbmReport.FontFamilyName, fontFooterHeight);
}
}
//----------------------------------------------------------------------
// Project info printing.
public void PrintFooter (Config config)
{
// Initialise tools.
InitialiseTools(config);
GraphicsUnit graphicsUnit = config.Graphics.PageUnit;
SmoothingMode smoothingMode = config.Graphics.SmoothingMode;
TextRenderingHint textRenderingHint = config.Graphics.TextRenderingHint;
// Construct footer text.
if (StringUtil.IsEmpty(footerLeft))
footerLeft = "Sarand::LogisticsMgr";
// Print footer - Format.
StringFormat footerLabelFormat = new StringFormat();
footerLabelFormat.Alignment = StringAlignment.Near;
footerLabelFormat.LineAlignment = StringAlignment.Far;
// Calculate text height to offset from bottom of page.
SizeF footerTextSize = config.Graphics.MeasureString("ABC", fontFooter);
float footerIndentX = config.PrintArea.Width * 0.02f;
float footerIndentY = config.PrintArea.Height * 0.02f;
float posYFooter = config.PrintArea.Bottom - footerIndentY;
// By default, footer wil be printed at bottom of page bounds (less a margin).
// Specific position - footer position has been stated by caling proc.
if (config.PosY > 0.0f)
{
// Footer will be printed with this position at the top.
posYFooter = config.PosY;
footerLabelFormat.LineAlignment = StringAlignment.Near;
}
// Print footer - Left.
PointF posFooterLeft = new PointF(config.PrintArea.Left + footerIndentX, posYFooter);
config.Graphics.DrawString(footerLeft, fontFooter, brushFooter, posFooterLeft, footerLabelFormat);
// Print footer - Center.
if (config.PageNumber > 0 && config.PageCount > 1)
{
String footerCenter = "Page " + config.PageNumber + (config.PageCount > 0 ? " of " + config.PageCount : "");
PointF posFooterCenter = new PointF(config.PrintArea.Left + (config.PrintArea.Width / 2.0f), posFooterLeft.Y);
footerLabelFormat.Alignment = StringAlignment.Center;
config.Graphics.DrawString(footerCenter, fontFooter, brushFooter, posFooterCenter, footerLabelFormat);
}
// Print footer - Right.
String footerRight = "Printed: " + DateTime.Now.Format("ddd d-MMM-yyyy HH:mm");
PointF posFooterRight = new PointF(config.PrintArea.Right - footerIndentX, posFooterLeft.Y);
footerLabelFormat.Alignment = StringAlignment.Far;
config.Graphics.DrawString(footerRight, fontFooter, brushFooter, posFooterRight, footerLabelFormat);
}
//----------------------------------------------------------------------
}
//==========================================================================
}