使用iText的自定义签名外观 [英] Custom signature appearance using iText

查看:65
本文介绍了使用iText的自定义签名外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText(锋利的5.5.13版)创建自定义数字签名,其中用户可以从四个位置(顶部,底部,左侧和右侧)中设置图像位置,如下所示:

深度:

左:

顶部:

底部:

到目前为止,我已尝试在签名的第0层上工作,但我认为我做错了,因为签名详细信息在第2层中进行了设置.

尽管如此,这只是用于设置图像位置的初始草图.在下面的代码中,我加载图像并将其放在一个块中(从此),我该如何实现呢?

解决方案

我最初的演唱会不是使用iText(v7),因为我们没有太多时间来迁移使用iText(v5)的所有项目,但是我继续尝试使用v7.但老实说,使用v5似乎并不容易实现.

另一方面,在iText(v7)中,我可以使用以下简单方法快速完成此操作:

  private static void SetCustomSignature(PdfDocument doc,PdfSignatureAppearance sap,SignatureFormat signatureFormat,X509Certificate2 signerCertificate){字符串signatureFont = signatureFormat.Font;float signatureFontSize = float.Parse(signatureFormat.FontSize);Rectangle rect =新的Rectangle(250,100,200,80);sap.SetPageRect(rect).SetPageNumber(1);PdfFormXObject layer2 = sap.GetLayer2();PdfCanvas canvas =新的PdfCanvas(layer2,doc);float MARGIN = 2;PdfFont字体= PdfFontFactory.CreateFont();字符串signingText = GetSignatureInfo(signerCertificate,signatureFormat);//左侧签名,右侧图片//Rectangle dataRect = new Rectangle(rect.GetWidth()/2 + MARGIN/2,MARGIN,rect.GetWidth()/2-MARGIN,rect.GetHeight()-2 * MARGIN);//矩形signatureRect =新的Rectangle(MARGIN,MARGIN,rect.GetWidth()/2-2 * MARGIN,rect.GetHeight()-2 * MARGIN);//签名在右边,图片在左边//Rectangle dataRect = new Rectangle(MARGIN,MARGIN,rect.GetWidth()/2-MARGIN,rect.GetHeight()-2 * MARGIN);//矩形signatureRect = new Rectangle(rect.GetWidth()/2 + MARGIN/2,MARGIN,rect.GetWidth()/2-2 * MARGIN,rect.GetHeight()-2 * MARGIN);//顶部签名,底部图片//Rectangle dataRect = new Rectangle(MARGIN,MARGIN,rect.GetWidth()-2 * MARGIN,rect.GetHeight()/2-MARGIN);//矩形signatureRect =新Rectangle(MARGIN,rect.GetHeight()/2 + MARGIN,rect.GetWidth()-2 * MARGIN,rect.GetHeight()/2-MARGIN);//底部签名,顶部图像Rectangle dataRect =新Rectangle(MARGIN,rect.GetHeight()/2 + MARGIN,rect.GetWidth()-2 * MARGIN,rect.GetHeight()/2-MARGIN);矩形signatureRect =新矩形(MARGIN,MARGIN,rect.GetWidth()-2 * MARGIN,rect.GetHeight()/2-MARGIN);尝试 {画布signLayoutCanvas = new Canvas(canvas,doc,signatureRect);段落段落=新段落(signingText).SetFont(font).SetMargin(0).SetMultipliedLeading(0.9f).SetFontSize(10);Div div =新的Div();div.SetHeight(signatureRect.GetHeight());div.SetWidth(signatureRect.GetWidth());div.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE);div.SetHorizo​​ntalAlignment(iText.Layout.Properties.Horizo​​ntalAlignment.CENTER);div.Add(paragraph);signLayoutCanvas.Add(div);Canvas dataLayoutCanvas = new Canvas(canvas,doc,dataRect);图片image = new Image(ImageDataFactory.Create(signatureFormat.SignatureImage));image.SetAutoScale(true);Div dataDiv =新的Div();dataDiv.SetHeight(dataRect.GetHeight());dataDiv.SetWidth(dataRect.GetWidth());dataDiv.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE);dataDiv.SetHorizo​​ntalAlignment(iText.Layout.Properties.Horizo​​ntalAlignment.CENTER);dataDiv.Add(image);dataLayoutCanvas.Add(dataDiv);}抓住 {扔;}} 

这将导致以下签名:

当然,它仍然需要一些改进,但可以作为其他示例:-)

I am trying to create custom digital signatures using iText (sharp, version 5.5.13) where the user is able to set the image location from a set of four positions (top, bottom, left and right), as shown below:

Rigth:

Left:

Top:

Bottom:

So far I tried working on layer 0 of the signature but I think I doing it wrong because signature details are set in layer 2.

Nevertheless, this is just an initial sketch to set position of the images. In the following code I load the image and put it in a chunk (idea taken from this example)

PdfTemplate pdfTemplate = sap.GetLayer(0);
ColumnText c1 = new ColumnText(pdfTemplate);
Image img = Image.GetInstance(signatureImage);
Phrase elements = new Phrase();
elements.Add(new Chunk(img, 0, 0, true));
//c1.SetSimpleColumn(elements, 0, 0, rectangle.Width, rectangle.Height / 4, 0, Element.ALIGN_CENTER); // align bottom
//c1.SetSimpleColumn(elements, 0, rectangle.Height / 2, rectangle.Width, rectangle.Height, 0, Element.ALIGN_CENTER); // align top
c1.SetSimpleColumn(elements, rectangle.Width/2, 0, rectangle.Width, rectangle.Height, 0, Element.ALIGN_CENTER); // align right
//c1.SetSimpleColumn(elements, 0, 0, rectangle.Width/2, rectangle.Height, 0, Element.ALIGN_CENTER); // align left
c1.Go();

The result is more or less the expected, but there are two problems: the signature information takes over the whole rectangle (this is normal since I do not modify layer 2, and the image in layer 0 is not scaled as it should)

If I scale the image to fit the column, it goes to the top of rectangle:

Is there any way to do this "out of the box" or I need to overload the method that builds the signature appearance (like this) and how can I achieve this?

解决方案

My initial concert was not to use iText (v7) because we do not have much time to migrate all the projects we have with iText (v5), but I went ahead and tried out with v7. But to be honest is does not seem to be very easy to achieve with v5.

On the other hand, in iText (v7) I was able to do this very quickly with this simple method:

private static void SetCustomSignature(PdfDocument doc, PdfSignatureAppearance sap, SignatureFormat signatureFormat, X509Certificate2 signerCertificate) {
    string signatureFont = signatureFormat.Font;
    float signatureFontSize = float.Parse(signatureFormat.FontSize);
    Rectangle rect = new Rectangle(250, 100, 200, 80);
    sap.SetPageRect(rect).SetPageNumber(1);
    PdfFormXObject layer2 = sap.GetLayer2();
    PdfCanvas canvas = new PdfCanvas(layer2, doc);

    float MARGIN = 2;
    PdfFont font = PdfFontFactory.CreateFont();

    string signingText = GetSignatureInfo(signerCertificate, signatureFormat);

    // Signature at left and image at right
    //Rectangle dataRect = new Rectangle(rect.GetWidth() / 2 + MARGIN / 2, MARGIN, rect.GetWidth() / 2 - MARGIN, rect.GetHeight() - 2 * MARGIN);
    //Rectangle signatureRect = new Rectangle(MARGIN, MARGIN, rect.GetWidth() / 2 - 2 * MARGIN, rect.GetHeight() - 2 * MARGIN);

    // Signature at right and image at left
    //Rectangle dataRect = new Rectangle(MARGIN, MARGIN, rect.GetWidth() / 2 - MARGIN, rect.GetHeight() - 2 * MARGIN);
    //Rectangle signatureRect = new Rectangle(rect.GetWidth() / 2 + MARGIN / 2, MARGIN, rect.GetWidth() / 2 - 2 * MARGIN, rect.GetHeight() - 2 * MARGIN);

    // Signature at top and image at bottom
    //Rectangle dataRect      = new Rectangle(MARGIN, MARGIN, rect.GetWidth() - 2 * MARGIN, rect.GetHeight() / 2 - MARGIN);
    //Rectangle signatureRect = new Rectangle(MARGIN, rect.GetHeight() / 2 + MARGIN, rect.GetWidth() - 2 * MARGIN, rect.GetHeight() / 2 - MARGIN);

    // Signature at bottom and image at top
    Rectangle dataRect = new Rectangle(MARGIN, rect.GetHeight() / 2 + MARGIN, rect.GetWidth() - 2 * MARGIN, rect.GetHeight() / 2 - MARGIN);
    Rectangle signatureRect = new Rectangle(MARGIN, MARGIN, rect.GetWidth() - 2 * MARGIN, rect.GetHeight() / 2 - MARGIN);

    try {
        Canvas signLayoutCanvas = new Canvas(canvas, doc, signatureRect);
        Paragraph paragraph = new Paragraph(signingText).SetFont(font).SetMargin(0).SetMultipliedLeading(0.9f).SetFontSize(10);
        Div div = new Div();
        div.SetHeight(signatureRect.GetHeight());
        div.SetWidth(signatureRect.GetWidth());
        div.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE);
        div.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER);
        div.Add(paragraph);
        signLayoutCanvas.Add(div);


        Canvas dataLayoutCanvas = new Canvas(canvas, doc, dataRect);
        Image image = new Image(ImageDataFactory.Create(signatureFormat.SignatureImage));
        image.SetAutoScale(true);
        Div dataDiv = new Div();
        dataDiv.SetHeight(dataRect.GetHeight());
        dataDiv.SetWidth(dataRect.GetWidth());
        dataDiv.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE);
        dataDiv.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER);
        dataDiv.Add(image);
        dataLayoutCanvas.Add(dataDiv);
    }
    catch {
        throw;
    }
}

This will result in the following signatures:

Of course, it still needs some improvements but it may serve as an example for others :-)

这篇关于使用iText的自定义签名外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆