|
|
|
@ -0,0 +1,138 @@ |
|
|
|
|
using PdfSharp.Drawing; |
|
|
|
|
using PdfSharp.Pdf; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Data; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
namespace PdfHelper |
|
|
|
|
{ |
|
|
|
|
public class PdfHelper |
|
|
|
|
{ |
|
|
|
|
readonly DataTable _table; |
|
|
|
|
readonly int _timestampColIdx = 0; |
|
|
|
|
|
|
|
|
|
// Page structure |
|
|
|
|
readonly int PAGE_HEIGHT = 842; // A4 |
|
|
|
|
readonly int PAGE_WIDTH = 595; // A4 |
|
|
|
|
|
|
|
|
|
readonly string FONT_NAME = "Malgun Gothic"; |
|
|
|
|
readonly double FONT_SIZE = 6d; |
|
|
|
|
|
|
|
|
|
readonly int MARGIN_LEFT = 20; |
|
|
|
|
readonly int MARGIN_TOP = 35; |
|
|
|
|
|
|
|
|
|
// Elements |
|
|
|
|
readonly double TIMESTAMP_CELL_WIDTH = 70; |
|
|
|
|
|
|
|
|
|
public readonly double MAX_CELL_COLUMN_COUNT = 10; |
|
|
|
|
readonly double ALL_CELL_WIDTH = 485; |
|
|
|
|
readonly double CELL_WIDTH = 48.5; |
|
|
|
|
readonly int CELL_HEIGHT = 15; |
|
|
|
|
|
|
|
|
|
// 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 XBrush HEADER_STRING_COLOR = new XSolidBrush(XColors.White); |
|
|
|
|
readonly XBrush STRING_COLOR = new XSolidBrush(XColor.FromArgb(34, 30, 32)); |
|
|
|
|
|
|
|
|
|
readonly int ROW_COUNT_PER_PAGE = 50; |
|
|
|
|
|
|
|
|
|
PdfDocument _document; |
|
|
|
|
XFont _font; |
|
|
|
|
XFont _boldFont; |
|
|
|
|
XStringFormat _format; |
|
|
|
|
|
|
|
|
|
public PdfHelper(DataTable table, int timestampColIdx = 0) |
|
|
|
|
{ |
|
|
|
|
if (table.Columns.Count - 1 > MAX_CELL_COLUMN_COUNT) |
|
|
|
|
throw new ArgumentException($"The number of data should be less than {MAX_CELL_COLUMN_COUNT}"); |
|
|
|
|
|
|
|
|
|
_table = table; |
|
|
|
|
_timestampColIdx = timestampColIdx; |
|
|
|
|
|
|
|
|
|
// Check cell width |
|
|
|
|
CELL_WIDTH = ALL_CELL_WIDTH / (table.Columns.Count - 1); |
|
|
|
|
|
|
|
|
|
_document = new PdfDocument(); |
|
|
|
|
_document.Info.Title = "NS Report"; |
|
|
|
|
_document.Info.Author = "Particle Measuring Systems Korea"; |
|
|
|
|
|
|
|
|
|
_font = new XFont(FONT_NAME, FONT_SIZE, XFontStyle.Regular); |
|
|
|
|
_boldFont = new XFont(FONT_NAME, FONT_SIZE, XFontStyle.Bold); |
|
|
|
|
_format = new XStringFormat(); |
|
|
|
|
_format.LineAlignment = XLineAlignment.Center; |
|
|
|
|
_format.Alignment = XStringAlignment.Center; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Save(string targetPath) |
|
|
|
|
{ |
|
|
|
|
for (int startRow = 0; startRow < _table.Rows.Count; startRow += ROW_COUNT_PER_PAGE) |
|
|
|
|
{ |
|
|
|
|
PdfPage page = _document.AddPage(); |
|
|
|
|
page.Size = PdfSharp.PageSize.A4; |
|
|
|
|
XGraphics gf = XGraphics.FromPdfPage(page); |
|
|
|
|
|
|
|
|
|
// Header |
|
|
|
|
double x = MARGIN_LEFT; |
|
|
|
|
double y = MARGIN_TOP; |
|
|
|
|
for (int col = 0; col < _table.Columns.Count; col++) |
|
|
|
|
{ |
|
|
|
|
double increment = 0; |
|
|
|
|
XRect rect; |
|
|
|
|
if (col == _timestampColIdx) |
|
|
|
|
{ |
|
|
|
|
rect = new XRect(x, y, TIMESTAMP_CELL_WIDTH, CELL_HEIGHT); |
|
|
|
|
increment = TIMESTAMP_CELL_WIDTH; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
rect = new XRect(x, y, CELL_WIDTH, CELL_HEIGHT); |
|
|
|
|
increment = CELL_WIDTH; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gf.DrawRectangle(HEADER_RECT_STYLE, rect); |
|
|
|
|
gf.DrawRectangle(RECT_EDGE_PEN, rect); |
|
|
|
|
gf.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; |
|
|
|
|
for (int col = 0; col < _table.Columns.Count; col++) |
|
|
|
|
{ |
|
|
|
|
double increment = 0; |
|
|
|
|
XRect rect; |
|
|
|
|
string value; |
|
|
|
|
if (col == _timestampColIdx) |
|
|
|
|
{ |
|
|
|
|
rect = new XRect(x, y, TIMESTAMP_CELL_WIDTH, CELL_HEIGHT); |
|
|
|
|
DateTime date = (DateTime)_table.Rows[row][col]; |
|
|
|
|
value = date.ToString("yyyy-MM-dd HH:mm:ss"); |
|
|
|
|
increment = TIMESTAMP_CELL_WIDTH; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
rect = new XRect(x, y, CELL_WIDTH, CELL_HEIGHT); |
|
|
|
|
value = _table.Rows[row][col].ToString(); |
|
|
|
|
increment = CELL_WIDTH; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gf.DrawRectangle(RECT_EDGE_PEN, rect); |
|
|
|
|
gf.DrawString(value, _font, STRING_COLOR, rect, _format); |
|
|
|
|
|
|
|
|
|
x += increment; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_document.Save(targetPath); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |