Refactor signature box layout and improve readability

Refactored the signature box layout to dynamically calculate
positions based on content, improving flexibility and precision.
Introduced new constants (`lineH`, `bgPad`) to standardize
spacing and replaced hardcoded values for better maintainability.
Adjusted background rectangle sizing to fit content dynamically
and improved text layout logic to handle optional fields more
gracefully. Simplified image area logic and reduced redundant
calculations. Overall, improved code readability and alignment
for a cleaner, more compact layout.
This commit is contained in:
2026-07-01 11:38:07 +02:00
parent bc34317720
commit 789e312316

View File

@@ -208,17 +208,16 @@
const double sigW = 1.77 * 72; // 127.44 pt const double sigW = 1.77 * 72; // 127.44 pt
const double sigH = 1.96 * 72; // 141.12 pt const double sigH = 1.96 * 72; // 141.12 pt
const double imgRatio = 0.52; // top 52% = image (was 0.60) const double imgRatio = 0.52; // top 52% = image
const double textRatio = 0.43; // bottom 43% = text (was 0.38) const double lineH = 11.5; // fixed row height matching font size (bold 7.5pt + normal 6.5pt)
const double bgPad = 3.0; // background box padding around content (pt)
var black = PdfSharp.Drawing.XColor.FromArgb(255, 20, 20, 20); var black = PdfSharp.Drawing.XColor.FromArgb(255, 20, 20, 20);
var darkGray = PdfSharp.Drawing.XColor.FromArgb(255, 80, 80, 80); var darkGray = PdfSharp.Drawing.XColor.FromArgb(255, 80, 80, 80);
var lineColor = PdfSharp.Drawing.XColor.FromArgb(180, 100, 100, 120); var lineColor = PdfSharp.Drawing.XColor.FromArgb(180, 100, 100, 120);
// Background: paper/cream tone, no border
var bgColor = PdfSharp.Drawing.XColor.FromArgb(255, 255, 253, 240); var bgColor = PdfSharp.Drawing.XColor.FromArgb(255, 255, 253, 240);
var bgBrush = new PdfSharp.Drawing.XSolidBrush(bgColor); var bgBrush = new PdfSharp.Drawing.XSolidBrush(bgColor);
const double bgPad = 3.0; // extra padding around the signature box (pt)
var fontBold = new PdfSharp.Drawing.XFont("Arial", 7.5, PdfSharp.Drawing.XFontStyleEx.Bold); var fontBold = new PdfSharp.Drawing.XFont("Arial", 7.5, PdfSharp.Drawing.XFontStyleEx.Bold);
var fontNormal = new PdfSharp.Drawing.XFont("Arial", 6.5, PdfSharp.Drawing.XFontStyleEx.Regular); var fontNormal = new PdfSharp.Drawing.XFont("Arial", 6.5, PdfSharp.Drawing.XFontStyleEx.Regular);
@@ -244,44 +243,52 @@
double x = field.X; double x = field.X;
double y = field.Y; double y = field.Y;
// --- Background rectangle (drawn first, sits between PDF and signature) --- // --- Calculate layout positions first (needed for bg rect) ---
var bgRect = new PdfSharp.Drawing.XRect(x - bgPad, y - bgPad, sigW + bgPad * 2, sigH + bgPad * 2); double imgH = sigH * imgRatio;
double lineY = y + imgH + 1.0; // 1pt gap between image and separator
double textY = lineY + 1.5; // 1.5pt gap below separator line
double padding = 3;
// Row 1: FullName
double row1Y = textY;
// Row 2: Position (optional)
double row2Y = row1Y + lineH;
// Row 3: Place, Date — immediately after row2 regardless of position
double row3Y = !string.IsNullOrWhiteSpace(sig.Position) ? row2Y + lineH : row2Y;
double contentBottom = row3Y + lineH;
// --- Background rectangle sized to actual content (not full sigH) ---
var bgRect = new PdfSharp.Drawing.XRect(
x - bgPad,
y - bgPad,
sigW + bgPad * 2,
(contentBottom - y) + bgPad * 2);
gfx.DrawRectangle(bgBrush, bgRect); gfx.DrawRectangle(bgBrush, bgRect);
// --- Image area --- // --- Image area ---
double imgH = sigH * imgRatio;
var imgRect = new PdfSharp.Drawing.XRect(x, y, sigW, imgH); var imgRect = new PdfSharp.Drawing.XRect(x, y, sigW, imgH);
using var imgStream = new System.IO.MemoryStream(imgBytes); using var imgStream = new System.IO.MemoryStream(imgBytes);
var xImg = PdfSharp.Drawing.XImage.FromStream(imgStream); var xImg = PdfSharp.Drawing.XImage.FromStream(imgStream);
gfx.DrawImage(xImg, imgRect); gfx.DrawImage(xImg, imgRect);
// --- Separator line --- // --- Separator line ---
double lineY = y + imgH + sigH * 0.005; // was 0.01 — tighter gap after image
gfx.DrawLine(linePen, x + 2, lineY, x + sigW - 2, lineY); gfx.DrawLine(linePen, x + 2, lineY, x + sigW - 2, lineY);
// --- Text area --- // --- Text rows ---
double textY = lineY + 1; // was +2 — tighter gap below line
double textH = sigH * textRatio;
double lineH = textH / 3.0; // was /3.5 — rows closer together
double padding = 3;
// Row 1: FullName (bold) // Row 1: FullName (bold)
var nameRect = new PdfSharp.Drawing.XRect(x + padding, textY, sigW - padding * 2, lineH); var nameRect = new PdfSharp.Drawing.XRect(x + padding, row1Y, sigW - padding * 2, lineH);
gfx.DrawString(sig.FullName, fontBold, new PdfSharp.Drawing.XSolidBrush(black), nameRect, fmtLeft); gfx.DrawString(sig.FullName, fontBold, new PdfSharp.Drawing.XSolidBrush(black), nameRect, fmtLeft);
// Row 2: Position (optional) // Row 2: Position (optional)
double row2Y = textY + lineH;
if (!string.IsNullOrWhiteSpace(sig.Position)) if (!string.IsNullOrWhiteSpace(sig.Position))
{ {
var posRect = new PdfSharp.Drawing.XRect(x + padding, row2Y, sigW - padding * 2, lineH); var posRect = new PdfSharp.Drawing.XRect(x + padding, row2Y, sigW - padding * 2, lineH);
gfx.DrawString(sig.Position, fontNormal, new PdfSharp.Drawing.XSolidBrush(darkGray), posRect, fmtLeft); gfx.DrawString(sig.Position, fontNormal, new PdfSharp.Drawing.XSolidBrush(darkGray), posRect, fmtLeft);
row2Y += lineH;
} }
// Row 3 (or 2 if no position): Place, Date // Row 3: Place, Date
var placeDate = $"{sig.Place}, {date}"; var placeDate = $"{sig.Place}, {date}";
var dateRect = new PdfSharp.Drawing.XRect(x + padding, row2Y, sigW - padding * 2, lineH); var dateRect = new PdfSharp.Drawing.XRect(x + padding, row3Y, sigW - padding * 2, lineH);
gfx.DrawString(placeDate, fontNormal, new PdfSharp.Drawing.XSolidBrush(darkGray), dateRect, fmtLeft); gfx.DrawString(placeDate, fontNormal, new PdfSharp.Drawing.XSolidBrush(darkGray), dateRect, fmtLeft);
} }