通过docign rest api在服务器上下载签名文档副本时出现问题



我在asp.net mvc应用程序中使用docusign rest api。我上传了一个。docx文件到docusign。签名后,我正在下载这个文件。它被下载为。docx文件本身。但是打不开。当我用adobereader打开相同的文件时,它可以以pdf文件打开。谁能帮我把签名的文件下载成pdf格式,这样我就可以方便地打开了?

        static string email = "****";   // your account email
        static string password = "****";    // your account password
        static string integratorKey = "******"; 
        static string recipientName = "***";    
        static string documentName = "test1.docx";  
        static string baseURL = ""; 
 public void UploadtoDocuSign(int DocId, string DocName, int ObjectTypeId, int ObjectId)
        {
            try
            {
                User _dealuser = _imanageVacancy.GetDealById(ObjectId).LeadUser;
                string recipientName = _dealuser.FirstName + " " + _dealuser.LastName;  // provide a recipient (signer's) name
                string documentName = DocName;
                string recipientEmail = _dealuser.EmailAddress;
                string sectionname = "";
                DocumentManager _docmanager = GetDocumentById(DocId);
                if (_docmanager.SectionID != null)
                    sectionname = "\" + GetSectionName_additionalDoc((int)_docmanager.SectionID);
                string FileNameWithPath = CommonFunctions.ConfigReader.GetConfigValue("appSettings", "FileUploadPath") + "\Documents\" + EnumsNeeded.VacancySubMenu.Deal.ToString() + "\" + ObjectId + sectionname + "\" + documentName;
                //============================================================================
                // STEP 1 - Login API Call (used to retrieve your baseUrl)
                //============================================================================
                // Endpoint for Login api call (in demo environment):
                string url = "https://demo.docusign.net/restapi/v2/login_information";
                // set request url, method, and headers. No body needed for login api call
                HttpWebRequest request = initializeRequest(url, "GET", null, email, password);
                // read the http response
                string response = getResponseBody(request);
                // parse baseUrl from response body
                baseURL = parseDataFromResponse(response, "baseUrl");
                //============================================================================
                // STEP 2 - Send Signature Request from Template
                //============================================================================
                /*
                This is the only DocuSign API call that requires a "multipart/form-data" content type. We will be
                constructing a request body in the following format (each newline is a CRLF):
                --AAA
                Content-Type: application/xml
                Content-Disposition: form-data
                <XML BODY GOES HERE>
                --AAA
                Content-Type:application/pdf
                Content-Disposition: file; filename="document.pdf"; documentid=1
                <DOCUMENT BYTES GO HERE>
                --AAA--
                */
                // append "/envelopes" to baseURL and use for signature request api call
                url = baseURL + "/envelopes";
                // construct an outgoing XML formatted request body (JSON also accepted)
                // .. following body adds one signer and places a signature tab 100 pixels to the right
                // and 100 pixels down from the top left corner of the document you supply
                string xmlBody =
                "<envelopeDefinition xmlns="http://www.docusign.com/restapi">" +
                "<emailSubject>DocuSign API - Signature Request on Document</emailSubject>" +
                "<status>sent</status>" + // "sent" to send immediately, "created" to save as draft in your account
                    // add document(s)
                "<documents>" +
                "<document>" +
                "<documentId>1</documentId>" +
                "<name>" + documentName + "</name>" +
                "</document>" +
                "</documents>" +
                    // add recipient(s)
                "<recipients>" +
                "<signers>" +
                "<signer>" +
                "<recipientId>1</recipientId>" +
                "<email>" + recipientEmail + "</email>" +
                "<name>" + recipientName + "</name>" +
                "<tabs>" +
                "<signHereTabs>" +
                "<signHere>" +
                "<xPosition>100</xPosition>" + // default unit is pixels
                "<yPosition>100</yPosition>" + // default unit is pixels
                "<documentId>1</documentId>" +
                "<pageNumber>4</pageNumber>" +
                "</signHere>" +
                "</signHereTabs>" +
                "</tabs>" +
                "</signer>" +
                "</signers>" +
                "</recipients>" +
                "</envelopeDefinition>";
                // set request url, method, headers. Don't set the body yet, we'll set that separelty after
                // we read the document bytes and configure the rest of the multipart/form-data request
                request = initializeRequest(url, "POST", null, email, password);
                // some extra config for this api call
                configureMultiPartFormDataRequest(request, xmlBody, documentName, FileNameWithPath);
                // read the http response
                response = getResponseBody(request);
                string envelopeId = parseDataFromResponse(response, "envelopeId");
                _docmanager.DocuSignReferenceId = envelopeId;
                _docmanager.DocuSignStatus = "Sent";
                _iDocRep.Update(_docmanager);
                _unitOfWork.Commit();
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                    }
                }
            }
        }
        public void DocuSignDownload()
        {
            try
            {
                //============================================================================
                // STEP 1 - Login API Call (used to retrieve your baseUrl)
                //============================================================================
                // Endpoint for Login api call (in demo environment):
                string url = "https://demo.docusign.net/restapi/v2/login_information";
                // set request url, method, and headers. No body needed for login api call
                HttpWebRequest request = initializeRequest(url, "GET", null, email, password);
                // read the http response
                string response = getResponseBody(request);
                // parse baseUrl from response body
                baseURL = parseDataFromResponse(response, "baseUrl");
                //============================================================================
                // STEP 2 - Get Statuses of a set of envelopes
                //============================================================================
                //*** This example gets statuses of all envelopes in your account going back 1 month...
                int curr_month = System.DateTime.Now.Month;
                int curr_day = System.DateTime.Now.Day;
                int curr_year = System.DateTime.Now.Year;
                if (curr_month != 1)
                {
                    curr_month -= 1;
                }
                else
                { // special case for january
                    curr_month = 12;
                    curr_year -= 1;
                }
                // append "/envelopes?from_date=MONTH/DAY/YEAR" and use in get statuses api call
                // we need to URL encode the slash (/) chars, whos URL encoding is: %2F
                url = baseURL + "/envelopes?from_date=" + curr_month.ToString() + "%2F" + curr_day.ToString() + "%2F" + curr_year.ToString();
                // set request url, method, and headers. No request body for this api call...
                request = initializeRequest(url, "GET", null, email, password);
                // read the http response
                response = getResponseBody(request);
                string ComplenvIds = CompletedenvelopeIds(response);
                ComplenvIds = ComplenvIds.TrimEnd(',');
                string[] envIds = ComplenvIds.Split(',');
                foreach (var envid in envIds)
                {
                    DocumentManager _doc = _iDocRep.Get(x => x.DocuSignReferenceId == envid);
                    if (_doc != null)
                    {
                        DownloadCompletedEnvelopes(envid, _doc);
                        _doc.DocuSignStatus = "Completed";
                        _iDocRep.Update(_doc);
                        _unitOfWork.Commit();
                    }
                }
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                    }
                }
            }
        }
        public void DownloadCompletedEnvelopes(string envId, DocumentManager doc)
        {
            try
            {
                //============================================================================
                // STEP 1 - Login API Call (used to retrieve your baseUrl)
                //============================================================================
                // Endpoint for Login api call (in demo environment):
                string url = "https://demo.docusign.net/restapi/v2/login_information";
                // set request url, method, and headers. No body needed for login api call
                HttpWebRequest request = initializeRequest(url, "GET", null, email, password);
                // read the http response
                string response = getResponseBody(request);
                // parse baseUrl from response body
                baseURL = parseDataFromResponse(response, "baseUrl");
                //============================================================================
                // STEP 2 - Get Envelope Document(s) List and Info
                //============================================================================
                // append "/envelopes/{envelopeId}/documents" to to baseUrl and use for next endpoint
                url = baseURL + "/envelopes/" + envId + "/documents";
                //url = baseURL + "/envelopes/";
                // set request url, method, body, and headers
                request = initializeRequest(url, "GET", null, email, password);
                // read the http response
                response = getResponseBody(request);
                // store each document name and uri locally, so that we can subsequently download each one
                Dictionary<string, string> docsList = new Dictionary<string, string>();
                string uri, name;
                using (XmlReader reader = XmlReader.Create(new StringReader(response)))
                {
                    while (reader.Read())
                    {
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "envelopeDocument"))
                        {
                            XmlReader reader2 = reader.ReadSubtree();
                            uri = ""; name = "";
                            while (reader2.Read())
                            {
                                if ((reader2.NodeType == XmlNodeType.Element) && (reader2.Name == "name"))
                                {
                                    name = reader2.ReadString();
                                }
                                if ((reader2.NodeType == XmlNodeType.Element) && (reader2.Name == "uri"))
                                {
                                    uri = reader2.ReadString();
                                }
                            }// end while
                            docsList.Add(name, uri);
                        }
                    }
                }
                //============================================================================
                // STEP 3 - Download the Document(s)
                //============================================================================
                //string envelopeID = "Some EnvelopeID";
                //EnvelopePDF envPDF = apiService.RequestPDF(envelopeID);
                foreach (KeyValuePair<string, string> kvp in docsList)
                {
                    // append document uri to baseUrl and use to download each document(s)
                    url = baseURL + kvp.Value;
                    // set request url, method, body, and headers
                    request = initializeRequest(url, "GET", null, email, password);
                    request.Accept = "application/pdf"; // documents are converted to PDF in the DocuSign cloud
                    // read the response and store into a local file:
                    HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                    string sectionname = "";
                    if (doc.SectionID != null)
                        sectionname = "\" + GetSectionName_additionalDoc((int)doc.SectionID);
                    using (MemoryStream ms = new MemoryStream())
                    using (FileStream outfile = new FileStream(CommonFunctions.ConfigReader.GetConfigValue("appSettings", "FileUploadPath") + "\Documents\" + EnumsNeeded.VacancySubMenu.Deal.ToString() + "\" + doc.ObjectID + sectionname + "\" + kvp.Key, FileMode.Create))
                    {  
                            webResponse.GetResponseStream().CopyTo(ms);
                            if (ms.Length > int.MaxValue)
                            {
                                throw new NotSupportedException("Cannot write a file larger than 2GB.");
                            }
                            outfile.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    }
                }
                Console.WriteLine("nDone downloading document(s), check local directory.");
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                    }
                }
            }
        }
        //***********************************************************************************************
        // --- HELPER FUNCTIONS ---
        //***********************************************************************************************
        public static HttpWebRequest initializeRequest(string url, string method, string body, string email, string password)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            addRequestHeaders(request, email, password);
            if (body != null)
                addRequestBody(request, body);
            return request;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void addRequestHeaders(HttpWebRequest request, string email, string password)
        {
            // authentication header can be in JSON or XML format. XML used for this walkthrough:
            string authenticateStr =
            "<DocuSignCredentials>" +
            "<Username>" + email + "</Username>" +
            "<Password>" + password + "</Password>" +
            "<IntegratorKey>" + integratorKey + "</IntegratorKey>" + // global (not passed)
            "</DocuSignCredentials>";
            request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
            request.Accept = "application/xml";
            request.ContentType = "application/xml";
        }
        public static void addRequestBody(HttpWebRequest request, string requestBody)
        {
            // create byte array out of request body and add to the request object
            byte[] body = System.Text.Encoding.UTF8.GetBytes(requestBody);
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(body, 0, requestBody.Length);
            dataStream.Close();
        }
        public static void configureMultiPartFormDataRequest(HttpWebRequest request, string xmlBody, string docName, string FileNameWithPath)
        {
            // overwrite the default content-type header and set a boundary marker
            request.ContentType = "multipart/form-data; boundary=BOUNDARY";
            // start building the multipart request body
            string requestBodyStart = "rnrn--BOUNDARYrn" +
            "Content-Type: application/xmlrn" +
            "Content-Disposition: form-datarn" +
            "rn" +
            xmlBody + "rnrn--BOUNDARYrn" + // our xml formatted envelopeDefinition
            "Content-Type: application/pdfrn" +
            "Content-Disposition: file; filename="" + docName + ""; documentId=1rn" +
            "rn";
            string requestBodyEnd = "rn--BOUNDARY--rnrn";
            // read contents of provided document into the request stream
            FileStream fileStream = System.IO.File.OpenRead(FileNameWithPath);
            // write the body of the request
            byte[] bodyStart = System.Text.Encoding.UTF8.GetBytes(requestBodyStart.ToString());
            byte[] bodyEnd = System.Text.Encoding.UTF8.GetBytes(requestBodyEnd.ToString());
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(bodyStart, 0, requestBodyStart.ToString().Length);
            // Read the file contents and write them to the request stream. We read in blocks of 4096 bytes
            byte[] buf = new byte[4096];
            int len;
            while ((len = fileStream.Read(buf, 0, 4096)) > 0)
            {
                dataStream.Write(buf, 0, len);
            }
            dataStream.Write(bodyEnd, 0, requestBodyEnd.ToString().Length);
            dataStream.Close();
        }
        public static string getResponseBody(HttpWebRequest request)
        {
            // read the response stream into a local string
            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string responseText = sr.ReadToEnd();
            return responseText;
        }
        public static string parseDataFromResponse(string response, string searchToken)
        {
            // look for "searchToken" in the response body and parse its value
            using (XmlReader reader = XmlReader.Create(new StringReader(response)))
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == searchToken))
                        return reader.ReadString();
                }
            }
            return null;
        }
        public static string CompletedenvelopeIds(string response)
        {
            string compenvIds = "";
            string envId = "";
            // look for "searchToken" in the response body and parse its value
            using (XmlReader reader = XmlReader.Create(new StringReader(response)))
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "envelopes"))
                    {
                        XmlReader reader2 = reader.ReadSubtree();
                        while (reader2.Read())
                        {
                            if ((reader2.NodeType == XmlNodeType.Element) && (reader2.Name == "envelope"))
                            {
                                XmlReader reader3 = reader2.ReadSubtree();
                                envId = "";
                                while (reader3.Read())
                                {
                                    if ((reader3.NodeType == XmlNodeType.Element) && (reader3.Name == "envelopeId"))
                                    {
                                        envId = reader3.ReadString();
                                    }
                                    if ((reader3.NodeType == XmlNodeType.Element) && (reader3.Name == "status"))
                                    {
                                        if (reader3.ReadString() == "completed")
                                            compenvIds = string.Concat(compenvIds, envId + ",");
                                    }
                                    if ((reader3.NodeType == XmlNodeType.Element) && (reader3.Name == "status"))
                                    {
                                        DateTime date = reader3.ReadContentAsDateTime();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return compenvIds;
        }
        public List<Lookup> LookupCache()
        {
            List<Lookup> _lookup = (List<Lookup>)HttpContext.Current.Cache["Lookup"];
            if (HttpContext.Current.Cache["Lookup"] == null)
                _lookup = _imanageUser.UpdateCache();
            return _lookup;
        }
        string GetSectionName_additionalDoc(int SectionId)
        {
            List<Lookup> _lookup = LookupCache();
            string _section = _lookup.Find(x => x.LookupType == (int)EnumsNeeded.EnumTypes.AdditionalDocumentTypes && x.LookupId == SectionId).LookupDesc;
            return _section;
        }

所有来自DocuSign的文件都将是。pdf文件。您的文档将被保存为他们上传的名称(通常包含扩展名)。如果你写的文件名字是它被DocumentPDF->Name存储在DocuSign中。我会用。replace替换扩展名为。pdf。

相关内容

最新更新