无法打印数字签名

时间:2018-03-15 10:08:52

标签: pdfsharp

我使用ModelDoc创建了一个pdf文档。创建后,我使用PDFSharp在文档中插入数字签名。 正确看到数字签名,我也能够执行签名验证的正常任务。 但是,打印文档时不会打印数字签名。我已经尝试过使用"文档和标记"和"文件和邮票"。 但是,签名仍未打印。这是数字签名的代码。 void ISignatureAppearanceHandler.DrawAppearance(XGraphics gfx,XRect rect)     {         string signtext ="这是一个数字签名&#34 ;;         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,字体,             新的XSolidBrush(                 XColor.FromKnownColor(XKnownColor.Black)             )             新XRect(                 xPoint.X,xPoint.Y,rect.Width,rect.Height             )            XStringFormats.TopLeft         );     }

1 个答案:

答案 0 :(得分:0)

在启动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);
    }