我正在尝试使用CoSign Web服务API签署pdf文件。我已经尝试了代码示例,我能够成功签名。我使用的是开发者帐户,我创建了几个图形签名,但生成的签名不包含任何图像(仅文本信息),尽管我已将appearanceMask设置为图形图像。
这是我正在使用的示例代码:
package trial;
import com.arx.sapiws.dss._1.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import oasis.names.tc.dss._1_0.core.schema.*;
import oasis.names.tc.saml._1_0.assertion.NameIdentifierType;
public class CosignHelloWorld {
public static void main(String[] args) throws MalformedURLException {
String fileName = "D:/demo - Copy.pdf";
String signerUsername = "";
String signerPassword = "";
System.out.println("Trying to sign '" + fileName + "' by '" + signerUsername + "' with password '" + signerPassword + "' ...");
if (!checkFile(fileName)) {
System.out.println("Cannot find '" + fileName + "' or it is read/write protected. Aborting.");
return;
}
//set document properties
DocumentType.Base64Data corePDFBytes = new DocumentType.Base64Data();
byte[] fileBytes = getPDFBytes(fileName);
corePDFBytes.setValue(fileBytes);
corePDFBytes.setMimeType("application/pdf");
DocumentType myDoc = new DocumentType();
myDoc.setBase64Data(corePDFBytes);
// set signer - USERNAME/DOMAIN
NameIdentifierType cosignUserProps = new NameIdentifierType();
cosignUserProps.setValue(signerUsername);
cosignUserProps.setNameQualifier(" ");
// set signer - PASSWORD
CoSignAuthDataType cosignPassword = new CoSignAuthDataType();
cosignPassword.setLogonPassword(signerPassword);
// set signer - USERNAME+PASSWORD
ClaimedIdentity cosignUser = new ClaimedIdentity();
cosignUser.setName(cosignUserProps);
cosignUser.setSupportingInfo(cosignPassword);
//set signature field settings
SAPISigFieldSettingsType sigFieldSettings = new SAPISigFieldSettingsType();
sigFieldSettings.setName("SigField");
sigFieldSettings.setInvisible(Boolean.FALSE); //visible
sigFieldSettings.setX(150);
sigFieldSettings.setY(110);
sigFieldSettings.setWidth(150);
sigFieldSettings.setHeight(50);
sigFieldSettings.setDependencyMode(DependencyModeEnum.INDEPENDENT);
sigFieldSettings.setSignatureType(SignatureTypeEnum.DIGITAL);
sigFieldSettings.setAppearanceMask(11L);
sigFieldSettings.setEmptyFieldLabel("");
sigFieldSettings.setPage(1);
//initiate request components
RequestBaseType.InputDocuments inputs = new RequestBaseType.InputDocuments();
inputs.getOtherOrDocumentHashOrTransformedData().add(myDoc);
RequestBaseType.OptionalInputs optInputs = new RequestBaseType.OptionalInputs();
optInputs.setSignatureType("http://arx.com/SAPIWS/DSS/1.0/signature-field-create-sign");
optInputs.setClaimedIdentity(cosignUser);
optInputs.setSAPISigFieldSettings(sigFieldSettings);
optInputs.setReturnPDFTailOnly(Boolean.TRUE);
//initiate connection to COSign SAPI service
DSS service = new DSS(new URL("https://prime.cosigntrial.com:8080/sapiws/dss.asmx"));
DSSSoap port = service.getDSSSoap12();
//initiate request
SignRequest request = new SignRequest();
request.setOptionalInputs(optInputs);
request.setInputDocuments(inputs);
request.setRequestID("DummyRequestId");
//make the call
DssSignResult result = port.dssSign(request);
//CHECK RESULT:
ResponseBaseType.Result rc = result.getResult();
String errmsg = rc.getResultMajor();
if (errmsg.compareTo("urn:oasis:names:tc:dss:1.0:resultmajor:Success") != 0) {
System.out.println("Cannot sign '" + fileName + "' got: " + rc.getResultMessage().getValue());
return;
}
System.out.println("Trying to append signature to '" + fileName + "' ('" + signerUsername + "') ...");
// IF OK Get the signature:
DssSignResult.SignatureObject sigObj = result.getSignatureObject();
DssSignResult.SignatureObject.Base64Signature sigBase64 = sigObj.getBase64Signature();
try {
FileOutputStream fos = new FileOutputStream(fileName, true);
fos.write(sigBase64.getValue());
fos.close();
} catch (Exception e) {
System.out.println("Error writing to PDF file: " + e.getMessage());
return;
}
System.out.println("Signature of '" + signerUsername + "' appended to '" + fileName + "'.");
}
private static byte[] getPDFBytes(String FileName) {
byte[] pdf;
// READ THE FILE INTO BUFFER
try {
File file = new File(FileName);
if (!file.canRead()) {
return null;
}
if (!file.canWrite()) {
return null;
}
pdf = new byte[(int) file.length()];
FileInputStream fos = new FileInputStream(file);
fos.read(pdf, 0, (int) file.length());
fos.close();
} catch (Exception e) {
return null;
}
return pdf;
}
private static boolean checkFile(String fileName) {
try {
File file = new File(fileName);
if (!file.canRead()) {
return false;
}
if (!file.canWrite()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
}
}
答案 0 :(得分:1)
由于您的CoSign帐户中有多个图形签名,因此您必须指明用于签署SAPI代码的默认图形签名。
一种简单的方法是通过提供默认图形签名的名称作为配置值的一部分。将以下代码段添加到您的代码中:
ArrayOfConfValueType confValArray = new ArrayOfConfValueType();
ConfValueType confVal = new ConfValueType();
confVal.setConfValueID(ConfIDEnum.GR_SIG_PREF_NAME);
confVal.setStringValue("{GRAPHICAL_IMAGE_NAME}");
confValArray.getConfValue().add(confVal);
optInputs.setConfigurationValues(confValArray);