在DotNet应用程序中使用DocuSign时出错

时间:2017-07-25 12:25:21

标签: asp.net-mvc docusignapi

我正在我的应用程序中实现DocuSign功能来签署PDF。 我关注以下链接: https://www.docusign.com/developer-center/api-overview 我们可以在使用上面链接中提到的步骤的同时在PDF中添加签名,但在第二次调用相同的方法签署新PDF时,我们在CreateEnvelope()

行获取错误消息
  

错误响应:{“errorCode”:“UNSPECIFIED_ERROR”,“message”:   “输入字符串的格式不正确。” }

在创建信封时会产生错误。在这一行

EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId,envDef);

以下是我使用的代码

static string userName = Utility.GetConfigValue("docSignUserName");
        static string password = Utility.GetConfigValue("docSignPassword");
        static string integratorKey = Utility.GetConfigValue("docSignIntegratorKey");
        static string baseURL = "";

        public static SignedPDF SignDocument(UserViewModel user, StateViewModel state, string signFilePath, string newFilePath, string controlSign)
        {
            PdfReader pdfReader = new PdfReader(signFilePath);
            PdfReader newpdfReader = new PdfReader(newFilePath);
            PdfStamper pdfStamper = new PdfStamper(newpdfReader, new FileStream(HttpContext.Current.Server.MapPath(Utility.GetConfigValue("StateTaxForms")) + "Test.pdf", FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            IList<AcroFields.FieldPosition> fieldPositions = pdfFormFields.GetFieldPositions(controlSign);

            try
            {
                AcroFields.FieldPosition fieldPosition = fieldPositions[0];

                // Enter recipient (signer) name and email address
                string recipientName = user.FirstName + " " + user.LastName;
                string recipientEmail = user.Email;

                // instantiate api client with appropriate environment (for production change to www.docusign.net/restapi)
                string basePath = Utility.GetConfigValue("docSignInstantiateClient");

                // instantiate a new api client
                ApiClient apiClient = new ApiClient(basePath);

                // set client in global config so we don't need to pass it to each API object
                Configuration.Default.ApiClient = apiClient;

                string authHeader = "{\"Username\":\"" + userName + "\", \"Password\":\"" + password + "\", \"IntegratorKey\":\"" + integratorKey + "\"}";
                Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);

                // we will retrieve this from the login() results
                string accountId = null;

                // the authentication api uses the apiClient (and X-DocuSign-Authentication header) that are set in Configuration object
                AuthenticationApi authApi = new AuthenticationApi();
                LoginInformation loginInfo = authApi.Login();

                // user might be a member of multiple accounts
                accountId = loginInfo.LoginAccounts[0].AccountId;

                // Read a file from disk to use as a document
                byte[] fileBytes = File.ReadAllBytes(signFilePath);

                EnvelopeDefinition envDef = new EnvelopeDefinition();
                envDef.EmailSubject = "Please sign this document";

                // Add a document to the envelope
                Document doc = new Document();
                doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
                doc.Name = "SignedFile.pdf";
                doc.DocumentId = "1";

                envDef.Documents = new List<Document>();
                envDef.Documents.Add(doc);

                // Add a recipient to sign the documeent
                Signer signer = new Signer();
                signer.Name = recipientName;
                signer.Email = recipientEmail;
                signer.RecipientId = "1";

                // must set |clientUserId| to embed the recipient
                signer.ClientUserId = "1234";

                // Create a |SignHere| tab somewhere on the document for the recipient to sign
                signer.Tabs = new Tabs();
                signer.Tabs.SignHereTabs = new List<SignHere>();
                SignHere signHere = new SignHere();

                var height = pdfReader.GetPageSize(1).Height;
                signHere.DocumentId = "1";
                signHere.RecipientId = "1";
                signHere.PageNumber = Convert.ToInt32(fieldPosition.page).ToString();
                signHere.XPosition = Convert.ToInt32(fieldPosition.position.Left).ToString();
                if (state.Abbreviation == "DC" && controlSign != "Signature of Employee")
                {
                    signHere.YPosition = (height - Convert.ToInt32(fieldPosition.position.Top - 5)).ToString();
                }
                else
                {
                    signHere.YPosition = (height - Convert.ToInt32(fieldPosition.position.Top + 35)).ToString();
                }
                if (state.Abbreviation == "NC" && controlSign != "Signature of Employee")
                {
                    signHere.YPosition = (height - Convert.ToInt32(fieldPosition.position.Top + 25)).ToString();
                }
                signer.Tabs.SignHereTabs.Add(signHere);

                envDef.Recipients = new Recipients();
                envDef.Recipients.Signers = new List<Signer>();
                envDef.Recipients.Signers.Add(signer);

                // set envelope status to "sent" to immediately send the signature request
                envDef.Status = "sent";

                // Use the EnvelopesApi to create and send the signature request
                EnvelopesApi envelopesApi = new EnvelopesApi();
                EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);

                RecipientViewRequest viewOptions = new RecipientViewRequest()
                {
                    ReturnUrl = Utility.GetConfigValue("docSignReturnURL"),
                    ClientUserId = "1234",  // must match clientUserId set in step #2!
                    AuthenticationMethod = "email",
                    UserName = recipientName,
                    Email = recipientEmail
                };

                // create the recipient view (aka signing URL)
                ViewUrl recipientView = envelopesApi.CreateRecipientView(accountId, envelopeSummary.EnvelopeId, viewOptions);

                // Start the embedded signing session!
                //var value = System.Diagnostics.Process.Start(recipientView.Url);
                SignedPDF signedPDF = new SignedPDF();
                signedPDF.URL = recipientView.Url;
                signedPDF.EnvelopeID = envelopeSummary.EnvelopeId;
                return signedPDF;
            }
            catch (Exception ex)
            {
                throw new PDFSignException(ErrorConstants.THERE_WAS_AN_ERROR_WHILE_SIGNING_PDF);
            }
            finally
            {
                pdfStamper.Close();
                pdfReader.Close();
            }
        }

你好CodingDawg这是JSON

{
  "documents": [
    {
      "documentBase64": "",
      "documentId": "1",
      "name": "SignedFile.pdf"
    }
  ],
  "emailSubject": "Please sign this document",
  "recipients": {
    "signers": [
      {
        "clientUserId": "1234",
        "email": "sagar.mali@tudip.com",
        "name": "Sagar Mali",
        "recipientId": "1",
        "tabs": {
          "signHereTabs": [
            {
              "documentId": "1",
              "pageNumber": "1",
              "recipientId": "1",
              "xPosition": "192",
              "yPosition": "679.968"
            }
          ]
        }
      }
    ]
  },
  "status": "sent"
}

由于长度不支持,我们删除了基本64字符串。

1 个答案:

答案 0 :(得分:1)

确保signHere.XPosition&amp; signHere.YPosition值正确传递。

以下语句可以计算为十进制值。确保它是一个整数。

signHere.YPosition = (height - Convert.ToInt32(fieldPosition.position.Top + 25)).ToString();

疑难解答步骤

请运行envelopeDefinition.ToJson()(Sdk documentation)并确保发布到DocuSign api的最终Json是正确的。