From 6f31d7b1d0df859cb44a4435aa9689a2c1e5b7c9 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 29 Sep 2025 15:06:24 +0200 Subject: [PATCH] refactor(pdf): optimize background rendering with single concat per page - Added `System.Linq` import to enable distinct and ordered page index selection - Applied `ConcatMatrix` transformation once per page instead of per signature - Improved performance and reduced redundant operations in `Background` method --- EnvelopeGenerator.PdfEditor/Pdf.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/EnvelopeGenerator.PdfEditor/Pdf.cs b/EnvelopeGenerator.PdfEditor/Pdf.cs index 6886f0b7..5c218500 100644 --- a/EnvelopeGenerator.PdfEditor/Pdf.cs +++ b/EnvelopeGenerator.PdfEditor/Pdf.cs @@ -6,6 +6,7 @@ using iText.Kernel.Geom; #if NETFRAMEWORK using System; using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; #endif @@ -82,6 +83,16 @@ namespace EnvelopeGenerator.PdfEditor public Pdf Background(IEnumerable signatures) where TSignature : ISignature { + // once per page + var pageIndexes = signatures.Select(signature => signature.Page).Distinct().OrderBy(index => index); + foreach(var pageIndex in pageIndexes) + { + var page = _doc.GetPage(pageIndex); + Rectangle pageSize = page.GetPageSize(); + var canvas = new PdfCanvas(page); + canvas.ConcatMatrix(1, 0, 0, -1, 0, pageSize.GetHeight()); + } + foreach (var signature in signatures) Page(signature.Page, page => { @@ -95,10 +106,6 @@ namespace EnvelopeGenerator.PdfEditor 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)