提问者:小点点

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


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

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

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


共1个答案

匿名用户

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

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

所以,与其

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...]

当然,您可以使用PdfSignature外观方法get外观的源代码来获取灵感,特别是如果您不希望您的设计与默认值偏离太多。

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

一个示例自定义的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);
}

这本质上是一个复制品