代码之家  ›  专栏  ›  技术社区  ›  Bimal Kumar

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

  •  0
  • Bimal Kumar  · 技术社区  · 7 年前

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

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

    信封摘要信封摘要=信封API。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"
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Community miroxlav    4 年前

    确保 signHere.XPosition signHere.YPosition

    以下语句的计算结果可能为十进制值。确保它是整数。

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

    故障排除步骤

    envelopeDefinition.ToJson() (Sdk documentation