Files
DocumentService/DocumentOperator.Application/ConvertToPdfA/ConvertToPdfACommand.cs
TekH a6694bfce7 Add PDF attachment and PDF/A conversion features
Introduced endpoints for embedding attachments in PDFs and converting
between standard PDFs and PDF/A formats. Added `PdfAttachmentController`
and `PdfConversionController` with multipart/form-data and Base64-based
support. Implemented commands, handlers, and validators for these
operations.

Extended `IPdfProcessor` with methods for adding attachments and
PDF/A conversion. Partially implemented functionality in
`DevExpressPdfProcessor`, including `ConvertFromPdfAAsync`.

Added `attachment.xml` and `withoutAttachment.pdf` as resources for
testing. Marked endpoints as `[Obsolete]` to indicate incomplete
implementation. Improved validation and error handling for commands.
2026-07-30 13:43:55 +02:00

60 lines
1.8 KiB
C#

using DocumentOperator.Application.Common.Interfaces;
using FluentValidation;
using MediatR;
namespace DocumentOperator.Application.ConvertToPdfA;
/// <summary>
/// Command to convert a standard PDF to PDF/A format
/// </summary>
public record ConvertToPdfACommand : IRequest<byte[]>
{
/// <summary>
/// PDF document stream. Must be positioned at the beginning (Position = 0).
/// </summary>
public required Stream PdfStream { get; init; }
/// <summary>
/// Target PDF/A level (e.g., "PDF/A-1b", "PDF/A-2b", "PDF/A-3b")
/// </summary>
public required string PdfALevel { get; init; }
}
/// <summary>
/// Handler for ConvertToPdfACommand
/// </summary>
public class ConvertToPdfACommandHandler(IPdfProcessor pdfProcessor)
: IRequestHandler<ConvertToPdfACommand, byte[]>
{
public async Task<byte[]> Handle(ConvertToPdfACommand request, CancellationToken cancellationToken)
{
return await pdfProcessor.ConvertToPdfAAsync(request.PdfStream, request.PdfALevel);
}
}
/// <summary>
/// Validator for ConvertToPdfACommand
/// </summary>
public class ConvertToPdfACommandValidator : AbstractValidator<ConvertToPdfACommand>
{
private static readonly string[] ValidPdfALevels =
{
"PDF/A-1b", "PDF/A-1a",
"PDF/A-2b", "PDF/A-2u", "PDF/A-2a",
"PDF/A-3b", "PDF/A-3u", "PDF/A-3a"
};
public ConvertToPdfACommandValidator()
{
RuleFor(x => x.PdfStream)
.NotNull()
.WithMessage("PDF stream is required");
RuleFor(x => x.PdfALevel)
.NotEmpty()
.WithMessage("PDF/A level is required")
.Must(level => ValidPdfALevels.Contains(level, StringComparer.OrdinalIgnoreCase))
.WithMessage($"Invalid PDF/A level. Valid values: {string.Join(", ", ValidPdfALevels)}");
}
}