コード例 #1
0
        public HttpResponseMessage GetFinalSignedPDFDocumentHeader(int envelopeCode)
        {
            HttpResponseMessage responseToClient = new HttpResponseMessage();
            var base64String = string.Empty;

            byte[] binaryData = new Byte[102222];
            //eSign.Web.Controllers.DocumentPackageController doc = new Web.Controllers.DocumentPackageController();
            EnvelopeHelperMain objEnvelope = new EnvelopeHelperMain();
            Guid   envelopeId;
            string finalDocumentName = string.Empty;

            try
            {
                System.Collections.Generic.IEnumerable <string> iHeader;
                Request.Headers.TryGetValues("AuthToken", out iHeader);
                string authToken = iHeader.ElementAt(0);
                using (var objectcontext = new eSignEntities())
                {
                    //var helper = new eSign.Web.Helpers.EnvelopeHelper();
                    UserTokenRepository tokenRepository    = new UserTokenRepository(objectcontext);
                    EnvelopeRepository  envelopeRepository = new EnvelopeRepository(objectcontext);
                    string userEmail = tokenRepository.GetUserEmailByToken(authToken);
                    Guid   UserId    = tokenRepository.GetUserProfileUserIDByID(tokenRepository.GetUserProfileIDByEmail(userEmail));
                    string eId       = envelopeRepository.GetEnvelopeIdByDisplayCode(envelopeCode, UserId);
                    finalDocumentName = envelopeRepository.GetFinalDocumentName(new Guid(eId));
                    if (string.IsNullOrEmpty(eId))
                    {
                        ResponseMessage responseMessage = new ResponseMessage();
                        responseMessage.StatusCode    = HttpStatusCode.NoContent;
                        responseMessage.StatusMessage = "NoContent";
                        responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["NoContent"].ToString());
                        responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessage);
                        return(responseToClient);
                    }
                    envelopeId = new Guid(eId);
                    Envelope envelopeObject = envelopeRepository.GetEntity(envelopeId);
                    if (envelopeObject == null)
                    {
                        ResponseMessage responseMessage = new ResponseMessage();
                        responseMessage.StatusCode    = HttpStatusCode.NoContent;
                        responseMessage.StatusMessage = "NoContent";
                        responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["NoContent"].ToString());
                        responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessage);
                        return(responseToClient);
                    }
                    if (envelopeObject.StatusID == Constants.StatusCode.Envelope.Waiting_For_Signature)
                    {
                        ResponseMessage responseMessage = new ResponseMessage();
                        responseMessage.StatusCode    = HttpStatusCode.BadRequest;
                        responseMessage.StatusMessage = "BadRequest";
                        responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["PartialComplete"].ToString());
                        responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessage);
                        return(responseToClient);
                    }
                    else if (envelopeObject.StatusID == Constants.StatusCode.Envelope.Incomplete_and_Expired)
                    {
                        ResponseMessage responseMessage = new ResponseMessage();
                        responseMessage.StatusCode    = HttpStatusCode.BadRequest;
                        responseMessage.StatusMessage = "BadRequest";
                        responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["IncompleteAndExpired"].ToString());
                        responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessage);
                        return(responseToClient);
                    }
                    else if (envelopeObject.StatusID == Constants.StatusCode.Envelope.Terminated)
                    {
                        ResponseMessage responseMessage = new ResponseMessage();
                        responseMessage.StatusCode    = HttpStatusCode.BadRequest;
                        responseMessage.StatusMessage = "BadRequest";
                        responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["Terminated"].ToString());
                        responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessage);
                        return(responseToClient);
                    }

                    string permanentPDFFilePath = ConfigurationManager.AppSettings["PermanentPDFPathStart"].ToString() + envelopeId.ToString() + ConfigurationManager.AppSettings["PermanentPDFPathEnd"].ToString();
                    string finalPdfFilePath     = ConfigurationManager.AppSettings["PDFPathStart"].ToString() + envelopeId.ToString() + ConfigurationManager.AppSettings["PDFPathEnd"].ToString();

                    if (System.IO.File.Exists(permanentPDFFilePath))
                    {
                        System.IO.FileStream inFile;
                        inFile     = new System.IO.FileStream(permanentPDFFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        binaryData = new Byte[inFile.Length];
                        long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
                        base64String = Convert.ToBase64String(binaryData, 0, (int)bytesRead);
                        inFile.Close();
                    }
                    else if (System.IO.File.Exists(finalPdfFilePath))
                    {
                        System.IO.FileStream inFile;
                        inFile     = new System.IO.FileStream(finalPdfFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        binaryData = new Byte[inFile.Length];
                        long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
                        base64String = Convert.ToBase64String(binaryData, 0, (int)bytesRead);
                        inFile.Close();
                    }
                    else
                    {
                        var result = objEnvelope.DownloadPDFApi(envelopeObject);
                        System.IO.FileStream inFile;
                        inFile     = new System.IO.FileStream(result, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        binaryData = new Byte[inFile.Length];
                        long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
                        base64String = Convert.ToBase64String(binaryData, 0, (int)bytesRead);
                        inFile.Close();
                    }
                }

                ResponseMessagePDF responseMessagePDF = new ResponseMessagePDF();
                responseMessagePDF.StatusCode    = HttpStatusCode.OK;
                responseMessagePDF.StatusMessage = "OK";
                responseMessagePDF.FileName      = finalDocumentName + ".pdf";
                responseMessagePDF.Message       = "Following is the binary data of pdf.";
                responseMessagePDF.PdfInBinary   = binaryData;
                responseToClient = Request.CreateResponse(HttpStatusCode.OK, responseMessagePDF);
                return(responseToClient);
            }
            catch (Exception ex)
            {
                responseToClient         = Request.CreateResponse((HttpStatusCode)422);
                responseToClient.Content = new StringContent(ex.Message, Encoding.Unicode);
                throw new HttpResponseException(responseToClient);
            }
        }