Copy link to clipboard
Copied
I need create digital signature PDF file. I use JS (CryptoPro PDF).
CPSign = app.trustedFunction(function (certificateHEX, pin, pdfLockMode, signReason, signLocation, signContactInfo, timeStamp, bShowCertificatesDialog, bShowSignDialogs, appearanceSettings, cadesType) {
if (typeof (bShowCertificatesDialog) === 'undefined') bShowCertificatesDialog = false;
if (typeof (bShowSignDialogs) === 'undefined') bShowSignDialogs = false;
var appearance = null;
if (appearanceSettings === undefined || appearanceSettings == "") {
appearance = null;
} else if (isObject(appearanceSettings)) {
appearance = appearanceSettings;
}
else if (isString(appearanceSettings)) {
try {
if (app.viewerVersion >= 10 && app.viewerVersion < 12) {
appearance = eval("(" + appearanceSettings + ")");
}
else if (app.viewerVersion >= 12) {
appearance = JSON.parse(appearanceSettings);
}
else {
return "Создание видимой подписи недоступно в версии Acrobat 9 и ниже.";
}
}
catch (ex) {
return "Неправильно задан параметр отбражения подписи.";
}
}
else {
appearance = null;
}
var signResult = false;
var theDoc = this;
if (theDoc == null) {
return "Не удалось открыть документ PDF.";
}
var theCert = { binary: certificateHEX };
var signatureField = null;
var fieldIndex = 0;
var signatureFieldName;
app.beginPriv();
try {
for (fieldIndex = 0; fieldIndex < CPPDF.maxSignaturesNumber; fieldIndex++) {
if (fieldIndex == 0) {
signatureFieldName = 'CPPDFDSig signature';
}
else {
signatureFieldName = 'CPPDFDSig signature ' + fieldIndex;
}
signatureField = theDoc.getField(signatureFieldName);
if (signatureField == null) {
var pageNumber = 0;
var appearanceCoords = [0, 0, 0, 0];
if (appearance != null) {
if (typeof (appearance.pageNumber) !== 'undefined') {
if (appearance.pageNumber < 0) {
pageNumber = theDoc.numPages + parseInt(appearance.pageNumber, 10);
if (pageNumber < 0) {
app.endPriv();
return "Номер страницы для отображения подписи превышает количество страниц в документе.";
}
}
else {
if (appearance.pageNumber > theDoc.numPages - 1) {
app.endPriv();
return "Номер страницы для отображения подписи превышает количество страниц в документе.";
}
pageNumber = appearance.pageNumber;
}
}
if (typeof (appearance.coords) !== 'undefined') {
appearanceCoords = appearance.coords;
var aRect = this.getPageBox({ cBox: "Media", nPage: pageNumber });
var isAppearanceInsidePage =
appearanceCoords[0] >= aRect[0] && appearanceCoords[0] <= aRect[2] &&
appearanceCoords[2] >= aRect[0] && appearanceCoords[2] <= aRect[2] &&
appearanceCoords[1] >= aRect[3] && appearanceCoords[1] <= aRect[1] &&
appearanceCoords[3] >= aRect[3] && appearanceCoords[3] <= aRect[1];
if (!isAppearanceInsidePage)
{
app.endPriv();
return "Координаты поля подписи выходят за границы страницы.";
}
}
}
signatureField =
theDoc.addField(signatureFieldName, 'signature', pageNumber, appearanceCoords);
if (signatureField == null) {
app.endPriv();
return "Не удалось создать поле для подписи в документе.";
}
break;
}
}
}
catch (ex) {
return "Ошибка при создании поля для подписи. " + ex.message ;
}
if (fieldIndex >= CPPDF.maxSignaturesNumber) {
app.endPriv();
return "Превышено допустимое количество подписей в документе.";
}
var theEngine = security.getHandler(CPPDF.sigHandlerName);
if (theEngine == null) {
app.endPriv();
return "Не удалось проинициализировать обработчик " + CPPDF.sigHandlerName + ".";
}
if (!theEngine.login({ oParams: { oEndUserSignCert: theCert }, bUI: bShowCertificatesDialog })) {
app.endPriv();
return "Ошибка при доступе к сертификату. " + ErrorInfo.Message;
}
var signatureParameters =
{
reason: signReason,
location: signLocation,
contactInfo: signContactInfo,
};
if (timeStamp != null && timeStamp != "") {
signatureParameters.timeStamp = timeStamp;
}
if (cadesType != null) {
signatureParameters.cadesType = cadesType;
}
if (pin != null && pin.length != 0) {
signatureParameters.password = pin;
}
var customAppearance = null;
if (appearance != null) {
if (typeof (appearance.appearanceFilter) !== 'undefined') {
var presetAppearances = theEngine.appearances;
if (presetAppearances == null ||
presetAppearances.indexOf(appearance.appearanceFilter) == -1) {
app.endPriv();
return "Оформление подписи с именем " + appearance.appearanceFilter + " не зарегистрировано в Adobe Acrobat.";
}
signatureParameters.appearance = appearance.appearanceFilter;
customAppearance = { appearanceType: CPPDF.templateAppearanceType };
}
else if (typeof (appearance.customAppearance) !== 'undefined') {
if (app.viewerVersion < 10) {
app.endPriv();
return "Создание настраиваемого отображения подписи недоступно в версии Acrobat 9 и ниже.";
}
customAppearance = appearance.customAppearance;
signatureField.delay = true;
if (typeof (appearance.customAppearance.borderStyle) !== 'undefined') {
signatureField.borderStyle = appearance.customAppearance.borderStyle;
}
if (typeof (appearance.customAppearance.borderColor) !== 'undefined') {
signatureField.borderColor = appearance.customAppearance.borderColor;
}
if (typeof (appearance.customAppearance.borderWidth) !== 'undefined') {
signatureField.lineWidth = appearance.customAppearance.borderWidth;
}
if (typeof (appearance.customAppearance.fillColor) !== 'undefined') {
signatureField.fillColor = appearance.customAppearance.fillColor;
}
signatureField.delay = false;
}
}
signatureParameters.oCustomAppearance = customAppearance;
try {
switch (pdfLockMode) {
case 0:
break;
case 1:
signatureField.signatureSetSeedValue({ lockDocument: "true", flags: 128});
break;
case 2:
signatureField.signatureSetSeedValue({ mdp: "allowNone" });
break;
case 3:
signatureField.signatureSetSeedValue({ mdp: "default" });
break;
case 4:
signatureField.signatureSetSeedValue({ mdp: "defaultAndComments" });
break;
}
var res = signatureField.signatureSign({ oSig: theEngine, oInfo: signatureParameters, bUI: bShowSignDialogs });
if (res != true) {
throw false;
}
signResult = true;
}
catch (ex) {
app.endPriv();
var exceptionMessage = "Документ не подписан. ";
if (typeof (ex.message) !== 'undefined') {
exceptionMessage += ex.message + " ";
}
if (typeof (ErrorInfo.Message) !== 'undefined') {
exceptionMessage += ErrorInfo.Message;
}
return exceptionMessage;
}
app.endPriv();
return signResult;
});
the function parameter appearanceSettings is a JSON string:
{"pageNumber":0,
"coords":[10,10,350,100],
"customAppearance":
{"font":"Arial",
"borderStyle":"beveled",
"borderColor":["RGB",0,0,255],
"borderWidth":2,
"textFields":[{"text":"myText",
"coords":[20,90,330,60]
},
{"type":"CN",
"coords":[10,70,300,50],
"fontSize":11
}
]
}
}
How change property textFields (textColor ....)?
Copy link to clipboard
Copied
I'm moving this to the Acrobat forum from the Enterprise & Teams Forum.
Copy link to clipboard
Copied
This article publsihed by ACP Thom Parker may be very useful:
Also check the section "Field Properties", starting on page 388 through page 399 of the JavaScript for Acrobat API Reference.
There are plenty of examples to feed from.