コード例 #1
0
        public ActionResult Sample43()
        {
            // Check is data posted
            if (Request.HttpMethod == "POST")
            {
                //### Set variables and get POST data
                System.Collections.Hashtable result = new System.Collections.Hashtable();
                String clientId = Request.Form["clientId"];
                String privateKey = Request.Form["privateKey"];
                String fileId = Request.Form["fileId"];
                String url = Request.Form["url"];
                var file = Request.Files["file"];
                String guid = "";
                String basePath = Request.Form["basePath"];
                // Set entered data to the results list
                result.Add("clientId", clientId);
                result.Add("privateKey", privateKey);
                result.Add("basePath", basePath);
                String message = null;
                String iframe = "";
                // Check is all needed fields are entered
                if (clientId == null || privateKey == null)
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample43", null, result);
                }
                else
                {

                    if (basePath.Equals(""))
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);

                    //if URL to web file was provided - upload the file from Web and get it's GUID
                    if (!url.Equals(""))
                    {

                        //Make request to upload file from entered Url
                        String uploadWeb = service.UploadUrl(url);
                        if (uploadWeb != null)
                        {
                            //If file uploaded return his GuId
                            guid = uploadWeb;

                        }
                        //If file wasn't uploaded return error
                        else
                        {
                            result.Add("error", "Something wrong with entered data");
                            return View("Sample43", null, result);
                        }

                    }
                    //if file was uploaded locally - upload the file and get it's GUID
                    if (!file.FileName.Equals(""))
                    {

                        //Upload local file
                        Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream);
                        if (upload.Guid != null)
                        {
                            //If file uploaded return his guid
                            guid = upload.Guid;

                        }
                        else
                        {
                            //If file wasn't uploaded return error
                            result.Add("error", "Something wrong with entered data");
                            return View("Sample43", null, result);
                        }

                    }
                    if (fileId != null)
                    {
                        guid = fileId;

                    }
                    //Create list of file types for converting
                    System.Collections.Generic.List<Groupdocs.Common.FileType> fileType = new System.Collections.Generic.List<Groupdocs.Common.FileType>();
                    //Add PDF to list of file types
                    fileType.Add(Groupdocs.Common.FileType.Doc);
                    Random rand = new Random();
                    //Create job which will add line numeration to the document
                    Groupdocs.Api.Contract.CreateJobResult createJob = service.CreateJob(Groupdocs.Common.JobActions.NumberLines | Groupdocs.Common.JobActions.Convert, fileType.ToArray(), false, false, "test" + rand.Next());
                    if (!createJob.JobGuid.Equals(""))
                    {

                        //Add document to the new job
                        Boolean addJobDocument = service.AddJobDocument(createJob.JobGuid, guid, "", false);
                        if (addJobDocument.Equals(false))
                        {
                            message = "Add document to job is failed";
                            result.Add("error", message);
                            return View("Sample43", null, result);
                        }
                        else
                        {

                            //Run job
                            Boolean schedule = service.ScheduleJob(createJob.JobId);
                            System.Threading.Thread.Sleep(5000);
                            //Get documents from job
                            int counter = 5;
                            for (int i = 0; i < counter; i++)
                            {
                                Groupdocs.Api.Contract.GetJobDocumentsResult getDocument = service.GetJobDocuments(createJob.JobId);
                                if (!getDocument.JobStatus.ToString().Equals("Archived"))
                                {
                                    System.Threading.Thread.Sleep(5000);
                                }
                                else
                                {
                                    guid = getDocument.Inputs[0].Outputs[0].Guid;
                                    break;
                                }
                            }
                            if (String.IsNullOrEmpty(guid))
                            {
                                message = "Get document from job is failed or job is Draft";
                                result.Add("error", message);
                                return View("Sample43", null, result);
                            }
                            else
                            {
                                // Generate Embed viewer url with entered file id
                                if (basePath.Equals("https://api.groupdocs.com/v2.0"))
                                {
                                    iframe = "https://apps.groupdocs.com/document-viewer/embed/" + guid;
                                }
                                if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0"))
                                {
                                    iframe = "https://dev-apps-groupdocs.dynabic.com/document-viewer/embed/" + guid;
                                }
                                if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0"))
                                {
                                    iframe = "https://stage-apps-groupdocs.dynabic.com/document-viewer/embed/" + guid;
                                }
                                if (basePath.Equals("https://realtime-api-groupdocs.dynabic.com/v2.0"))
                                {
                                    iframe = "https://realtime-apps-groupdocs.dynabic.com/document-viewer/embed/" + guid;
                                }
                                iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey);
                                result.Add("iframe", iframe);
                                result.Add("fileId", guid);
                                return View("Sample43", null, result);
                            }
                        }
                    }
                    else
                    {
                        result.Add("error", "Create job is failed");
                        return View("Sample43", null, result);
                    }
                }
            }
            // If data not posted return to template for filling of necessary fields
            else
            {
                return View("Sample43");
            }
        }
コード例 #2
0
        public ActionResult Sample42()
        {
            // Check is data posted
            if (Request.HttpMethod == "POST")
            {
                //### Set variables and get POST data
                System.Collections.Hashtable result = new System.Collections.Hashtable();
                String clientId = Request.Form["clientId"];
                String privateKey = Request.Form["privateKey"];
                String fileId = Request.Form["fileId"];
                String basePath = Request.Form["basePath"];
                String guid = "";
                String message = null;
                String iframe = "";
                String name = "";
                if (clientId == null || privateKey == null)
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample42", null, result);
                }
                else
                {
                    if (basePath == "")
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    //Add data to template
                    result.Add("basePath", basePath);
                    result.Add("clientId", clientId);
                    result.Add("privateKey", privateKey);
                    result.Add("fileId", fileId);

                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);
                    //Create list with URL's
                    Groupdocs.Api.Contract.Annotation.ListAnnotationsResult allAnnotations = service.ListAnnotations(fileId);
                    if (allAnnotations.Annotations.Length != 0)
                    {
                        Random rand = new Random();
                        //Create job which will get document with annotations
                        Groupdocs.Api.Contract.CreateJobResult createJob = service.CreateJob(Groupdocs.Common.JobActions.ImportAnnotations, null, false, false, "test" + rand.Next());
                        if (!createJob.JobGuid.Equals(""))
                        {
                            //Add document with annotations to new job
                            Boolean addJobDocument = service.AddJobDocument(createJob.JobGuid, fileId, "pdf", false);
                            if (addJobDocument.Equals(false))
                            {
                                message = "Add document to job is failed";
                                result.Add("error", message);
                                return View("Sample42", null, result);
                            }
                            else
                            {
                                //Run job
                                Boolean schedule = service.ScheduleJob(createJob.JobId);
                                System.Threading.Thread.Sleep(5000);
                                //Get documents from job
                                int counter = 5;
                                for (int i = 0; i < counter; i++)
                                {
                                    Groupdocs.Api.Contract.GetJobDocumentsResult getDocument = service.GetJobDocuments(createJob.JobId);
                                    if (!getDocument.JobStatus.ToString().Equals("Archived"))
                                    {
                                        System.Threading.Thread.Sleep(5000);
                                    }
                                    else
                                    {
                                        guid = getDocument.Inputs[0].Outputs[0].Guid;
                                        name = getDocument.Inputs[0].Outputs[0].Name;
                                        break;
                                    }
                                }
                                if (String.IsNullOrEmpty(guid))
                                {
                                    message = "Get document from job is failed or job is Draft";
                                    result.Add("error", message);
                                    return View("Sample42", null, result);
                                }
                                else
                                {
                                    // Generate Embed Annotation url with entered file id
                                    if (basePath.Equals("https://api.groupdocs.com/v2.0"))
                                    {
                                        iframe = "https://apps.groupdocs.com/document-annotation/embed/" + guid;
                                    }
                                    if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0"))
                                    {
                                        iframe = "https://dev-apps-groupdocs.dynabic.com/document-annotation/embed/" + guid;
                                    }
                                    if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0"))
                                    {
                                        iframe = "https://stage-apps-groupdocs.dynabic.com/document-annotation/embed/" + guid;
                                    }
                                    if (basePath.Equals("https://realtime-api-groupdocs.dynabic.com/v2.0"))
                                    {
                                        iframe = "https://realtime-apps-groupdocs.dynabic.com/document-annotation/embed/" + guid;
                                    }
                                    iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey);
                                    result.Add("iframe", iframe);
                                    // Definition of folder where to download file
                                    String downloadFolder = AppDomain.CurrentDomain.BaseDirectory + "downloads/";

                                    if (!Directory.Exists(downloadFolder))
                                    {
                                        DirectoryInfo di = Directory.CreateDirectory(downloadFolder);
                                    }

                                    //### Make a request to Storage Api for dowloading file
                                    // Download file
                                    bool file = service.DownloadFile(guid, downloadFolder + name);
                                    message = "File with annotations was downloaded to server's local folder. You can download it from ";
                                    result.Add("message", message);
                                    result.Add("name", name);
                                    return View("Sample42", null, result);
                                }
                            }
                        }
                        else
                        {
                            message = "Create job is failed";
                            result.Add("error", message);
                            return View("Sample42", null, result);
                        }
                    }
                    else
                    {
                        message = "Get all anotations is failed";
                        result.Add("error", message);
                        return View("Sample42", null, result);
                    }
                }
            }
            // If data not posted return to template for filling of necessary fields
            else
            {
                return View("Sample42");
            }
        }