refactor(Pdf): Add Background-method to place a background behind the signatures.
This commit is contained in:
parent
bf0bd8e9e7
commit
16657f6a31
@ -21,4 +21,8 @@
|
|||||||
<PackageReference Include="itext.bouncy-castle-adapter" Version="8.0.5" />
|
<PackageReference Include="itext.bouncy-castle-adapter" Version="8.0.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\EnvelopeGenerator.Domain\EnvelopeGenerator.Domain.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,10 +1,14 @@
|
|||||||
using iText.Kernel.Pdf;
|
using iText.Kernel.Pdf;
|
||||||
using iText.Kernel.Pdf.Canvas;
|
using iText.Kernel.Pdf.Canvas;
|
||||||
|
using EnvelopeGenerator.Domain.Interfaces;
|
||||||
|
using iText.Kernel.Colors;
|
||||||
|
using iText.Kernel.Geom;
|
||||||
|
|
||||||
#if NETFRAMEWORK
|
#if NETFRAMEWORK
|
||||||
using System.IO;
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace EnvelopeGenerator.PdfEditor
|
namespace EnvelopeGenerator.PdfEditor
|
||||||
@ -76,6 +80,40 @@ namespace EnvelopeGenerator.PdfEditor
|
|||||||
});
|
});
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
public Pdf<TInputStream, TOutputStream> Background<TSignature>(IEnumerable<TSignature> signatures)
|
||||||
|
where TSignature : ISignature
|
||||||
|
{
|
||||||
|
foreach (var signature in signatures)
|
||||||
|
Page(signature.Page, page =>
|
||||||
|
{
|
||||||
|
var canvas = new PdfCanvas(page);
|
||||||
|
double inchFactor = 72;
|
||||||
|
double magin = .2;
|
||||||
|
double x = (signature.X - .7 - magin) * inchFactor;
|
||||||
|
double y = (signature.Y - .5 - magin) * inchFactor;
|
||||||
|
double width = 1.9500000000000002 * inchFactor;
|
||||||
|
double height = 2.52 * inchFactor;
|
||||||
|
|
||||||
|
double bottomLineLength = 2.5;
|
||||||
|
|
||||||
|
Rectangle pageSize = page.GetPageSize();
|
||||||
|
|
||||||
|
canvas.ConcatMatrix(1, 0, 0, -1, 0, pageSize.GetHeight());
|
||||||
|
|
||||||
|
// draw background
|
||||||
|
canvas.SetFillColor(new DeviceRgb(222, 220, 215))
|
||||||
|
.Rectangle(x, y, width, height)
|
||||||
|
.Fill();
|
||||||
|
|
||||||
|
// draw bottom line
|
||||||
|
canvas.SetFillColor(new DeviceRgb(204, 202, 198))
|
||||||
|
.Rectangle(x, y + height - bottomLineLength, width, bottomLineLength)
|
||||||
|
.Fill();
|
||||||
|
});
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
#region Finalizer
|
#region Finalizer
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
|
|
||||||
|
|||||||
@ -10,8 +10,6 @@ using EnvelopeGenerator.Domain.Constants;
|
|||||||
using EnvelopeGenerator.PdfEditor;
|
using EnvelopeGenerator.PdfEditor;
|
||||||
using EnvelopeGenerator.Web.Extensions;
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
using EnvelopeGenerator.Web.Models;
|
using EnvelopeGenerator.Web.Models;
|
||||||
using iText.Kernel.Colors;
|
|
||||||
using iText.Kernel.Pdf.Canvas;
|
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
@ -224,21 +222,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)
|
||||||
{
|
{
|
||||||
using var pdf = Pdf.FromMemory(doc.ByteData).Design(1, canvas =>
|
using var pdf = Pdf.FromMemory(doc.ByteData).Background(doc.Elements!);
|
||||||
{
|
|
||||||
canvas.SetFillColor(new DeviceRgb(135, 62, 35));
|
|
||||||
|
|
||||||
canvas.Rectangle(100, 500, 200, 100);
|
|
||||||
|
|
||||||
canvas.Fill();
|
|
||||||
|
|
||||||
canvas.SetFillColor(new DeviceRgb(222, 220, 215));
|
|
||||||
|
|
||||||
canvas.SetStrokeColor(new DeviceRgb(135, 62, 35));
|
|
||||||
canvas.Circle(300, 500, 100);
|
|
||||||
canvas.FillStroke();
|
|
||||||
canvas.Fill();
|
|
||||||
});
|
|
||||||
|
|
||||||
doc.ByteData = pdf.ExportAsBytes();
|
doc.ByteData = pdf.ExportAsBytes();
|
||||||
|
|
||||||
|
|||||||
@ -5,35 +5,6 @@ async function createAnnotations(document) {
|
|||||||
for (let element of document.elements) {
|
for (let element of document.elements) {
|
||||||
const annotParams = await getAnnotationParams(element.left, element.top);
|
const annotParams = await getAnnotationParams(element.left, element.top);
|
||||||
const page = element.page - 1
|
const page = element.page - 1
|
||||||
|
|
||||||
//#region background
|
|
||||||
if (annotParams.background) {
|
|
||||||
let background = annotParams.background;
|
|
||||||
const id_background = PSPDFKit.generateInstantId();
|
|
||||||
const annotation_background = new PSPDFKit.Annotations.WidgetAnnotation({
|
|
||||||
id: id_background,
|
|
||||||
pageIndex: page,
|
|
||||||
formFieldName: id_background,
|
|
||||||
backgroundColor: background?.backgroundColor ? new PSPDFKit.Color(background.backgroundColor) : null,
|
|
||||||
blendMode: 'normal',
|
|
||||||
boundingBox: new PSPDFKit.Geometry.Rect(background),
|
|
||||||
fontSize: 8,
|
|
||||||
borderStyle: background.borderStyle,
|
|
||||||
borderWidth: background.borderWidth,
|
|
||||||
borderColor: background?.borderColor ? new PSPDFKit.Color(background.borderColor) : null
|
|
||||||
});
|
|
||||||
|
|
||||||
const formFieldBackground = new PSPDFKit.FormFields.ButtonFormField({
|
|
||||||
name: id_background,
|
|
||||||
annotationIds: PSPDFKit.Immutable.List([annotation_background.id]),
|
|
||||||
value: "",
|
|
||||||
readOnly: true
|
|
||||||
});
|
|
||||||
|
|
||||||
signatures.push(annotation_background)
|
|
||||||
signatures.push(formFieldBackground)
|
|
||||||
}
|
|
||||||
//#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let element of document.elements) {
|
for (let element of document.elements) {
|
||||||
|
|||||||
@ -72,6 +72,7 @@ async function getAnnotationParams(leftInInch = 0, topInInch = 0, inchToPointFac
|
|||||||
annot.top += topInInch - 0.5;
|
annot.top += topInInch - 0.5;
|
||||||
annot.top *= inchToPointFactor;
|
annot.top *= inchToPointFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
return annotParams;
|
return annotParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user