Пример #1
0
        public ActionResult Sample11()
        {
            // 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 basePath = Request.Form["basePath"];
                String fileId = Request.Form["fileId"];
                String url = Request.Form["url"];
                var file = Request.Files["file"];
                String fileGuId = "";
                String annotationType = Request.Form["annotationType"];
                String boxX = Request.Form["boxX"];
                String boxY = Request.Form["boxY"];
                String boxWidth = Request.Form["boxWidth"];
                String boxHeight = Request.Form["boxHeight"];
                String annotationPositionX = Request.Form["annotationPositionX"];
                String annotationPositionY = Request.Form["annotationPositionY"];
                String rangePosition = Request.Form["rangePosition"];
                String rangeLength = Request.Form["rangeLength"];
                String text = Request.Form["text"];
                String iframe = "";

                // Set entered data to the results list
                result.Add("clientId", clientId);
                result.Add("privateKey", privateKey);
                result.Add("fileId", fileId);
                result.Add("type", annotationType);
                String message = null;
                // Check is all needed fields are entered
                if (clientId == null || privateKey == null || annotationType == null)
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample11", null, result);
                }
                else
                {

                    if (basePath == "")
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    result.Add("basePath", basePath);
                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);
                    //Check is chosen local file
                    if (!file.ContentLength.Equals(0))
                    {
                        // Upload file with empty description.
                        Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream);
                        // Check is upload successful
                        if (upload.Guid != null)
                        {
                            // Put uploaded file GuId to the result's list
                            fileGuId = upload.Guid;

                        }
                        // If upload was failed return error
                        else
                        {
                            message = "UploadFile returns error";
                            result.Add("error", message);
                            return View("Sample11", null, result);
                        }

                    }
                    //Check is url entered
                    if (!url.Equals(""))
                    {
                        //Make request to upload file from entered Url
                        String guid = service.UploadUrl(url);
                        if (guid != null)
                        {
                            //Get all files from GroupDocs account
                            Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("My Web Documents", 0, -1, null, false, null, null, true);
                            if (storageInfo.Files.Length > 0)
                            {
                                // Get file id by uploaded file GuId
                                for (int i = 0; i < storageInfo.Files.Length; i++)
                                {
                                    if (storageInfo.Files[i].Guid == guid)
                                    {
                                        fileGuId = storageInfo.Files[i].Guid;

                                    }

                                }
                            }
                            else
                            {
                                message = "Get files list is failed";
                                result.Add("error", message);
                                return View("Sample11", null, result);
                            }
                        }
                        //If file wasn't uploaded return error
                        else
                        {
                            result.Add("error", "Something wrong with entered data");
                            return View("Sample11", null, result);
                        }
                    }
                    //Check is file guid entered
                    if (!fileId.Equals(""))
                    {
                        //Get all files from GroupDocs account
                        Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("", 0, -1, null, false, null, null, true);
                        if (storageInfo.Files.Length > 0)
                        {
                            // Get file id and name by entered file GuId
                            for (int i = 0; i < storageInfo.Files.Length; i++)
                            {
                                if (storageInfo.Files[i].Guid == fileId)
                                {
                                    fileGuId = storageInfo.Files[i].Guid;

                                }

                            }
                        }
                        else
                        {
                            message = "Get files list is failed";
                            result.Add("error", message);
                            return View("Sample11", null, result);
                        }
                    }
                    //###Create AnnotationType
                    Groupdocs.Common.AnnotationType type = new Groupdocs.Common.AnnotationType();
                    //Create Rectangle object
                    Groupdocs.Api.Contract.Rectangle rectangle = new Groupdocs.Api.Contract.Rectangle();
                    //Create Point object
                    Groupdocs.Api.Contract.Data.Point point = new Groupdocs.Api.Contract.Data.Point();
                    //Create Range object
                    Groupdocs.Api.Contract.Range range = new Groupdocs.Api.Contract.Range();
                    //Set annotation parameters if annotation type is point
                    if (annotationType == "point")
                    {
                        type = Groupdocs.Common.AnnotationType.Point;
                        rectangle.X = float.Parse(boxX);
                        rectangle.Y = float.Parse(boxY);
                        rectangle.Width = 0;
                        rectangle.Height = 0;
                        point.X = 0;
                        point.Y = 0;

                    }
                    //Set annotation parameters if annotation type is text
                    else if (annotationType == "text")
                    {
                        type = Groupdocs.Common.AnnotationType.Text;
                        point.X = double.Parse(annotationPositionX);
                        point.Y = double.Parse(annotationPositionY);
                        range.Length = Int32.Parse(rangeLength);
                        range.Position = Int32.Parse(rangePosition);
                    }
                    //Set annotation parameters if annotation type is area
                    else
                    {
                        type = Groupdocs.Common.AnnotationType.Area;
                        point.X = 0;
                        point.Y = 0;
                        rectangle.X = float.Parse(boxX);
                        rectangle.Y = float.Parse(boxY);
                        rectangle.Width = float.Parse(boxWidth);
                        rectangle.Height = float.Parse(boxHeight);
                    }
                    //### Make request to Api to create Annotation
                    Groupdocs.Api.Contract.Annotation.CreateAnnotationResult annotation = service.CreateAnnotation(fileGuId, type, rectangle, point, range, null, null);
                    //Check if GuId of document with added annotation is not empty
                    if (annotation.DocumentGuid != "")
                    {
                        // Generate Embed annotation url with entered file id
                        if (basePath.Equals("https://api.groupdocs.com/v2.0"))
                        {
                            iframe = "https://apps.groupdocs.com/document-annotation2/embed/" + annotation.DocumentGuid;
                        }
                        if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0"))
                        {
                            iframe = "https://dev-apps-groupdocs.dynabic.com/document-annotation2/embed/" + annotation.DocumentGuid;
                        }
                        if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0"))
                        {
                            iframe = "https://stage-api-groupdocs.dynabic.com/document-annotation2/embed/" + annotation.DocumentGuid;
                        }
                        if (basePath.Equals("https://realtime-api.groupdocs.com/v2.0"))
                        {
                            iframe = "https://realtime-apps.groupdocs.com/document-annotation2/embed/" + annotation.DocumentGuid;
                        }
                        //Set data for template
                        iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey);
                        result.Add("iframe", iframe);
                        result.Add("guid", annotation.DocumentGuid);
                        return View("Sample11", null, result);
                    }
                    //If GuId is empty return error
                    else
                    {
                        result.Add("error", "Annotation is fail");
                        return View("Sample11", null, result);
                    }

                }

            }
            // If data not posted return to template for filling of necessary fields
            else
            {
                return View("Sample11");
            }
        }