refactor(Extensions): add ToBytes shortcut method
This commit is contained in:
parent
b0b734ecfb
commit
7964cc44fb
@ -1,54 +1,17 @@
|
|||||||
using iText.Kernel.Pdf;
|
#if NETFRAMEWORK
|
||||||
using iText.Kernel.Pdf.Canvas;
|
|
||||||
|
|
||||||
#if NETFRAMEWORK
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace EnvelopeGenerator.PdfEditor
|
namespace EnvelopeGenerator.PdfEditor
|
||||||
#if NET
|
|
||||||
;
|
|
||||||
#elif NETFRAMEWORK
|
|
||||||
{
|
{
|
||||||
#endif
|
|
||||||
|
|
||||||
public static class Extensions
|
public static class Extensions
|
||||||
{
|
{
|
||||||
#region Edit PDF document
|
/// <summary>
|
||||||
public static TStream Edit<TStream>(this TStream inputStream, Action<PdfDocument> edit)
|
/// Writes the stream contents to a byte array, regardless of the System.IO.MemoryStream.Position property.
|
||||||
where TStream : Stream, new()
|
/// </summary>
|
||||||
{
|
/// <typeparam name="TPdf"></typeparam>
|
||||||
using (var outputStream = new TStream())
|
/// <param name="pdf"></param>
|
||||||
{
|
/// <returns>A new byte array</returns>
|
||||||
using (var pdfDoc = new PdfDocument(new PdfReader(inputStream), new PdfWriter(outputStream)))
|
public static byte[] ToBytes<TPdf>(this TPdf pdf) where TPdf : Pdf<MemoryStream, MemoryStream> => pdf.OutputStream.ToArray();
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public static byte[] Design(this byte[] pdfBytes, params (int pageIndex, Action<PdfCanvas> design)[] pageDesign)
|
|
||||||
=> pdfBytes.Edit(doc =>
|
|
||||||
{
|
|
||||||
foreach((int pageIndex, Action<PdfCanvas> design) in pageDesign)
|
|
||||||
{
|
|
||||||
var page = doc.GetPage(pageIndex);
|
|
||||||
var canvas = new PdfCanvas(page);
|
|
||||||
design(canvas);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
#if NETFRAMEWORK
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@ -224,7 +224,7 @@ public class EnvelopeController : ViewControllerBase
|
|||||||
{
|
{
|
||||||
if (er.Envelope!.Documents?.FirstOrDefault() is DocumentDto doc && doc.ByteData is not null)
|
if (er.Envelope!.Documents?.FirstOrDefault() is DocumentDto doc && doc.ByteData is not null)
|
||||||
{
|
{
|
||||||
var edit = Pdf.FromMemory(doc.ByteData).Draw(1, canvas =>
|
using var pdf = Pdf.FromMemory(doc.ByteData).Design(1, canvas =>
|
||||||
{
|
{
|
||||||
canvas.SetStrokeColor(ColorConstants.RED);
|
canvas.SetStrokeColor(ColorConstants.RED);
|
||||||
canvas.SetFillColor(ColorConstants.CYAN);
|
canvas.SetFillColor(ColorConstants.CYAN);
|
||||||
@ -234,11 +234,7 @@ public class EnvelopeController : ViewControllerBase
|
|||||||
canvas.FillStroke();
|
canvas.FillStroke();
|
||||||
});
|
});
|
||||||
|
|
||||||
edit._doc.Close();
|
doc.ByteData = pdf.ToBytes();
|
||||||
|
|
||||||
var foo = Convert.ToBase64String(edit.OutputStream.ToArray());
|
|
||||||
doc.ByteData = edit.OutputStream.ToArray();
|
|
||||||
|
|
||||||
|
|
||||||
ViewData["DocumentBytes"] = doc.ByteData;
|
ViewData["DocumentBytes"] = doc.ByteData;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user