为可见签名嵌入字体

时间:2017-08-25 10:51:23

标签: c# pdf itext

我尝试在PDF中添加可见签名,并且要实现PDF / A一致性,必须嵌入字体。以下代码:

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");

抛出此异常:

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

这很有道理,但问题在于我无法弄清楚如何实际嵌入字体。在我找到的iText文档中没有多大帮助,但在搜索了一下之后我最终尝试了这个:

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

还有这个:

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

仍然没有运气。

如何将字体嵌入可见的签名中?

1 个答案:

答案 0 :(得分:0)

正如@mkl所提到的,iText不会嵌入标准类型1字体的字体。提供字体为.ttf文件而不是工作:

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