I'm trying to add a custom signature appearance in a PDF with iText7, now I want to rotate the whole signature (90 degrees, to put it in the lateral margin of the document). my code is:
PdfReader reader = new PdfReader(inputPDF);
using (FileStream os = new FileStream(outputPDF, FileMode.Create))
{
PdfSigner signer = new PdfSigner(reader, os, new StampingProperties());
StampingProperties properties = new StampingProperties();
SignatureFieldAppearance sfa = new SignatureFieldAppearance("TestSignature");
signer.SetSignatureCreator("Me");
signer.SetSignatureAppearance(sfa);
signer.SetReason(!string.IsNullOrEmpty(SigReason) ? SigReason.ToUpper() : string.Empty);
signer.SetContact(contact);
signer.SetLocation(!string.IsNullOrEmpty(SigLocation) ? SigLocation.ToUpper() : string.Empty);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Documento firmato digitalmente da " + name.ToUpper());
sb.AppendLine(now.ToString("dddd dd/MM/yyyy").ToUpper() + " alle " + now.ToString("HH:mm:ss"));
if (!string.IsNullOrEmpty(SigReason))
sb.AppendLine("Motivo della firma: " + SigReason.ToUpper());
if (!string.IsNullOrEmpty(SigLocation))
sb.AppendLine("Luogo Firma: " + SigLocation.ToUpper());
var assembly = typeof(PaDESSigner).GetTypeInfo().Assembly;
byte[] imgBytes = new byte[0];
using (var stream = new MemoryStream())
{
Resources.pdf_signatureLogo.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
imgBytes = stream.ToArray();
}
iText.IO.Image.ImageData img = iText.IO.Image.ImageDataFactory.Create(imgBytes);
img.SetXYRatio(0.5f);
img.SetRotation(angle);
sfa.SetContent(sb.ToString(), img);
AffineTransform af = new AffineTransform();
af.Rotate(Math.PI / 180 * angle);
iText.Kernel.Geom.Rectangle rectangle = new iText.Kernel.Geom.Rectangle(x, y, w, h);
signer.SetPageRect(rectangle);
signer.SetPageNumber(_page);
PdfFormXObject foregroundLayer = new PdfFormXObject(rectangle);
PdfCanvas canvas = new PdfCanvas(foregroundLayer, signer.GetDocument());
canvas.ConcatMatrix(af);
canvas.SaveState();
signer.SetSignDate(now);
signer.GetSignatureField().SetSignatureAppearanceLayer(foregroundLayer);
signer.GetDocument().GetCatalog().SetModified();
X509Certificate2Signature externalSignature = new X509Certificate2Signature(myCert, "SHA256");
signer.SignDetached(externalSignature, chain.ToArray(), null, null, null, 0, PdfSigner.CryptoStandard.CMS);
try
{
File.Delete(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "Temp" + System.IO.Path.DirectorySeparatorChar + "t0.pdf");
}
catch { }
try
{
File.Delete(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "Temp" + System.IO.Path.DirectorySeparatorChar + "t2.pdf");
}
catch { }
return true;
}
Now, if I don't use the canvas and "foregroundLayer" everything works except the signature doesn't - obviously - rotate.
With my actual code the pdf is signed, but the signature field is invisible, or, I think, goes on a layer under the actual visible document.
How can I remedy? I think I have to "associate" the canvas to a layer on top, but I don't know how, nor if it's the right path.