From 797c061bbf488fa467d89c57bd46a4a5215d6698 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 14 Mar 2024 10:05:54 +0900 Subject: [PATCH] Add number column function --- PLibs/PdfHelperFW/PdfHelper.cs | 74 +++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/PLibs/PdfHelperFW/PdfHelper.cs b/PLibs/PdfHelperFW/PdfHelper.cs index 87583c6..7041696 100644 --- a/PLibs/PdfHelperFW/PdfHelper.cs +++ b/PLibs/PdfHelperFW/PdfHelper.cs @@ -20,12 +20,16 @@ namespace PdfHelperFW readonly int PAGE_WIDTH = 595; // A4 readonly string FONT_NAME = "Malgun Gothic"; - readonly double FONT_SIZE = 6d; + readonly double FONT_SIZE = 5.5d; readonly int MARGIN_LEFT = 20; readonly int MARGIN_TOP = 45; // Elements + readonly bool _enableNoCol = true; + readonly double NO_COL_MARGIN = 4; + + readonly double CONTENT_WIDTH = 555; readonly double TIMESTAMP_CELL_WIDTH = 70; public readonly double MAX_CELL_COLUMN_COUNT = 10; @@ -55,7 +59,7 @@ namespace PdfHelperFW readonly double LOGO_Y_MARGIN = 5; MemoryStream _ms; - public PdfHelper(DataTable table, int timestampColIdx = 0) + public PdfHelper(DataTable table, int timestampColIdx = 0, bool enableNoCol = true) { if (table.Columns.Count - 1 > MAX_CELL_COLUMN_COUNT) throw new ArgumentException($"The number of data should be less than {MAX_CELL_COLUMN_COUNT}"); @@ -63,6 +67,8 @@ namespace PdfHelperFW _table = table; _timestampColIdx = timestampColIdx; + _enableNoCol = enableNoCol; + // Check cell width CELL_WIDTH = ALL_CELL_WIDTH / (table.Columns.Count - 1); @@ -77,7 +83,7 @@ namespace PdfHelperFW _format.Alignment = XStringAlignment.Center; } - public PdfHelper(DataTable table, string logoPath, double logoHeight, int timestampColIdx = 0) : this(table, timestampColIdx) + public PdfHelper(DataTable table, string logoPath, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true) : this(table, timestampColIdx, enableNoCol) { if (string.IsNullOrEmpty(logoPath) || !File.Exists(logoPath) || logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) @@ -86,11 +92,11 @@ namespace PdfHelperFW 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_X = MARGIN_LEFT + CONTENT_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) + public PdfHelper(DataTable table, Bitmap logoBitmap, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true) : this(table, timestampColIdx, enableNoCol) { if (logoBitmap == null || logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) @@ -105,7 +111,7 @@ namespace PdfHelperFW 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_X = MARGIN_LEFT + CONTENT_WIDTH - LOGO_WIDTH; LOGO_Y = MARGIN_TOP - LOGO_HEIGHT - LOGO_Y_MARGIN; } @@ -115,15 +121,33 @@ namespace PdfHelperFW { PdfPage page = _document.AddPage(); page.Size = PdfSharp.PageSize.A4; - XGraphics gf = XGraphics.FromPdfPage(page); + XGraphics xg = XGraphics.FromPdfPage(page); // Logo if (LOGO_IMAGE != null) - gf.DrawImage(LOGO_IMAGE, LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT); + xg.DrawImage(LOGO_IMAGE, LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT); // Header double x = MARGIN_LEFT; double y = MARGIN_TOP; + int endRow = Math.Min(startRow + ROW_COUNT_PER_PAGE, _table.Rows.Count); + double cellWidth = CELL_WIDTH; + double noCellWidth = 0; + // Cell width recalculation + if (_enableNoCol) + { + int maxNo = endRow + 1; + noCellWidth = MeasureStringWidth(xg, maxNo.ToString(), _font) + NO_COL_MARGIN; + cellWidth = (ALL_CELL_WIDTH - noCellWidth) / (_table.Columns.Count - 1); + + XRect rect = new XRect(x, y, noCellWidth, CELL_HEIGHT); + xg.DrawRectangle(HEADER_RECT_STYLE, rect); + xg.DrawRectangle(RECT_EDGE_PEN, rect); + //xg.DrawString("No.", _boldFont, HEADER_STRING_COLOR, rect, _format); + + x += noCellWidth; + } + for (int col = 0; col < _table.Columns.Count; col++) { double increment = 0; @@ -135,23 +159,31 @@ namespace PdfHelperFW } else { - rect = new XRect(x, y, CELL_WIDTH, CELL_HEIGHT); - increment = CELL_WIDTH; + rect = new XRect(x, y, cellWidth, CELL_HEIGHT); + increment = cellWidth; } - gf.DrawRectangle(HEADER_RECT_STYLE, rect); - gf.DrawRectangle(RECT_EDGE_PEN, rect); - gf.DrawString(_table.Columns[col].ColumnName, _boldFont, HEADER_STRING_COLOR, rect, _format); + xg.DrawRectangle(HEADER_RECT_STYLE, rect); + xg.DrawRectangle(RECT_EDGE_PEN, rect); + xg.DrawString(_table.Columns[col].ColumnName, _boldFont, HEADER_STRING_COLOR, rect, _format); x += increment; } // Row - int endRow = Math.Min(startRow + ROW_COUNT_PER_PAGE, _table.Rows.Count); for (int row = startRow; row < endRow; row++) { x = MARGIN_LEFT; y += CELL_HEIGHT; + + if (_enableNoCol) + { + XRect rect = new XRect(x, y, noCellWidth, CELL_HEIGHT); + xg.DrawRectangle(RECT_EDGE_PEN, rect); + xg.DrawString((row + 1).ToString(), _font, STRING_COLOR, rect, _format); + x += noCellWidth; + } + for (int col = 0; col < _table.Columns.Count; col++) { double increment = 0; @@ -166,13 +198,13 @@ namespace PdfHelperFW } else { - rect = new XRect(x, y, CELL_WIDTH, CELL_HEIGHT); + rect = new XRect(x, y, cellWidth, CELL_HEIGHT); value = _table.Rows[row][col].ToString(); - increment = CELL_WIDTH; + increment = cellWidth; } - gf.DrawRectangle(RECT_EDGE_PEN, rect); - gf.DrawString(value, _font, STRING_COLOR, rect, _format); + xg.DrawRectangle(RECT_EDGE_PEN, rect); + xg.DrawString(value, _font, STRING_COLOR, rect, _format); x += increment; } @@ -184,5 +216,11 @@ namespace PdfHelperFW if (_ms != null) _ms.Dispose(); } + + double MeasureStringWidth(XGraphics xg, string text, XFont font) + { + XSize size = xg.MeasureString(text, font); + return size.Width; + } } }