pdf data sort

main
Peace 11 months ago
parent dbea3660a7
commit 2c0d518b99
  1. 38
      PLibs/PdfHelperFW/PdfHelper.cs

@ -25,6 +25,8 @@ namespace PdfHelperFW
readonly int MARGIN_LEFT = 20; readonly int MARGIN_LEFT = 20;
readonly int MARGIN_TOP = 45; readonly int MARGIN_TOP = 45;
readonly bool SORT_ASC = true;
// Elements // Elements
readonly bool _enableNoCol = true; readonly bool _enableNoCol = true;
readonly double NO_COL_MARGIN = 4; readonly double NO_COL_MARGIN = 4;
@ -59,7 +61,7 @@ namespace PdfHelperFW
readonly double LOGO_Y_MARGIN = 5; readonly double LOGO_Y_MARGIN = 5;
MemoryStream _ms; MemoryStream _ms;
public PdfHelper(DataTable table, int timestampColIdx = 0, bool enableNoCol = true) public PdfHelper(DataTable table, int timestampColIdx = 0, bool enableNoCol = true, bool sortAsc = true)
{ {
if (table.Columns.Count - 1 > MAX_CELL_COLUMN_COUNT) if (table.Columns.Count - 1 > MAX_CELL_COLUMN_COUNT)
throw new ArgumentException($"The number of data should be less than {MAX_CELL_COLUMN_COUNT}"); throw new ArgumentException($"The number of data should be less than {MAX_CELL_COLUMN_COUNT}");
@ -69,6 +71,8 @@ namespace PdfHelperFW
_enableNoCol = enableNoCol; _enableNoCol = enableNoCol;
SORT_ASC = sortAsc;
// Check cell width // Check cell width
CELL_WIDTH = ALL_CELL_WIDTH / (table.Columns.Count - 1); CELL_WIDTH = ALL_CELL_WIDTH / (table.Columns.Count - 1);
@ -83,7 +87,7 @@ namespace PdfHelperFW
_format.Alignment = XStringAlignment.Center; _format.Alignment = XStringAlignment.Center;
} }
public PdfHelper(DataTable table, string logoPath, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true) : this(table, timestampColIdx, enableNoCol) public PdfHelper(DataTable table, string logoPath, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true, bool sortAsc = true) : this(table, timestampColIdx, enableNoCol, sortAsc)
{ {
if (string.IsNullOrEmpty(logoPath) || !File.Exists(logoPath) || if (string.IsNullOrEmpty(logoPath) || !File.Exists(logoPath) ||
logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN)
@ -96,7 +100,7 @@ namespace PdfHelperFW
LOGO_Y = MARGIN_TOP - LOGO_HEIGHT - LOGO_Y_MARGIN; LOGO_Y = MARGIN_TOP - LOGO_HEIGHT - LOGO_Y_MARGIN;
} }
public PdfHelper(DataTable table, Bitmap logoBitmap, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true) : this(table, timestampColIdx, enableNoCol) public PdfHelper(DataTable table, Bitmap logoBitmap, double logoHeight, int timestampColIdx = 0, bool enableNoCol = true, bool sortAsc = true) : this(table, timestampColIdx, enableNoCol, sortAsc)
{ {
if (logoBitmap == null if (logoBitmap == null
|| logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN) || logoHeight >= MARGIN_TOP - LOGO_Y_MARGIN)
@ -117,7 +121,19 @@ namespace PdfHelperFW
public void Save(string targetPath) public void Save(string targetPath)
{ {
for (int startRow = 0; startRow < _table.Rows.Count; startRow += ROW_COUNT_PER_PAGE) EnumerableRowCollection sorted;
if (SORT_ASC)
sorted = _table.AsEnumerable().OrderBy(row => row.Field<DateTime>(_timestampColIdx));
else
sorted = _table.AsEnumerable().OrderByDescending(row => row.Field<DateTime>(_timestampColIdx));
DataTable sortedTable = _table.Clone();
foreach (DataRow row in sorted)
{
sortedTable.ImportRow(row);
}
for (int startRow = 0; startRow < sortedTable.Rows.Count; startRow += ROW_COUNT_PER_PAGE)
{ {
PdfPage page = _document.AddPage(); PdfPage page = _document.AddPage();
page.Size = PdfSharp.PageSize.A4; page.Size = PdfSharp.PageSize.A4;
@ -130,7 +146,7 @@ namespace PdfHelperFW
// Header // Header
double x = MARGIN_LEFT; double x = MARGIN_LEFT;
double y = MARGIN_TOP; double y = MARGIN_TOP;
int endRow = Math.Min(startRow + ROW_COUNT_PER_PAGE, _table.Rows.Count); int endRow = Math.Min(startRow + ROW_COUNT_PER_PAGE, sortedTable.Rows.Count);
double cellWidth = CELL_WIDTH; double cellWidth = CELL_WIDTH;
double noCellWidth = 0; double noCellWidth = 0;
// Cell width recalculation // Cell width recalculation
@ -138,7 +154,7 @@ namespace PdfHelperFW
{ {
int maxNo = endRow + 1; int maxNo = endRow + 1;
noCellWidth = MeasureStringWidth(xg, maxNo.ToString(), _font) + NO_COL_MARGIN; noCellWidth = MeasureStringWidth(xg, maxNo.ToString(), _font) + NO_COL_MARGIN;
cellWidth = (ALL_CELL_WIDTH - noCellWidth) / (_table.Columns.Count - 1); cellWidth = (ALL_CELL_WIDTH - noCellWidth) / (sortedTable.Columns.Count - 1);
XRect rect = new XRect(x, y, noCellWidth, CELL_HEIGHT); XRect rect = new XRect(x, y, noCellWidth, CELL_HEIGHT);
xg.DrawRectangle(HEADER_RECT_STYLE, rect); xg.DrawRectangle(HEADER_RECT_STYLE, rect);
@ -148,7 +164,7 @@ namespace PdfHelperFW
x += noCellWidth; x += noCellWidth;
} }
for (int col = 0; col < _table.Columns.Count; col++) for (int col = 0; col < sortedTable.Columns.Count; col++)
{ {
double increment = 0; double increment = 0;
XRect rect; XRect rect;
@ -165,7 +181,7 @@ namespace PdfHelperFW
xg.DrawRectangle(HEADER_RECT_STYLE, rect); xg.DrawRectangle(HEADER_RECT_STYLE, rect);
xg.DrawRectangle(RECT_EDGE_PEN, rect); xg.DrawRectangle(RECT_EDGE_PEN, rect);
xg.DrawString(_table.Columns[col].ColumnName, _boldFont, HEADER_STRING_COLOR, rect, _format); xg.DrawString(sortedTable.Columns[col].ColumnName, _boldFont, HEADER_STRING_COLOR, rect, _format);
x += increment; x += increment;
} }
@ -184,7 +200,7 @@ namespace PdfHelperFW
x += noCellWidth; x += noCellWidth;
} }
for (int col = 0; col < _table.Columns.Count; col++) for (int col = 0; col < sortedTable.Columns.Count; col++)
{ {
double increment = 0; double increment = 0;
XRect rect; XRect rect;
@ -192,14 +208,14 @@ namespace PdfHelperFW
if (col == _timestampColIdx) if (col == _timestampColIdx)
{ {
rect = new XRect(x, y, TIMESTAMP_CELL_WIDTH, CELL_HEIGHT); rect = new XRect(x, y, TIMESTAMP_CELL_WIDTH, CELL_HEIGHT);
DateTime date = (DateTime)_table.Rows[row][col]; DateTime date = (DateTime)sortedTable.Rows[row][col];
value = date.ToString("yyyy-MM-dd HH:mm:ss"); value = date.ToString("yyyy-MM-dd HH:mm:ss");
increment = TIMESTAMP_CELL_WIDTH; increment = TIMESTAMP_CELL_WIDTH;
} }
else else
{ {
rect = new XRect(x, y, cellWidth, CELL_HEIGHT); rect = new XRect(x, y, cellWidth, CELL_HEIGHT);
value = _table.Rows[row][col].ToString(); value = sortedTable.Rows[row][col].ToString();
increment = cellWidth; increment = cellWidth;
} }

Loading…
Cancel
Save