1

我正在使用 CoSign 开发人员帐户,并且创建了多个图形签名。我正在使用示例代码对 pdf 文件进行签名,它可以工作,我能够枚举签名,但我无法弄清楚如何从帐户的签名列表中指定要使用的签名。

使用的代码示例:

package trial;

import com.arx.sapiws.dss._1.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import oasis.names.tc.dss._1_0.core.schema.DocumentType.Base64Data;
import oasis.names.tc.dss._1_0.core.schema.*;
import oasis.names.tc.saml._1_0.assertion.*;

public class MultipeSignatures {

    public static void main(String[] args) throws Exception {

        String fileName = "D:/demo - Copy.pdf";
        String signerUsername = "";
        String signerPassword = "";

        if (!checkFile(fileName)) {
            System.out.println("Cannot find '" + fileName + "' or it is read/write protected. Aborting.");
            return;
        }

        // Get all user's graphical signatures
        List<GraphicImageType> gSignatures = listGraphicalSignatures(signerUsername, " ", signerPassword);
        for (GraphicImageType image : gSignatures) {
            System.out.println("image: " + image.getGraphicImageName());
        }

        SignPDF(fileName, signerUsername, signerPassword);
    }

    private static void SignPDF(String fileName, String signerUsername, String signerPassword) {
        System.out.println("Trying to sign '" + fileName + "' by '" + signerUsername + "' with password '" + signerPassword + "' ...");

        byte[] fileBytes = getPDFBytes(fileName);
        Base64Data corePDFBytes = new Base64Data();
        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);
        sigFieldSettings.setX(150);
        sigFieldSettings.setY(110);
        sigFieldSettings.setWidth(150);
        sigFieldSettings.setHeight(50);
        sigFieldSettings.setDependencyMode(DependencyModeEnum.INDEPENDENT);
        sigFieldSettings.setSignatureType(SignatureTypeEnum.DIGITAL);
        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();
        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 sig = result.getSignatureObject();
        DssSignResult.SignatureObject.Base64Signature B64Sig = sig.getBase64Signature();

        try {
            FileOutputStream fos = new FileOutputStream(fileName, true);
            fos.write(B64Sig.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 List<GraphicImageType> listGraphicalSignatures(String username, String domain, String password) throws Exception {
        System.out.println("Retrieving list of graphical signatures for user: " + username);

        DssSignResult result;
        try {
            //initiate connection to COSign SAPI service
            DSS service = new DSS();
            DSSSoap port = service.getDSSSoap12();

            SignRequest request = new SignRequest();

            RequestBaseType.OptionalInputs optInputs = new RequestBaseType.OptionalInputs();

            // SET SIGNATURE TYPE (enumerate graphical signatures)
            optInputs.setSignatureType("http://arx.com/SAPIWS/DSS/1.0/enum-graphic-images");

            // SET USERNAME / PASSWORD
            ClaimedIdentity cosignUser = new ClaimedIdentity();
            NameIdentifierType userName = new NameIdentifierType();
            userName.setValue(username);
            userName.setNameQualifier(domain);
            cosignUser.setName(userName);

            CoSignAuthDataType userPass = new CoSignAuthDataType();
            userPass.setLogonPassword(password);
            cosignUser.setSupportingInfo(userPass);
            optInputs.setClaimedIdentity(cosignUser);

            // SET FLAGS
            optInputs.setFlags(Long.parseLong("00FF0000", 16));

            // ALL DONE, MAKE A CALL:
            request.setOptionalInputs(optInputs);
            request.setInputDocuments(null);
            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) {
                return result.getOptionalOutputs().getAvailableGraphicSignature();
            } else {
                throw new Exception(rc.getResultMessage().getValue());
            }
        } catch (java.rmi.RemoteException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        }
    }

    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;
    }
}
4

1 回答 1

1

只需获取首选图形签名的名称并按照我对您之前问题的回答中的描述进行设置。

于 2014-09-22T11:37:42.990 回答