名称和描述的字体大小PDF数字签名

时间:2018-02-27 11:55:43

标签: java itext7

如果我使用字体大小appearance.setLayer2FontSize(6.0f);,它会为名称和描述设置字体大小。

enter image description here

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

有没有办法为Name和description设置不同的字体大小  (名称应该比描述大一点)

1 个答案:

答案 0 :(得分:0)

整个Layer2Text是一个String,无论您是设置它还是iText构建它,它都使用单个字体和字体大小排版为单个段落。因此,不,您不能要求iText使用多个样式绘制您的Layer2Text或其默认文本。

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

所以,而不是

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的来源获取灵感,特别是如果您不希望自己的设计偏离默认设置。

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

例如

示例自定义的layer2内容可能形如下:

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代码的复制和粘贴以及重构,为其不同的文本部分创建了不同字体大小的默认外观。