feat(Extensions): Add PDF editing extension methods using iText

- Introduced `Edit` extension methods for `Stream` and `byte[]` to allow
  in-place PDF modifications via a `PdfDocument` action.
- Supports both .NET and .NET Framework projects.
- Ensures proper disposal of streams and PdfDocument instances.
This commit is contained in:
2025-09-24 14:12:35 +02:00
parent 8709bd5c2e
commit 0fa641c15d
3 changed files with 63 additions and 5 deletions

View File

@@ -1,9 +1,17 @@
using DigitalData.Core.Abstraction.Application.DTO;
using DigitalData.Core.API;
using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
using EnvelopeGenerator.Application.Common.Extensions;
using EnvelopeGenerator.Application.Common.Interfaces.Services;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
using EnvelopeGenerator.Application.Resources;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.PdfEditor;
using EnvelopeGenerator.Web.Extensions;
using EnvelopeGenerator.Web.Models;
using iText.Kernel.Colors;
using iText.Kernel.Pdf.Canvas;
using MediatR;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
@@ -11,11 +19,6 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using OtpNet;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
using EnvelopeGenerator.Application.Common.Extensions;
using EnvelopeGenerator.Application.Common.Interfaces.Services;
namespace EnvelopeGenerator.Web.Controllers;
@@ -221,6 +224,18 @@ public class EnvelopeController : ViewControllerBase
{
if (er.Envelope!.Documents?.FirstOrDefault() is DocumentDto doc && doc.ByteData is not null)
{
doc.ByteData = doc.ByteData.Edit(doc =>
{
var page = doc.GetFirstPage();
var canvas = new PdfCanvas(page);
canvas.SetStrokeColor(ColorConstants.RED);
canvas.SetFillColor(ColorConstants.CYAN);
canvas.SetFillColorRgb(222, 220, 215);
canvas.SetLineWidth(2);
canvas.Rectangle(100, 500, 200, 100);
canvas.FillStroke();
});
ViewData["DocumentBytes"] = doc.ByteData;
}
else