}
options.log("console.createOutPdf", outFile);
fout = new FileOutputStream(outFile);
final HashAlgorithm hashAlgorithm = options.getHashAlgorithmX();
options.log("console.createSignature");
char tmpPdfVersion = '\0'; // default version - the same as input
if (reader.getPdfVersion() < hashAlgorithm.getPdfVersion()) {
tmpPdfVersion = hashAlgorithm.getPdfVersion();
options.log("console.updateVersion", new String[] { String.valueOf(reader.getPdfVersion()),
String.valueOf(tmpPdfVersion) });
}
final PdfStamper stp = PdfStamper.createSignature(reader, fout, tmpPdfVersion, null, options.isAppendX());
if (!options.isAppendX()) {
// we are not in append mode, let's remove existing signatures
// (otherwise we're getting to troubles)
final AcroFields acroFields = stp.getAcroFields();
final List<String> sigNames = acroFields.getSignatureNames();
for (String sigName : sigNames) {
acroFields.removeField(sigName);
}
}
if (options.isEncryptedX()) {
options.log("console.setEncryption");
final int tmpRight = options.getRightPrinting().getRight()
| (options.isRightCopy() ? PdfWriter.ALLOW_COPY : 0)
| (options.isRightAssembly() ? PdfWriter.ALLOW_ASSEMBLY : 0)
| (options.isRightFillIn() ? PdfWriter.ALLOW_FILL_IN : 0)
| (options.isRightScreanReaders() ? PdfWriter.ALLOW_SCREENREADERS : 0)
| (options.isRightModifyAnnotations() ? PdfWriter.ALLOW_MODIFY_ANNOTATIONS : 0)
| (options.isRightModifyContents() ? PdfWriter.ALLOW_MODIFY_CONTENTS : 0);
stp.setEncryption(true, options.getPdfUserPwdStr(), options.getPdfOwnerPwdStr(), tmpRight);
}
final PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
if (!StringUtils.isEmpty(options.getReason())) {
options.log("console.setReason", options.getReason());
sap.setReason(options.getReason());
}
if (!StringUtils.isEmpty(options.getLocation())) {
options.log("console.setLocation", options.getLocation());
sap.setLocation(options.getLocation());
}
if (!StringUtils.isEmpty(options.getContact())) {
options.log("console.setContact", options.getContact());
sap.setContact(options.getContact());
}
options.log("console.setCertificationLevel");
sap.setCertificationLevel(options.getCertLevelX().getLevel());
if (options.isVisible()) {
// visible signature is enabled
options.log("console.configureVisible");
options.log("console.setAcro6Layers");
sap.setAcro6Layers(true);
final String tmpImgPath = options.getImgPath();
if (tmpImgPath != null) {
options.log("console.createImage", tmpImgPath);
final Image img = Image.getInstance(tmpImgPath);
options.log("console.setSignatureGraphic");
sap.setSignatureGraphic(img);
}
final String tmpBgImgPath = options.getBgImgPath();
if (tmpBgImgPath != null) {
options.log("console.createImage", tmpBgImgPath);
final Image img = Image.getInstance(tmpBgImgPath);
options.log("console.setImage");
sap.setImage(img);
}
options.log("console.setImageScale");
sap.setImageScale(options.getBgImgScale());
options.log("console.setL2Text");
if (options.getL2Text() != null) {
sap.setLayer2Text(options.getL2Text());
} else {
final StringBuilder buf = new StringBuilder();
buf.append(res.get("default.l2text.signedBy")).append(" ");
buf.append(PdfPKCS7.getSubjectFields((X509Certificate) chain[0]).getField("CN")).append('\n');
final SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
buf.append(res.get("default.l2text.date")).append(" ").append(
sd.format(sap.getSignDate().getTime()));
if (StringUtils.hasLength(options.getReason()))
buf.append('\n').append(res.get("default.l2text.reason")).append(" ").append(
options.getReason());
if (StringUtils.hasLength(options.getLocation()))
buf.append('\n').append(res.get("default.l2text.location")).append(" ").append(
options.getLocation());
sap.setLayer2Text(buf.toString());
;
}
if (FontUtils.getL2BaseFont() != null) {
sap.setLayer2Font(new Font(FontUtils.getL2BaseFont(), options.getL2TextFontSize()));
}
options.log("console.setL4Text");
sap.setLayer4Text(options.getL4Text());
options.log("console.setRender");
sap.setRender(options.getRenderMode().getRender());
options.log("console.setVisibleSignature");
sap.setVisibleSignature(new Rectangle(options.getPositionLLX(), options.getPositionLLY(), options
.getPositionURX(), options.getPositionURY()), options.getPage(), null);
}
options.log("console.processing");
final PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached"));
dic.setReason(sap.getReason());
dic.setLocation(sap.getLocation());
dic.setContact(sap.getContact());
dic.setDate(new PdfDate(sap.getSignDate()));
sap.setCryptoDictionary(dic);
final Proxy tmpProxy = options.createProxy();
final CRLInfo crlInfo = new CRLInfo(options, chain);
// CRLs are stored twice in PDF c.f.
// PdfPKCS7.getAuthenticatedAttributeBytes
final int contentEstimated = (int) (Constants.DEFVAL_SIG_SIZE + 2L * crlInfo.getByteCount());
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
sap.preClose(exc);
PdfPKCS7 sgn = new PdfPKCS7(key, chain, crlInfo.getCrls(), hashAlgorithm.getAlgorithmName(), null, false);
InputStream data = sap.getRangeStream();
final MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName());
byte buf[] = new byte[8192];
int n;
while ((n = data.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}