Recently, I've been trying to utilize PdfSharp to add a signature to a fillable form. I was hoping to utilize Kaushan Script (
https://fonts.google.com/specimen/Kaushan+Script) for the signature, as that is what I'm utilizing on the front end as well as for when the signature is added by box coordinates. When I go to set the font on a PdfTextField, the font stays the same as the font was before even though the text is added.
Here is how I am adding it:
Code:
public void AddSignatureToFillableForm(IPdfEncryptedStreamProvider streamProvider, string signature)
{
using (var pdfDoc = PdfReader.Open(streamProvider.GetDecryptedReadStream(), PdfDocumentOpenMode.Modify))
{
if (!pdfDoc.AcroForm.Elements.ContainsKey("/NeedAppearances"))
{
pdfDoc.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
}
else
{
pdfDoc.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
}
for (int i = 0; i < pdfDoc.AcroForm.Fields.Count; i++)
{
var field = pdfDoc.AcroForm.Fields[i];
if (field.Name.Equals("Signature"))
{
(field as PdfTextField).Font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, 12);
field.Value = new PdfString(signature);
}
else if (field.Name.Equals("Initials"))
{
(field as PdfTextField).Font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, 12);
field.Value = new PdfString(createInitials(signature));
}
}
pdfDoc.Save(streamProvider.GetEncryptedWriteStream());
}
}
I've tried changing the Default Appearance value as well, and that ends up creating an empty string when set to Kaushan-Script Regular.
This is the working snippet for using KaushanScript when drawing the string just to show that it isn't necessarily with the Font Resolver.
Code:
public void AddSignatureToCoordinateBox(IPdfEncryptedStreamProvider streamProvider, long x, long y, long height, long width, string signature, long fontSize)
{
var adjustedFontSize = GetAdjustedFontSize(width, height, fontSize, signature, streamProvider);
var adjustedTop = y - (adjustedFontSize - fontSize);
using (var pdfDoc = PdfReader.Open(streamProvider.GetDecryptedReadStream(), PdfDocumentOpenMode.Modify))
{
var pdfPage = pdfDoc.Pages[0];
var sigRectangle = new XRect(x, adjustedTop, width, height);
var gfx = XGraphics.FromPdfPage(pdfPage);
var font = new XFont(SignatureConstants.SupportedFonts.KaushanScriptRegular, adjustedFontSize);
var textFormatter = new XTextFormatter(gfx);
textFormatter.Alignment = XParagraphAlignment.Center;
textFormatter.DrawString(signature, font, XBrushes.Black, sigRectangle);
Logger.LogInformation($"Adding signature {signature} to document.");
pdfDoc.Save(streamProvider.GetEncryptedWriteStream());
}
}
Just wasn't sure if this was supported and before I asked more questions wanted to double check. Thanks for the help.