diff --git a/PLibs/PdfHelperFW/PdfHelper.cs b/PLibs/PdfHelperFW/PdfHelper.cs index a3f5099..87583c6 100644 --- a/PLibs/PdfHelperFW/PdfHelper.cs +++ b/PLibs/PdfHelperFW/PdfHelper.cs @@ -3,6 +3,8 @@ using PdfSharp.Pdf; using System; using System.Collections.Generic; using System.Data; +using System.Drawing; +using System.IO; using System.Linq; using System.Text; @@ -21,7 +23,7 @@ namespace PdfHelperFW readonly double FONT_SIZE = 6d; readonly int MARGIN_LEFT = 20; - readonly int MARGIN_TOP = 35; + readonly int MARGIN_TOP = 45; // Elements readonly double TIMESTAMP_CELL_WIDTH = 70; @@ -33,7 +35,7 @@ namespace PdfHelperFW // Design readonly XSolidBrush HEADER_RECT_STYLE = new XSolidBrush(XColor.FromArgb(210, 51, 52)); - readonly XPen RECT_EDGE_PEN = new XPen(XColor.FromArgb(34, 30, 32), 0.5d); + readonly XPen RECT_EDGE_PEN = new XPen(XColor.FromArgb(34, 30, 32), 0.25d); readonly XBrush HEADER_STRING_COLOR = new XSolidBrush(XColors.White); readonly XBrush STRING_COLOR = new XSolidBrush(XColor.FromArgb(34, 30, 32)); @@ -44,6 +46,15 @@ namespace PdfHelperFW XFont _boldFont; XStringFormat _format; + // Logo + readonly XImage LOGO_IMAGE; + readonly double LOGO_WIDTH; + readonly double LOGO_HEIGHT; + readonly double LOGO_X; + readonly double LOGO_Y; + readonly double LOGO_Y_MARGIN = 5; + MemoryStream _ms; + public PdfHelper(DataTable table, int timestampColIdx = 0) { if (table.Columns.Count - 1 > MAX_CELL_COLUMN_COUNT) @@ -66,6 +77,38 @@ namespace PdfHelperFW _format.Alignment = XStringAlignment.Center; } + public PdfHelper(DataTable table, string logoPath, double logoHeight, int timestampColIdx = 0) : this(table, timestampColIdx) + { + if (string.IsNullOrEmpty(logoPath) || !File.Exists(logoPath) || + logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) + throw new ArgumentException($"Some arguments for logo are invalid"); + + LOGO_IMAGE = XImage.FromFile(logoPath); + LOGO_HEIGHT = logoHeight; + LOGO_WIDTH = LOGO_IMAGE.PointWidth * (LOGO_HEIGHT / LOGO_IMAGE.PointHeight); + LOGO_X = MARGIN_LEFT + TIMESTAMP_CELL_WIDTH + ALL_CELL_WIDTH - LOGO_WIDTH; + LOGO_Y = MARGIN_TOP - LOGO_HEIGHT - LOGO_Y_MARGIN; + } + + public PdfHelper(DataTable table, Bitmap logoBitmap, double logoHeight, int timestampColIdx = 0) : this(table, timestampColIdx) + { + if (logoBitmap == null + || logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) + throw new ArgumentException($"Some arguments for logo are invalid"); + + _ms = new MemoryStream(); + + logoBitmap.Save(_ms, System.Drawing.Imaging.ImageFormat.Jpeg); + _ms.Position = 0; + + LOGO_IMAGE = XImage.FromStream(_ms); + + LOGO_HEIGHT = logoHeight; + LOGO_WIDTH = LOGO_IMAGE.PointWidth * (LOGO_HEIGHT / LOGO_IMAGE.PointHeight); + LOGO_X = MARGIN_LEFT + TIMESTAMP_CELL_WIDTH + ALL_CELL_WIDTH - LOGO_WIDTH; + LOGO_Y = MARGIN_TOP - LOGO_HEIGHT - LOGO_Y_MARGIN; + } + public void Save(string targetPath) { for (int startRow = 0; startRow < _table.Rows.Count; startRow += ROW_COUNT_PER_PAGE) @@ -74,6 +117,10 @@ namespace PdfHelperFW page.Size = PdfSharp.PageSize.A4; XGraphics gf = XGraphics.FromPdfPage(page); + // Logo + if (LOGO_IMAGE != null) + gf.DrawImage(LOGO_IMAGE, LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT); + // Header double x = MARGIN_LEFT; double y = MARGIN_TOP; @@ -133,6 +180,9 @@ namespace PdfHelperFW } _document.Save(targetPath); + + if (_ms != null) + _ms.Dispose(); } } }