public PdfTemplate GetAppearance()
{
if (IsInvisible()) {
PdfTemplate t = new PdfTemplate(writer);
t.BoundingBox = new Rectangle(0, 0);
writer.AddDirectTemplateSimple(t, null);
return t;
}
if (app[0] == null) {
PdfTemplate t = app[0] = new PdfTemplate(writer);
t.BoundingBox = new Rectangle(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.BoundingBox = new Rectangle(100, 100);
writer.AddDirectTemplateSimple(t, new PdfName("n1"));
t.SetLiteral(questionMark);
}
if (app[2] == null) {
String text;
if (layer2Text == null) {
StringBuilder buf = new StringBuilder();
buf.Append("Digitally signed by ");
String name = CertificateInfo.GetSubjectFields(signCertificate).GetField("CN");
if (name == null)
name = CertificateInfo.GetSubjectFields(signCertificate).GetField("E");
if (name == null)
name = "";
buf.Append(name).Append('\n');
buf.Append("Date: ").Append(signDate.ToString("yyyy.MM.dd HH:mm:ss zzz"));
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.BoundingBox = rect;
writer.AddDirectTemplateSimple(t, new PdfName("n2"));
if (image != null) {
if (imageScale == 0) {
t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0);
}
else {
float usableScale = imageScale;
if (imageScale < 0)
usableScale = Math.Min(rect.Width / image.Width, rect.Height / image.Height);
float w = image.Width * usableScale;
float h = image.Height * usableScale;
float x = (rect.Width - w) / 2;
float y = (rect.Height - h) / 2;
t.AddImage(image, w, 0, 0, h, x, y);
}
}
Font font;
if (layer2Font == null)
font = new Font();
else
font = new Font(layer2Font);
float size = font.Size;
Rectangle dataRect = null;
Rectangle signatureRect = null;
if (renderingMode == RenderingMode.NAME_AND_DESCRIPTION ||
(renderingMode == RenderingMode.GRAPHIC_AND_DESCRIPTION && this.SignatureGraphic != null)) {
// origin is the bottom-left
signatureRect = new Rectangle(
MARGIN,
MARGIN,
rect.Width / 2 - MARGIN,
rect.Height - MARGIN);
dataRect = new Rectangle(
rect.Width / 2 + MARGIN / 2,
MARGIN,
rect.Width - MARGIN / 2,
rect.Height - MARGIN);
if (rect.Height > rect.Width) {
signatureRect = new Rectangle(
MARGIN,
rect.Height / 2,
rect.Width - MARGIN,
rect.Height);
dataRect = new Rectangle(
MARGIN,
MARGIN,
rect.Width - MARGIN,
rect.Height / 2 - MARGIN);
}
}
else if (renderingMode == RenderingMode.GRAPHIC) {
if (signatureGraphic == null) {
throw new InvalidOperationException(MessageLocalization.GetComposedMessage("a.signature.image.should.be.present.when.rendering.mode.is.graphic.only"));
}
signatureRect = new Rectangle(
MARGIN,
MARGIN,
rect.Width - MARGIN, // take all space available
rect.Height - MARGIN);
}
else {
dataRect = new Rectangle(
MARGIN,
MARGIN,
rect.Width - MARGIN,
rect.Height * (1 - TOP_SECTION) - MARGIN);
}
if (renderingMode == RenderingMode.NAME_AND_DESCRIPTION) {
string signedBy = CertificateInfo.GetSubjectFields(signCertificate).GetField("CN");
if (signedBy == null)
signedBy = CertificateInfo.GetSubjectFields(signCertificate).GetField("E");
if (signedBy == null)
signedBy = "";
Rectangle sr2 = new Rectangle(signatureRect.Width - MARGIN, signatureRect.Height - MARGIN );
float signedSize = ColumnText.FitText(font, signedBy, sr2, -1, runDirection);
ColumnText ct2 = new ColumnText(t);
ct2.RunDirection = runDirection;
ct2.SetSimpleColumn(new Phrase(signedBy, font), signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, signedSize, Element.ALIGN_LEFT);
ct2.Go();
}
else if (renderingMode == RenderingMode.GRAPHIC_AND_DESCRIPTION) {
if (signatureGraphic == null) {
throw new InvalidOperationException(MessageLocalization.GetComposedMessage("a.signature.image.should.be.present.when.rendering.mode.is.graphic.and.description"));
}
ColumnText ct2 = new ColumnText(t);
ct2.RunDirection = runDirection;
ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT);
Image im = Image.GetInstance(SignatureGraphic);
im.ScaleToFit(signatureRect.Width, signatureRect.Height);
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.ScaledHeight + 15;
x = x + (signatureRect.Width - im.ScaledWidth) / 2;
y = y - (signatureRect.Height - im.ScaledHeight) / 2;
p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false));
ct2.AddElement(p);
ct2.Go();
}
else if (renderingMode == RenderingMode.GRAPHIC) {
ColumnText ct2 = new ColumnText(t);
ct2.RunDirection = runDirection;
ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT);
Image im = Image.GetInstance(signatureGraphic);
im.ScaleToFit(signatureRect.Width, signatureRect.Height);
Paragraph p = new Paragraph(signatureRect.Height);
// must calculate the point to draw from to make image appear in middle of column
float x = (signatureRect.Width - im.ScaledWidth) / 2;
float y = (signatureRect.Height - im.ScaledHeight) / 2;
p.Add(new Chunk(im, x, y, false));
ct2.AddElement(p);
ct2.Go();
}
if (renderingMode != RenderingMode.GRAPHIC) {
if (size <= 0) {
Rectangle sr = new Rectangle(dataRect.Width, dataRect.Height);
size = ColumnText.FitText(font, text, sr, 12, runDirection);
}
ColumnText ct = new ColumnText(t);
ct.RunDirection = runDirection;
ct.SetSimpleColumn(new Phrase(text, font), dataRect.Left, dataRect.Bottom, dataRect.Right, dataRect.Top, size, Element.ALIGN_LEFT);
ct.Go();
}
}
if (app[3] == null && !acro6Layers) {
PdfTemplate t = app[3] = new PdfTemplate(writer);
t.BoundingBox = new Rectangle(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.BoundingBox = new Rectangle(0, rect.Height * (1 - TOP_SECTION), rect.Right, rect.Top);
writer.AddDirectTemplateSimple(t, new PdfName("n4"));
Font font;
if (layer2Font == null)
font = new Font();
else
font = new Font(layer2Font);
float size = font.Size;
String text = "Signature Not Verified";
if (layer4Text != null)
text = layer4Text;
Rectangle sr = new Rectangle(rect.Width - 2 * MARGIN, rect.Height * TOP_SECTION - 2 * MARGIN);
size = ColumnText.FitText(font, text, sr, 15, runDirection);
ColumnText ct = new ColumnText(t);
ct.RunDirection = runDirection;
ct.SetSimpleColumn(new Phrase(text, font), MARGIN, 0, rect.Width - MARGIN, rect.Height - MARGIN, size, Element.ALIGN_LEFT);
ct.Go();
}
int rotation = writer.reader.GetPageRotation(page);
Rectangle rotated = new Rectangle(rect);
int n = rotation;
while (n > 0) {
rotated = rotated.Rotate();
n -= 90;
}
if (frm == null) {
frm = new PdfTemplate(writer);
frm.BoundingBox = rotated;
writer.AddDirectTemplateSimple(frm, new PdfName("FRM"));
float scale = Math.Min(rect.Width, rect.Height) * 0.9f;
float x = (rect.Width - scale) / 2;
float y = (rect.Height - scale) / 2;
scale /= 100;
if (rotation == 90)
frm.ConcatCTM(0, 1, -1, 0, rect.Height, 0);
else if (rotation == 180)
frm.ConcatCTM(-1, 0, 0, -1, rect.Width, rect.Height);
else if (rotation == 270)
frm.ConcatCTM(0, -1, 1, 0, 0, rect.Width);
frm.AddTemplate(app[0], 0, 0);
if (!acro6Layers)
frm.AddTemplate(app[1], scale, 0, 0, scale, x, y);
frm.AddTemplate(app[2], 0, 0);
if (!acro6Layers) {
frm.AddTemplate(app[3], scale, 0, 0, scale, x, y);
frm.AddTemplate(app[4], 0, 0);
}
}
PdfTemplate napp = new PdfTemplate(writer);
napp.BoundingBox = rotated;
writer.AddDirectTemplateSimple(napp, null);
napp.AddTemplate(frm, 0, 0);
return napp;
}