refactor(Edit): rename as Pdf

- add Page and Design methods as shortcut methods
This commit is contained in:
tekh 2025-09-25 11:06:03 +02:00
parent 2d6347ffa6
commit b0b734ecfb
2 changed files with 28 additions and 9 deletions

View File

@ -1,4 +1,6 @@
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
#if NETFRAMEWORK
using System.IO;
using System;
@ -7,15 +9,15 @@ using System.Threading.Tasks;
namespace EnvelopeGenerator.PdfEditor
{
public static class Edit
public static class Pdf
{
public static Edit<MemoryStream, MemoryStream> FromMemory(byte[] documentBytes)
public static Pdf<MemoryStream, MemoryStream> FromMemory(byte[] documentBytes)
{
return new Edit<MemoryStream, MemoryStream>(new MemoryStream(documentBytes), new MemoryStream());
return new Pdf<MemoryStream, MemoryStream>(new MemoryStream(documentBytes), new MemoryStream());
}
}
public class Edit<TInputStream, TOutputStream> : IDisposable, IAsyncDisposable
public class Pdf<TInputStream, TOutputStream> : IDisposable, IAsyncDisposable
where TInputStream : Stream
where TOutputStream : Stream
{
@ -25,7 +27,7 @@ namespace EnvelopeGenerator.PdfEditor
private readonly PdfReader _reader;
private readonly PdfWriter _writer;
public Edit(TInputStream inputStream, TOutputStream outputStream)
public Pdf(TInputStream inputStream, TOutputStream outputStream)
{
_inputStream = inputStream;
_outputStream = outputStream;
@ -40,7 +42,7 @@ namespace EnvelopeGenerator.PdfEditor
/// <remarks>
/// Accessing this property will close the underlying <see cref="PdfDocument"/> to ensure
/// all changes are flushed to the stream. After accessing this property, the PDF document
/// can no longer be modified using this <see cref="Edit{TInputStream, TOutputStream}"/> instance.
/// can no longer be modified using this <see cref="Pdf{TInputStream, TOutputStream}"/> instance.
/// </remarks>
/// <returns>
/// The <typeparamref name="TOutputStream"/> instance that contains the updated PDF bytes.
@ -54,16 +56,33 @@ namespace EnvelopeGenerator.PdfEditor
}
}
public Edit<TInputStream, TOutputStream> Document(Action<PdfDocument> edit)
#region Edit methods
public Pdf<TInputStream, TOutputStream> Document(Action<PdfDocument> edit)
{
edit(_doc);
return this;
}
public Pdf<TInputStream, TOutputStream> Page(int pageIndex, Action<PdfPage> design)
=> Document(doc =>
{
var page = doc.GetPage(pageIndex);
design(doc.GetPage(pageIndex));
});
public Pdf<TInputStream, TOutputStream> Design(int pageIndex, Action<PdfCanvas> design)
=> Document(doc =>
{
var page = doc.GetPage(pageIndex);
var canvas = new PdfCanvas(page);
design(canvas);
});
#endregion
#region Finalizer
private bool _disposed = false;
~Edit()
~Pdf()
{
// If Dispose is not called, clean up unmanaged resources
Dispose(false);

View File

@ -224,7 +224,7 @@ public class EnvelopeController : ViewControllerBase
{
if (er.Envelope!.Documents?.FirstOrDefault() is DocumentDto doc && doc.ByteData is not null)
{
var edit = Edit.FromMemory(doc.ByteData).Draw(1, canvas =>
var edit = Pdf.FromMemory(doc.ByteData).Draw(1, canvas =>
{
canvas.SetStrokeColor(ColorConstants.RED);
canvas.SetFillColor(ColorConstants.CYAN);