使用CoSign对位于SharePoint中的文档进行签名

时间:2018-10-25 10:38:23

标签: cosign-api

我正在尝试使用CoSign API获取签名字段并签署位于SharePoint中的docx文档。

使用我创建的FileHandler对象的字节数组,我可以从SharePoint文件中获取内存流或字节数组。

我在下面发布了我正在使用的辅助方法,FileHandle返回一个对象,但是SAPI.SignatureFieldEnumInitEx方法返回0个字段,并使用SharePoint加载项检查文档,示例代码返回2个字段。问题似乎出在我要发送的byteArray中,打开文档的item.File.OpenBinary()方法似乎不正确。

using (SPSite site = new SPSite("http://sharepointweburl"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList documents = web.Lists["Documents"];
            SPListItem item = documents.GetItemById(8);
            Console.WriteLine("Item: {0}", item.Name);
            var binaryFile = item.File.OpenBinary();

            var handle = DocuSign.GetFileHandleFromStream(binaryFile);
            var fields = DocuSign.GetSignatureFieldsFromHandle(handle);
        }
    }

public static FileHandle GetFileHandleFromStream(byte[] byteArray)
{
    FileHandle fileHandle = null;
    var sapiByteArray = new SAPIByteArray();
    sapiByteArray.FromArray(byteArray);
    SAPIContext ctxSigField = new SAPIContextClass();
    SAPI.CreateFileHandleByMem(out fileHandle, SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_OFFICE_XML_PACKAGE, 0, sapiByteArray);
    return fileHandle;
}

public static DOCXField[] GetSignatureFieldsFromHandle(FileHandle fileHandle)
{
    SAPIContext ctxSigField = new SAPIContextClass();
    int rc;
    int NumOfFields = 0;
    rc = SAPI.SignatureFieldEnumInitEx(
        hSession,
        ctxSigField,
        SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_OFFICE_XML_PACKAGE,
        "",
        fileHandle,
        0,
        ref NumOfFields
        );
    DOCXField[] fields = new DOCXField[NumOfFields];
    Console.WriteLine("Found {0} signature fields", NumOfFields);
    for (int i = 0; i < NumOfFields; i++)
    {
        fields[i] = new DOCXField();
        //Get Signature Field Handle
        rc = SAPI.SignatureFieldEnumCont(hSession, ctxSigField, out fields[i].hSigField);
        if (rc != 0) throw new Exception("Failed in SignatureFieldEnumCont (" + rc.ToString("X") + ")");

        //Get Signature Field Details
        rc = SAPI.SignatureFieldInfoGet(
            hSession,
            fields[i].hSigField,
            fields[i].sSettings,
            fields[i].sInfo);

        if (rc != 0) throw new Exception("Failed in SignatureFieldInfoGet (" + rc.ToString("X") + ")");
    }
    SAPI.ContextRelease(ctxSigField);
    return fields;
}

1 个答案:

答案 0 :(得分:0)

CoSign API不支持将内存流用于DOCX(仅PDF)。唯一的选择是将内存流另存为临时文件,然后使用文件路径调用SAPI方法。