Adding a digital signature placeholder in HTML templates
If you want to add a visual signature when converting HTML with pdfHTML, it can be difficult to place the signature in the exact position you require for the resulting document.
This example uses a custom HTML tag <signature-field>
to define the place for the signature in the HTML template.
In the first step, a customTagWorker is defined for the HTML element <signature-field>
and then the HTML is converted into a PDF with a signature form field.
In the second step, this PDF is then signed and the signature form field is filled with a visible appearance.
Code Example
This example is adapted from the signature tag examples on our GitHub repository: Java or C#.
The following very simple HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Signature sample</title>
</head>
<body>
<h1>Agreement</h1>
<p>This document requires your signature:</p>
<signature-field id="signature_id" width="150" height="100"></signature-field>
</body>
</html>
would then lead to the following output when using the CustomSignatureTagWorker
ConverterProperties converterProperties = new ConverterProperties();
DefaultTagWorkerFactory tagWorkerFactory = new SignatureTagWorkerFactory();
converterProperties.setTagWorkerFactory(tagWorkerFactory);
PdfWriter pdfWriter = new PdfWriter(dest);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
HtmlConverter.convertToPdf(new FileInputStream(src), pdfDocument, converterProperties);

This intermediate form field will then be signed with the PdfPadesSigner()
(Java/.NET) and create a custom signature appearance.
// for compatibility with iText 8.0.2-9.0.0 please uncomment
// Security.addProvider(new BouncyCastleProvider());
PdfPadesSigner padesSigner = new PdfPadesSigner(new PdfReader(FileUtil.getInputStreamForFile(dest)),
FileUtil.getFileOutputStream(DEST));
// We can pass the appearance through the signer properties.
SignerProperties signerProperties = createSignerProperties("signature_id");
padesSigner.signWithBaselineBProfile(signerProperties, getCertificateChain(), getPrivateKey());

The position within the HTML structure was defined by the signature-field
element. The specified property “id
” then specifies the PDF signature field name.
The display of the signature field can be further influenced via the HTML template using the width
and height
properties.
This sample was built for iText Core 9.1.0. For use with iText versions 8.0.2-9.0.0 Security.addProvider(new BouncyCastleProvider());
must be added.