我使用ModelDoc创建了一个pdf文档。创建后,我使用PDFSharp在文档中插入数字签名。
正确看到数字签名,我也能够执行签名验证的正常任务。
但是,打印文档时不会打印数字签名。我已经尝试过使用“文档和标记”和“文档和邮票”。
但是,签名仍未打印。这是数字签名的代码。
void ISignatureAppearanceHandler.DrawAppearance(XGraphics gfx, XRect rect)
{
string signtext = "This is a digital signature";
XFont font = new XFont("Verdana", 8.0, XFontStyle.Regular);
XPoint xPoint = new XPoint(0.0, 0.0);
gfx.DrawRectangle(
XBrushes.LightBlue, xPoint.X,
xPoint.Y, rect.Width, rect.Height
);
XTextFormatter xTextFormatter = new XTextFormatter(gfx);
xTextFormatter.DrawString(
signtext, font,
new XSolidBrush(
XColor.FromKnownColor(XKnownColor.Black)
),
new XRect(
xPoint.X, xPoint.Y, rect.Width, rect.Height
),
XStringFormats.TopLeft
);
}
尝试在启动PdfSignatureHandler之前打印文本它对我来说很好。
private static void SignExisting()
{
double height = 40;
double width = 200;
double x = 0;
double y = 0;
PdfDocument pdfDocument = PdfReader.Open(path);
PdfPage pp = pdfDocument.Pages[0];
var cert = GetCertificate();
string signtext = "Digitaly Signed by " + cert.GetNameInfo(X509NameType.SimpleName, false);
XGraphics gfx = XGraphics.FromPdfPage(pp);
XFont font = new XFont("Arial", 10, XFontStyle.Regular);
gfx.DrawString(signtext, font, XBrushes.Black, new XRect(x, pp.Height - y, pp.Width, pp.Height), XStringFormats.TopLeft);
gfx.DrawString("Date: " + DateTime.Now.ToString(), font, XBrushes.Black, new XRect(x, pp.Height - y, pp.Width, pp.Height), XStringFormats.TopLeft);
PdfSignatureOptions options = new PdfSignatureOptions
{
ContactInfo = cert.GetNameInfo(X509NameType.SimpleName, false),
Location = cert.Subject,
Reason = "",
Rectangle = new XRect(x, y, width, height),
AppearanceHandler = new SignAppearenceHandler()
};
PdfSignatureHandler pdfSignatureHandler = new PdfSignatureHandler(cert, null, options);
pdfSignatureHandler.AttachToDocument(pdfDocument);
Stream stream = new MemoryStream();
pdfDocument.Save(stream);
}