名称和说明的字体大小PDF数字签名 [英] Font size for Name and Description PDF Digital signature

查看:97
本文介绍了名称和说明的字体大小PDF数字签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用字体大小appearance.setLayer2FontSize(6.0f);,它将同时设置名称和描述的字体大小.

If I use font size appearance.setLayer2FontSize(6.0f); it sets font size for both Name and Description.

        PdfReader reader = null;
    PdfSigner signer = null;
    try {
        reader = new PdfReader(inStream);
        signer = new PdfSigner(reader, pdfos, false);
    } catch (IOException e) {
        LOGGER.error("Error while loading PDF");
        throw new DigitalSignException("Error while loading PDF", e);
    }

    int noOfPages = signer.getDocument().getNumberOfPages();
    PdfSignatureAppearance appearance = signer.getSignatureAppearance().setReason(reason).setLocation(loc)
            .setReuseAppearance(false);
    Rectangle rect = new Rectangle(250, 100, 200, 80);
    appearance.setRenderingMode(RenderingMode.NAME_AND_DESCRIPTION);
    appearance.setLayer2FontSize(6.0f);
    appearance.setPageRect(rect).setPageNumber(noOfPages);
    signer.setFieldName("sign");

    // Creating the signature
    IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, bouncyCastleProvider.getName());
    IExternalDigest digest = new BouncyCastleDigest();
    try {
        signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
    } catch (IOException | GeneralSecurityException e) {
        LOGGER.error("Error while adding digital signature to PDF");
        throw new DigitalSignException("Error while adding digital signature to PDF", e);
    }

是否可以为名称"和描述"设置不同的字体大小 (名称应比描述大一些)

Is there a way to set different font sizes for Name and description (Name should be little bigger than description)

推荐答案

整个Layer2Text是单个String,无论您设置它还是由iText构建它,并且都使用单个字体将其排版为单个段落.和字体大小.因此,不能,您不能要求iText使用Layer2Text或其默认文本为不同部分绘制多种样式.

The whole Layer2Text is a single String, whether you set it or iText builds it, and it is typeset as a single paragraph using a single font and font size. Thus, NO, you cannot ask iText to draw your Layer2Text or its default text using multiple styles for different parts of it.

但是,您可以做的是在iText在其上创建外观之前检索PdfFormXObject Layer2,并且您可以在其上绘制任何样式的东西.

What you can do, though, is to retrieve the PdfFormXObject Layer2 before iText has created its appearance on it, and you can draw anything in any style on it.

所以,而不是

appearance.setRenderingMode(RenderingMode.NAME_AND_DESCRIPTION);
appearance.setLayer2FontSize(6.0f);
appearance.setPageRect(rect).setPageNumber(noOfPages);

你会做的

appearance.setPageRect(rect).setPageNumber(noOfPages);
PdfFormXObject layer2 = getLayer2();
[...shape the layer2 contents as you desire...]

当然,您可以使用PdfSignatureAppearance方法getAppearance的源进行启发,特别是如果您不希望设计与默认设置有很大出入的话.

Of course you can use the source of the PdfSignatureAppearance method getAppearance for inspiration, in particular if you don't want your design to deviate much from the default.

因此,是的,您可以完全自定义签名外观.

Thus, YES, you can completely customize the signature appearance.

定制的layer2内容示例可能如下所示:

An example customized layer2 content might be shaped like this:

PdfFormXObject layer2 = appearance.getLayer2();
PdfCanvas canvas = new PdfCanvas(layer2, signer.getDocument());

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

String name = null;
CertificateInfo.X500Name x500name = CertificateInfo.getSubjectFields((X509Certificate)chain[0]);
if (x500name != null) {
    name = x500name.getField("CN");
    if (name == null)
        name = x500name.getField("E");
}
if (name == null)
    name = "";

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

try (Canvas layoutCanvas = new Canvas(canvas, signer.getDocument(), signatureRect);) {
    Paragraph paragraph = new Paragraph(name).setFont(font).setMargin(0).setMultipliedLeading(0.9f).setFontSize(20);
    layoutCanvas.add(paragraph);
}

try (Canvas layoutCanvas = new Canvas(canvas, signer.getDocument(), dataRect);) {
    Paragraph paragraph = new Paragraph().setFont(font).setMargin(0).setMultipliedLeading(0.9f);
    paragraph.add(new Text("Digitally signed by ").setFontSize(6));
    paragraph.add(new Text(name + '\n').setFontSize(9));
    paragraph.add(new Text("Date: " + new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z").format(signer.getSignDate().getTime()) + '\n').setFontSize(6));
    paragraph.add(new Text("Reason: " + appearance.getReason() + '\n').setFontSize(6));
    paragraph.add(new Text("Location: " + appearance.getLocation()).setFontSize(6));
    layoutCanvas.add(paragraph);
}

从本质上讲,这是对iText代码的复制与粘贴和重构,以创建默认外观,并为其不同文本部分使用不同的字体大小.

This essentially is a copy&paste&refactoring of the iText code creating its default appearance with different font sizes for different text parts of it.

这篇关于名称和说明的字体大小PDF数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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