Boxiom Boxiom - 2 months ago 11
C# Question

Embed font for visible signature

I'm trying to add a visible signature to a PDF, and to achieve PDF/A conformance the font must be embedded. The following code:

var pdfReader = new PdfReader(pdfToSign);
var os = new FileStream(tempFilePath, FileMode.OpenOrCreate);

var pdfStamper = PdfStamper.CreateSignature(pdfReader, os, char.MinValue);
pdfStamper.Writer.PDFXConformance = PdfWriter.PDFX32002;
pdfStamper.Writer.CreateXmpMetadata();
pdfStamper.FormFlattening = true;

var sap = pdfStamper.SignatureAppearance;
sap.SetVisibleSignature(new Rectangle(0, 748, 144, 780), 1, "Signature");


throws this exception:

iTextSharp.text.pdf.PdfXConformanceException: All the fonts must be embedded. This one isn't: Helvetica


This makes perfect sense, but the problem is that I can't figure out how to actually embed the font. There's not much help in the iText documentation I found, but after searching around a bit I ended up trying this:

var font = FontFactory.GetFont(FontFactory.HELVETICA, FontFactory.DefaultEncoding, embedded: true);
sap.Layer2Font = font;


And also this:

var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
sap.Layer2Font = new Font(baseFont);


Still no luck.

How do I embed the font in a visible signature?

Answer Source

As @mkl mentioned, iText will not embed fonts that are Standard Type 1 fonts. Providing the font as .ttf-file instead worked:

var baseFont = BaseFont.CreateFont(@"D:\Fonts\Helvetica-Regular.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);