refactor(Extensions): add ToBytes shortcut method
This commit is contained in:
@@ -1,54 +1,17 @@
|
||||
using iText.Kernel.Pdf;
|
||||
using iText.Kernel.Pdf.Canvas;
|
||||
|
||||
#if NETFRAMEWORK
|
||||
#if NETFRAMEWORK
|
||||
using System.IO;
|
||||
using System;
|
||||
#endif
|
||||
|
||||
namespace EnvelopeGenerator.PdfEditor
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
#region Edit PDF document
|
||||
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();
|
||||
}
|
||||
}
|
||||
#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);
|
||||
}
|
||||
});
|
||||
/// <summary>
|
||||
/// Writes the stream contents to a byte array, regardless of the System.IO.MemoryStream.Position property.
|
||||
/// </summary>
|
||||
/// <typeparam name="TPdf"></typeparam>
|
||||
/// <param name="pdf"></param>
|
||||
/// <returns>A new byte array</returns>
|
||||
public static byte[] ToBytes<TPdf>(this TPdf pdf) where TPdf : Pdf<MemoryStream, MemoryStream> => pdf.OutputStream.ToArray();
|
||||
}
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user