* @throws DocumentException on error
*/
public PdfTemplate getAppearance() throws DocumentException {
if (isInvisible()) {
PdfTemplate t = new PdfTemplate(writer);
t.setBoundingBox(new LwgRectangle(0, 0));
writer.addDirectTemplateSimple(t, null);
return t;
}
if (app[0] == null) {
PdfTemplate t = app[0] = new PdfTemplate(writer);
t.setBoundingBox(new LwgRectangle(100, 100));
writer.addDirectTemplateSimple(t, new PdfName("n0"));
t.setLiteral("% DSBlank\n");
}
if (app[1] == null && !acro6Layers) {
PdfTemplate t = app[1] = new PdfTemplate(writer);
t.setBoundingBox(new LwgRectangle(100, 100));
writer.addDirectTemplateSimple(t, new PdfName("n1"));
t.setLiteral(questionMark);
}
if (app[2] == null) {
String text;
if (layer2Text == null) {
StringBuffer buf = new StringBuffer();
buf.append("Digitally signed by ").append(PdfPKCS7.getSubjectFields((X509Certificate)certChain[0]).getField("CN")).append('\n');
SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
buf.append("Date: ").append(sd.format(signDate.getTime()));
if (reason != null)
buf.append('\n').append("Reason: ").append(reason);
if (location != null)
buf.append('\n').append("Location: ").append(location);
text = buf.toString();
}
else
text = layer2Text;
PdfTemplate t = app[2] = new PdfTemplate(writer);
t.setBoundingBox(rect);
writer.addDirectTemplateSimple(t, new PdfName("n2"));
if (image != null) {
if (imageScale == 0) {
t.addImage(image, rect.getWidth(), 0, 0, rect.getHeight(), 0, 0);
}
else {
float usableScale = imageScale;
if (imageScale < 0)
usableScale = Math.min(rect.getWidth() / image.getWidth(), rect.getHeight() / image.getHeight());
float w = image.getWidth() * usableScale;
float h = image.getHeight() * usableScale;
float x = (rect.getWidth() - w) / 2;
float y = (rect.getHeight() - h) / 2;
t.addImage(image, w, 0, 0, h, x, y);
}
}
LwgFont font;
if (layer2Font == null)
font = new LwgFont();
else
font = new LwgFont(layer2Font);
float size = font.getSize();
LwgRectangle dataRect = null;
LwgRectangle signatureRect = null;
if (render == SignatureRenderNameAndDescription ||
(render == SignatureRenderGraphicAndDescription && this.signatureGraphic != null)) {
// origin is the bottom-left
signatureRect = new LwgRectangle(
MARGIN,
MARGIN,
rect.getWidth() / 2 - MARGIN,
rect.getHeight() - MARGIN);
dataRect = new LwgRectangle(
rect.getWidth() / 2 + MARGIN / 2,
MARGIN,
rect.getWidth() - MARGIN / 2,
rect.getHeight() - MARGIN);
if (rect.getHeight() > rect.getWidth()) {
signatureRect = new LwgRectangle(
MARGIN,
rect.getHeight() / 2,
rect.getWidth() - MARGIN,
rect.getHeight());
dataRect = new LwgRectangle(
MARGIN,
MARGIN,
rect.getWidth() - MARGIN,
rect.getHeight() / 2 - MARGIN);
}
}
else {
dataRect = new LwgRectangle(
MARGIN,
MARGIN,
rect.getWidth() - MARGIN,
rect.getHeight() * (1 - TOP_SECTION) - MARGIN);
}
if (render == SignatureRenderNameAndDescription) {
String signedBy = PdfPKCS7.getSubjectFields((X509Certificate)certChain[0]).getField("CN");
LwgRectangle sr2 = new LwgRectangle(signatureRect.getWidth() - MARGIN, signatureRect.getHeight() - MARGIN );
float signedSize = fitText(font, signedBy, sr2, -1, runDirection);
ColumnText ct2 = new ColumnText(t);
ct2.setRunDirection(runDirection);
ct2.setSimpleColumn(new LwgPhrase(signedBy, font), signatureRect.getLeft(), signatureRect.getBottom(), signatureRect.getRight(), signatureRect.getTop(), signedSize, LwgElement.ALIGN_LEFT);
ct2.go();
}
else if (render == SignatureRenderGraphicAndDescription) {
ColumnText ct2 = new ColumnText(t);
ct2.setRunDirection(runDirection);
ct2.setSimpleColumn(signatureRect.getLeft(), signatureRect.getBottom(), signatureRect.getRight(), signatureRect.getTop(), 0, LwgElement.ALIGN_RIGHT);
LwgImage im = LwgImage.getInstance(signatureGraphic);
im.scaleToFit(signatureRect.getWidth(), signatureRect.getHeight());
Paragraph p = new Paragraph();
// must calculate the point to draw from to make image appear in middle of column
float x = 0;
// experimentation found this magic number to counteract Adobe's signature graphic, which
// offsets the y co-ordinate by 15 units
float y = -im.getScaledHeight() + 15;
x = x + (signatureRect.getWidth() - im.getScaledWidth()) / 2;
y = y - (signatureRect.getHeight() - im.getScaledHeight()) / 2;
p.add(new Chunk(im, x + (signatureRect.getWidth() - im.getScaledWidth()) / 2, y, false));
ct2.addElement(p);
ct2.go();
}
if (size <= 0) {
LwgRectangle sr = new LwgRectangle(dataRect.getWidth(), dataRect.getHeight());
size = fitText(font, text, sr, 12, runDirection);
}
ColumnText ct = new ColumnText(t);
ct.setRunDirection(runDirection);
ct.setSimpleColumn(new LwgPhrase(text, font), dataRect.getLeft(), dataRect.getBottom(), dataRect.getRight(), dataRect.getTop(), size, LwgElement.ALIGN_LEFT);
ct.go();
}
if (app[3] == null && !acro6Layers) {
PdfTemplate t = app[3] = new PdfTemplate(writer);
t.setBoundingBox(new LwgRectangle(100, 100));
writer.addDirectTemplateSimple(t, new PdfName("n3"));
t.setLiteral("% DSBlank\n");
}
if (app[4] == null && !acro6Layers) {
PdfTemplate t = app[4] = new PdfTemplate(writer);
t.setBoundingBox(new LwgRectangle(0, rect.getHeight() * (1 - TOP_SECTION), rect.getRight(), rect.getTop()));
writer.addDirectTemplateSimple(t, new PdfName("n4"));
LwgFont font;
if (layer2Font == null)
font = new LwgFont();
else
font = new LwgFont(layer2Font);
float size = font.getSize();
String text = "Signature Not Verified";
if (layer4Text != null)
text = layer4Text;
LwgRectangle sr = new LwgRectangle(rect.getWidth() - 2 * MARGIN, rect.getHeight() * TOP_SECTION - 2 * MARGIN);
size = fitText(font, text, sr, 15, runDirection);
ColumnText ct = new ColumnText(t);
ct.setRunDirection(runDirection);
ct.setSimpleColumn(new LwgPhrase(text, font), MARGIN, 0, rect.getWidth() - MARGIN, rect.getHeight() - MARGIN, size, LwgElement.ALIGN_LEFT);
ct.go();
}
int rotation = writer.reader.getPageRotation(page);
LwgRectangle rotated = new LwgRectangle(rect);
int n = rotation;
while (n > 0) {
rotated = rotated.rotate();
n -= 90;
}
if (frm == null) {
frm = new PdfTemplate(writer);
frm.setBoundingBox(rotated);