1
我想使用CoSign Web servies API簽署pdf文件。我已經嘗試過代碼示例,並且能夠成功簽名。我正在使用開發者帳戶,並且已經創建了多個圖形簽名,但生成的簽名不包含任何圖像(僅限文本信息),儘管我已將appearanceMask設置爲圖形圖像。如何簽名使用cosign SOAP API的圖形簽名
這裏是我使用的示例代碼:
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;
}
}