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:
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
|
||||
Reference in New Issue
Block a user