public ActionResult Sample35() { // 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"]; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); String message = null; // Check is all needed fields are entered if (String.IsNullOrEmpty(clientId) || String.IsNullOrEmpty(privateKey)) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample35", null, result); } else { String basePath = Request.Form["basePath"]; //Check is base path entered if (basePath.Equals("")) { //If base path empty set base path to the dev server basePath = "https://api.groupdocs.com/v2.0"; } //Make propper URL for basePath if (!basePath.Substring(basePath.Length - 3, 3).Equals("2.0")) { if (!basePath.Substring(basePath.Length - 1, 1).Equals("/")) { basePath = basePath + "/v2.0"; } else { basePath = basePath + "v2.0"; } } result.Add("basePath", basePath); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); //Check which form is used (if Request.Form["guid"] that meens that this is second form with merge data) if (String.IsNullOrEmpty(Request.Form["guid"])) { //Get data from first form String fileId = Request.Form["guidField"]; String url = Request.Form["url"]; String fileGuId = ""; var file = Request.Files["file"]; //if URL to web file was provided - upload the file from Web and get it's GUID if (url != "") { //Make request to upload file from entered Url String guid = service.UploadUrl(url); if (guid != null) { //If file uploaded return his GuId fileGuId = guid; } //If file wasn't uploaded return error else { result.Add("error", "Something wrong with entered data"); return View("Sample35", null, result); } } //if file was uploaded locally - upload the file and get it's GUID if (file.FileName != "") { //Upload local file Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, "uploaded", file.InputStream); if (upload.Guid != null) { //If file uploaded return his guid fileGuId = upload.Guid; } else { //If file wasn't uploaded return error result.Add("error", "Something wrong with entered data"); return View("Sample35", null, result); } } //If user choose file guid if (!fileId.Equals("")) { //Set file guid as entered by user file guid fileGuId = fileId; } //Get all fields from selected document Groupdocs.Assembly.Contract.TemplateFieldsResult getFields = service.GetTemplateFields(fileGuId); if (getFields.Fields.Length != 0) { //Create support variables and counters String element = ""; Int32 countRadio = 0; Int32 countCheckBox = 0; String fieldName = ""; Groupdocs.Assembly.Data.TemplateField[] fields = getFields.Fields; //Create second form for entering data for merge for (Int32 i = 0; i < fields.Length; i++) { fieldName = fields[i].Name; if (fields[i].Type == "Text") { element += "<br /><label for=\"" + fieldName + "\">" + fields[i].Name + "</label>" + "<br /><input type=\"text\" name=\"" + fieldName + "\" value=\"\" /><br />"; } else if (fields[i].Type == "Checkbox") { element += "<br /><input type=\"checkbox\" name=\"" + fieldName + "\" value=\"" + countCheckBox + "\" >" + fields[i].Name + "</input><br />"; countCheckBox = countCheckBox + 1; } else if (fields[i].Type == "RadioButton") { element += "<br /><input type=\"radio\" name=\"" + fieldName + "\" value=\"" + countRadio + "\" >" + fields[i].Name + "</input><br />"; countRadio = countRadio + 1; } else if (fields[i].Type == "MultiLineText") { element += "<br /><label for=\"" + fieldName + "\">" + fields[i].Name + "</label>" + "<br /><input type=\"textarea\" name=\"" + fieldName + "\" value=\"\" >" + fields[i].Name + "</input><br />"; countCheckBox = countCheckBox + 1; } } result.Add("newForm", element); result.Add("fileId", fileGuId); return View("Sample35", null, result); } else { //If get fields failed result.Add("error", "Something wrong with get fields from template"); return View("Sample35", null, result); } } //If second form with merge data else { //Get file GUID from hidden input String fileGuId = Request.Form["guid"]; string iframe = ""; //Create empty Value collection System.Collections.Specialized.NameValueCollection postData = new System.Collections.Specialized.NameValueCollection(); //Get all POST data postData = Request.Form; //Create empty HashTable System.Collections.Hashtable enteredData = new System.Collections.Hashtable(); //Select data from POST foreach (String elementName in postData) { if (elementName == "clientId") { clientId = postData[elementName]; } else if (elementName == "privateKey") { privateKey = postData[elementName]; } else if (elementName == "basePath") { basePath = postData[elementName]; } else if (elementName == "guid") { fileGuId = postData[elementName]; } else { enteredData.Add(elementName, postData[elementName]); } } //Get all fields from document Groupdocs.Assembly.Contract.TemplateFieldsResult templateFields = service.GetTemplateFields(fileGuId); if (templateFields.Fields.Length != 0) { //Set fileds counter Int32 count = enteredData.Count; //Create Datasource object Groupdocs.Assembly.Data.Datasource dataSource = new Groupdocs.Assembly.Data.Datasource(); //Create array of DatasourceField objects Groupdocs.Assembly.Data.DatasourceField[] fieldArray = new Groupdocs.Assembly.Data.DatasourceField[count]; Int32 counter = 0; //Create DataSourceFieldType object Groupdocs.Assembly.Data.DatasourceFieldType type = new Groupdocs.Assembly.Data.DatasourceFieldType(); //Create counters for RadioButtons and CheckBoxes Int32 counteRadio = -1; Int32 counteCheckBox = -1; //Create DataSourceField for (Int32 n = 0; n < templateFields.Fields.Length; n++) { foreach (System.Collections.DictionaryEntry fields in enteredData) { string[] values = new string[1]; //Check is field name from document equal to field name from merge data if (fields.Key.ToString().Equals(templateFields.Fields[n].Name)) { //Set field type and value if (templateFields.Fields[n].Type.Equals("RadioButton")) { counteRadio = counteRadio + 1; Int32 value; int.TryParse(fields.Value.ToString(), out value); if (counteRadio == value) { values.SetValue(System.Convert.ToString(fields.Value), 0); type = Groupdocs.Assembly.Data.DatasourceFieldType.Text; } else { continue; } } else if (templateFields.Fields[n].Type.Equals("MultiLineText")) { //Set object array content values.SetValue(System.Convert.ToString(fields.Value), 0); type = Groupdocs.Assembly.Data.DatasourceFieldType.Text; } else if (templateFields.Fields[n].Type.Equals("Checkbox")) { counteCheckBox = counteCheckBox + 1; Int32 value; int.TryParse(fields.Value.ToString(), out value); if (counteCheckBox == value) { values.SetValue(System.Convert.ToString("true"), 0); type = Groupdocs.Assembly.Data.DatasourceFieldType.Text; } else { continue; } } else { values.SetValue(System.Convert.ToString(fields.Value), 0); type = Groupdocs.Assembly.Data.DatasourceFieldType.Text; } //Add merge data to field object Groupdocs.Assembly.Data.DatasourceField field = new Groupdocs.Assembly.Data.DatasourceField(); field.Name = System.Convert.ToString(fields.Key); field.Type = type; field.Values = values; //Push DatasourceField to array of DatasourceField objects fieldArray.SetValue(field, counter); counter++; } } } //Set feilds array to the Datasource dataSource.Fields = fieldArray; //Add new Datasource to GroupDocs decimal addDataSource = service.AddDataSource(dataSource); //Check is not empty addDataSource if (!addDataSource.Equals("")) { //If status ok merge Datasource to new pdf file decimal job = service.MergeTemplate(fileGuId, addDataSource, "pdf", false); if (!job.Equals("")) { Groupdocs.Api.Contract.GetJobDocumentsResult jobInfo = new Groupdocs.Api.Contract.GetJobDocumentsResult(); //### Check job status for (int n = 0; n <= 5; n++) { //Delay necessary that the inquiry would manage to be processed System.Threading.Thread.Sleep(5000); //Make request to api for get document info by job id jobInfo = service.GetJobDocuments(job); //Check job status, if status is Completed or Archived exit from cycle if (jobInfo.JobStatus.ToString().Equals("Completed") || jobInfo.JobStatus.ToString().Equals("Archived")) { break; } else if (jobInfo.JobStatus.ToString().Equals("Postponed")) { result.Add("error", "Job is fail"); return View("Sample35", null, result); } } //Get guid String guid = jobInfo.Inputs[0].Outputs[0].Guid; // 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.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); // Return to the template return View("Sample35", null, result); } else { result.Add("error", "MargeTemplate is fail"); return View("Sample35", null, result); } } else { result.Add("error", "AddDataSource returned empty job id"); return View("Sample35", null, result); } } else { result.Add("error", "Get template fields is failed"); return View("Sample35", null, result); } } } } // If data not posted return to template for filling of necessary fields else { return View("Sample35"); } }
public ActionResult Sample25() { // 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"]; String fileGuId = ""; String iframe = ""; var file = Request.Files["file"]; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); String message = null; // Check is all needed fields are entered if (String.IsNullOrEmpty(clientId) || String.IsNullOrEmpty(privateKey)) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample25", null, result); } else { String basePath = Request.Form["basePath"]; //Check is base path entered if (basePath.Equals("")) { //If base path empty set base path to the dev server basePath = "https://api.groupdocs.com/v2.0"; } result.Add("basePath", basePath); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); //Make request to get document pages as images //if URL to web file was provided - upload the file from Web and get it's GUID if (url != "") { //Make request to upload file from entered Url String guid = service.UploadUrl(url); if (guid != null) { //If file uploaded return his GuId fileGuId = guid; } //If file wasn't uploaded return error else { result.Add("error", "Something wrong with entered data"); return View("Sample25", null, result); } } //if file was uploaded locally - upload the file and get it's GUID if (file.FileName != "") { //Upload local file Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, "uploaded", file.InputStream); if (upload.Guid != null) { //If file uploaded return his guid fileGuId = upload.Guid; } else { //If file wasn't uploaded return error result.Add("error", "Something wrong with entered data"); return View("Sample25", null, result); } } //If user choose file guid if (!fileId.Equals("")) { //Set file guid as entered by user file guid fileGuId = fileId; } //Get all fields from template Groupdocs.Assembly.Contract.TemplateFieldsResult fields = service.GetTemplateFields(fileGuId); if (fields.Fields.Length > 0) { //Set fileds counter Int32 count = fields.Fields.Length; //Create Datasource object Groupdocs.Assembly.Data.Datasource dataSource = new Groupdocs.Assembly.Data.Datasource(); //Create array of DatasourceField objects Groupdocs.Assembly.Data.DatasourceField[] fieldArray = new Groupdocs.Assembly.Data.DatasourceField[count]; //Create array od objects with string[] values = new string[2]; //Set object array content values.SetValue("value1", 0); values.SetValue("value2", 1); //Set DatasourceFiled data for (int i = 0; i < count; i++) { Groupdocs.Assembly.Data.DatasourceField field = new Groupdocs.Assembly.Data.DatasourceField(); Groupdocs.Assembly.Data.DatasourceFieldType type = new Groupdocs.Assembly.Data.DatasourceFieldType(); type = 0; field.Name = fields.Fields[i].Name; field.Type = type; field.Values = values; //Push DatasourceField to array of DatasourceField objects fieldArray.SetValue(field, i); } //Set feilds array to the Datasource dataSource.Fields = fieldArray; //Add new Datasource to GroupDocs decimal addDataSource = service.AddDataSource(dataSource); //Check is not empty addDataSource if (!addDataSource.Equals("")) { //If status ok merge Datasource to new pdf file decimal job = service.MergeTemplate(fileGuId, addDataSource, "pdf", false); if (!job.Equals("")) { Groupdocs.Api.Contract.GetJobDocumentsResult jobInfo = new Groupdocs.Api.Contract.GetJobDocumentsResult(); //### Check job status for (int n = 0; n <= 5; n++) { //Delay necessary that the inquiry would manage to be processed System.Threading.Thread.Sleep(5000); //Make request to api for get document info by job id jobInfo = service.GetJobDocuments(job); //Check job status, if status is Completed or Archived exit from cycle if (jobInfo.JobStatus.Equals("Completed") || jobInfo.JobStatus.Equals("Archived")) { break; //If job status Postponed throw exception with error } else if (jobInfo.JobStatus.Equals("Postponed")) { result.Add("error", "Job is fail"); return View("Sample25", null, result); } } //Get guid String guid = jobInfo.Inputs[0].Outputs[0].Guid; //Get name String name = jobInfo.Inputs[0].Outputs[0].Name; // Definition of folder where to download file String LocalPath = AppDomain.CurrentDomain.BaseDirectory + "downloads/"; if (!Directory.Exists(LocalPath)) { DirectoryInfo di = Directory.CreateDirectory(LocalPath); } //### Make a request to Storage Api for dowloading file // Download file bool download = service.DownloadFile(guid, LocalPath + name); // If file downloaded successful if (download != false) { // 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-api-groupdocs.dynabic.com/document-viewer/embed/" + guid; } if (basePath.Equals("https://realtime-api.groupdocs.com/v2.0")) { iframe = "https://realtime-apps.groupdocs.com/document-viewer/embed/" + guid; } iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); result.Add("iframe", iframe); // Put file info to the result's list result.Add("message", "File was converted and downloaded to the " + LocalPath + "/" + name); result.Add("fileId", guid); // Return to the template return View("Sample25", null, result); } } else { result.Add("error", "MargeTemplate is fail"); return View("Sample25", null, result); } } else { result.Add("error", "AddDataSource returned empty job id"); return View("Sample25", null, result); } } return View("Sample25", null, result); } } // If data not posted return to template for filling of necessary fields else { return View("Sample25"); } }