add OutputStream getter

This commit is contained in:
tekh 2025-09-24 17:49:15 +02:00
parent 54c21556f6
commit ac29fac88d
2 changed files with 35 additions and 15 deletions

View File

@ -7,6 +7,14 @@ using System.Threading.Tasks;
namespace EnvelopeGenerator.PdfEditor
{
public static class Edit
{
public static Edit<MemoryStream, MemoryStream> FromMemory(byte[] documentBytes)
{
return new Edit<MemoryStream, MemoryStream>(new MemoryStream(documentBytes), new MemoryStream());
}
}
public class Edit<TInputStream, TOutputStream> : IDisposable, IAsyncDisposable
where TInputStream : Stream
where TOutputStream : Stream
@ -26,6 +34,26 @@ namespace EnvelopeGenerator.PdfEditor
_doc = new PdfDocument(_reader, _writer);
}
/// <summary>
/// Gets the output stream containing the edited PDF document.
/// </summary>
/// <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.
/// </remarks>
/// <returns>
/// The <typeparamref name="TOutputStream"/> instance that contains the updated PDF bytes.
/// </returns>
public TOutputStream OutputStream
{
get
{
_doc.Close();
return _outputStream;
}
}
public static Edit<MemoryStream, MemoryStream> OnByte(byte[] documentBytes)
{
return new Edit<MemoryStream, MemoryStream>(new MemoryStream(documentBytes), new MemoryStream());

View File

@ -208,7 +208,7 @@ public class EnvelopeController : ViewControllerBase
//add PSPDFKit licence key
ViewData["PSPDFKitLicenseKey"] = _configuration["PSPDFKitLicenseKey"];
return View("ShowEnvelope", er);
return await CreateShowEnvelopeView(er);
}
else
{
@ -224,10 +224,8 @@ public class EnvelopeController : ViewControllerBase
{
if (er.Envelope!.Documents?.FirstOrDefault() is DocumentDto doc && doc.ByteData is not null)
{
doc.ByteData = doc.ByteData.Edit(doc =>
var edit = Edit.FromMemory(doc.ByteData).Draw(1, canvas =>
{
var page = doc.GetFirstPage();
var canvas = new PdfCanvas(page);
canvas.SetStrokeColor(ColorConstants.RED);
canvas.SetFillColor(ColorConstants.CYAN);
canvas.SetFillColorRgb(222, 220, 215);
@ -236,17 +234,11 @@ public class EnvelopeController : ViewControllerBase
canvas.FillStroke();
});
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();
});
edit._doc.Close();
var foo = Convert.ToBase64String(edit.OutputStream.ToArray());
doc.ByteData = edit.OutputStream.ToArray();
ViewData["DocumentBytes"] = doc.ByteData;
}