Files
EnvelopeGenerator/EnvelopeGenerator.ReceiverUI/Models/SignatureDto.cs
TekH b888c85937 Add SignatureDtoExtensions with Convert<T> method
Introduce a new static class `SignatureDtoExtensions` that adds
a `Convert<T>` extension method for collections of `SignatureDto`
objects. This method converts all `SignatureDto` instances in a
collection to a specified `UnitOfLength`, modifying them in place
and returning the same collection.

The method includes:
- A generic constraint to support any `IEnumerable<SignatureDto>`.
- Null checks to ensure the collection is not null.
- Comprehensive XML documentation with usage examples and notes.
2026-06-07 11:07:49 +02:00

101 lines
3.5 KiB
C#

using EnvelopeGenerator.ReceiverUI.Models.Constants;
namespace EnvelopeGenerator.ReceiverUI.Models;
/// <summary>
/// Represents a signature position on a PDF page.
/// Coordinates stored in INCHES (GdPicture14 native unit).
/// Origin: Top-left corner, X increases right, Y increases down.
/// </summary>
public class SignatureDto
{
/// <summary>Unique identifier.</summary>
public int Id { get; init; }
private double _x;
private double _y;
/// <summary>Horizontal position in INCHES from left edge.</summary>
public double X
{
get => _x * Factor;
init => _x = value;
}
/// <summary>Vertical position in INCHES from top edge.</summary>
public double Y
{
get => _y * Factor;
init => _y = value;
}
/// <summary>1-based page number.</summary>
public int Page { get; init; }
/// <summary>Sender application type that created this signature.</summary>
public SenderAppType SenderAppType { get; init; }
private UnitOfLength _unitOfLength;
public SignatureDto Convert(UnitOfLength unitOfLength)
{
_unitOfLength = unitOfLength;
return this;
}
public double Factor
{
get
{
if (SenderAppType != SenderAppType.LegacyFormApp)
{
throw new NotImplementedException(
$"SenderAppType '{SenderAppType}' is not yet implemented. " +
$"Currently, only '{nameof(SenderAppType.LegacyFormApp)}' is supported. " +
$"Future implementations will handle '{nameof(SenderAppType.ReceiverUIBlazorApp)}' and other types.");
}
// LegacyFormApp uses GdPicture14 with INCHES
return _unitOfLength switch
{
UnitOfLength.Inch => 1.0, // No conversion needed: INCHES → INCHES
UnitOfLength.Point => 72.0, // INCHES → PDF Points: 1 inch = 72 points (PDF standard, NOT pixels!)
_ => throw new InvalidOperationException(
$"Unknown UnitOfLength: {_unitOfLength}. Expected '{nameof(UnitOfLength.Inch)}' or '{nameof(UnitOfLength.Point)}'.")
};
}
}
}
public static class SignatureDtoExtensions
{
/// <summary>
/// Converts all signatures in the collection to the specified unit of length.
/// </summary>
/// <typeparam name="T">Type of the collection (IEnumerable, List, etc.)</typeparam>
/// <param name="signatures">Collection of SignatureDto objects to convert.</param>
/// <param name="unitOfLength">Target unit of measurement (Inch or Point).</param>
/// <returns>The same collection with all signatures converted to the specified unit.</returns>
/// <exception cref="ArgumentNullException">Thrown when signatures collection is null.</exception>
/// <remarks>
/// <b>Usage:</b>
/// <code>
/// var signatures = await SignatureService.GetAsync(envelopeKey);
/// var convertedSignatures = signatures.ConvertAll(UnitOfLength.Point);
/// </code>
/// <b>Note:</b> This method modifies each SignatureDto object in place and returns the same collection.
/// </remarks>
public static T Convert<T>(this T signatures, UnitOfLength unitOfLength)
where T : IEnumerable<SignatureDto>
{
if (signatures == null)
throw new ArgumentNullException(nameof(signatures));
foreach (var signature in signatures)
{
signature.Convert(unitOfLength);
}
return signatures;
}
}