main
syneffort 1 year ago
parent cbbc2bc5c4
commit 7e787aaa91
  1. 6
      PLibs/PLibs.sln
  2. 138
      PLibs/PdfHelper/PdfHelper.cs
  3. 57
      PLibs/PdfHelper/PdfHelper.csproj
  4. 36
      PLibs/PdfHelper/Properties/AssemblyInfo.cs
  5. 4
      PLibs/PdfHelper/packages.config

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelHelper", "ExcelHelper\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelHelperCore", "ExcelHelperCore\ExcelHelperCore.csproj", "{0DC75403-4084-462B-936B-FA86EA90811B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfHelper", "PdfHelper\PdfHelper.csproj", "{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{0DC75403-4084-462B-936B-FA86EA90811B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DC75403-4084-462B-936B-FA86EA90811B}.Release|Any CPU.Build.0 = Release|Any CPU
{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -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);
}
}
}

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4F62138C-7B92-4FF8-8CB3-F37980AE99F2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PdfHelper</RootNamespace>
<AssemblyName>PdfHelper</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PdfSharp, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>..\packages\PDFsharp.1.50.5147\lib\net20\PdfSharp.dll</HintPath>
</Reference>
<Reference Include="PdfSharp.Charting, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>..\packages\PDFsharp.1.50.5147\lib\net20\PdfSharp.Charting.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PdfHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("PdfHelper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PdfHelper")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에
// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면
// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요.
[assembly: ComVisible(false)]
// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다.
[assembly: Guid("4f62138c-7b92-4ff8-8cb3-f37980ae99f2")]
// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다.
//
// 주 버전
// 부 버전
// 빌드 번호
// 수정 버전
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를
// 기본값으로 할 수 있습니다.
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="PDFsharp" version="1.50.5147" targetFramework="net40" />
</packages>
Loading…
Cancel
Save