Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

java - Font size for Name and Description PDF Digital signature

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

So, instead of

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

you'd do

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

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.

For example

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 + '
').setFontSize(9));
    paragraph.add(new Text("Date: " + new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z").format(signer.getSignDate().getTime()) + '
').setFontSize(6));
    paragraph.add(new Text("Reason: " + appearance.getReason() + '
').setFontSize(6));
    paragraph.add(new Text("Location: " + appearance.getLocation()).setFontSize(6));
    layoutCanvas.add(paragraph);
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...