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:
parent
8709bd5c2e
commit
0fa641c15d
42
EnvelopeGenerator.PdfEditor/Extensions.cs
Normal file
42
EnvelopeGenerator.PdfEditor/Extensions.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using iText.Kernel.Colors;
|
||||
using iText.Kernel.Pdf;
|
||||
using iText.Kernel.Pdf.Canvas;
|
||||
|
||||
#if NETFRAMEWORK
|
||||
using System.IO;
|
||||
using System;
|
||||
#endif
|
||||
|
||||
namespace EnvelopeGenerator.PdfEditor
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static TStream Edit<TStream>(this TStream inputStream, Action<PdfDocument> edit)
|
||||
where TStream : Stream, new()
|
||||
{
|
||||
using (var outputStream = new TStream())
|
||||
{
|
||||
using (var pdfDoc = new PdfDocument(new PdfReader(inputStream), new PdfWriter(outputStream)))
|
||||
{
|
||||
edit(pdfDoc);
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Edit(this byte[] pdfBytes, Action<PdfDocument> edit)
|
||||
{
|
||||
using (var inputStream = new MemoryStream(pdfBytes))
|
||||
{
|
||||
return inputStream.Edit(edit).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
@ -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
|
||||
|
||||
@ -2129,6 +2129,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\EnvelopeGenerator.Application\EnvelopeGenerator.Application.csproj" />
|
||||
<ProjectReference Include="..\EnvelopeGenerator.Infrastructure\EnvelopeGenerator.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user