|
| 1 | +using GroupDocs.Annotation.Domain; |
| 2 | +using GroupDocs.Annotation.Domain.Results; |
| 3 | +using GroupDocs.Annotation.Handler; |
| 4 | +using GroupDocs.Annotation.Handler.Input; |
| 5 | +using GroupDocs.Annotation.Handler.Input.DataObjects; |
| 6 | +using GroupDocs.Annotation_for.NET.Models; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Serialization; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.IO; |
| 12 | +using System.Linq; |
| 13 | +using System.Web; |
| 14 | +using System.Web.Mvc; |
| 15 | + |
| 16 | +namespace GroupDocs.Annotation_for.NET.Controllers |
| 17 | +{ |
| 18 | + [RoutePrefix("annotation")] |
| 19 | + public class AnnotationController : Controller |
| 20 | + { |
| 21 | + [HttpGet] |
| 22 | + [Route("")] |
| 23 | + public ActionResult Get(string guid) |
| 24 | + { |
| 25 | + Response.AddHeader("Content-Type", "application/json"); |
| 26 | + |
| 27 | + AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler(); |
| 28 | + return Content(JsonConvert.SerializeObject( |
| 29 | + imageHandler.GetAnnotation(guid).Annotation, |
| 30 | + Formatting.Indented, |
| 31 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 32 | + ), "application/json"); |
| 33 | + } |
| 34 | + [HttpDelete] |
| 35 | + public ActionResult Delete(string guid) |
| 36 | + { |
| 37 | + Response.AddHeader("Content-Type", "application/json"); |
| 38 | + AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler(); |
| 39 | + long annotationId = imageHandler.GetAnnotation(guid).Id; |
| 40 | + DeleteAnnotationResult result = imageHandler.DeleteAnnotation(annotationId); |
| 41 | + return Content(JsonConvert.SerializeObject( |
| 42 | + result, |
| 43 | + Formatting.Indented, |
| 44 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 45 | + ), "application/json"); |
| 46 | + |
| 47 | + |
| 48 | + } |
| 49 | + [HttpPost] |
| 50 | + public ActionResult Post() |
| 51 | + { |
| 52 | + Response.AddHeader("Content-Type", "application/json"); |
| 53 | + AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler(); |
| 54 | + String guid = Request.Params["guid"]; |
| 55 | + String section = Request.Params["guid"]; |
| 56 | + AnnotationInfo annotationInfo = imageHandler.GetAnnotation(guid).Annotation; |
| 57 | + long annotationId = imageHandler.GetAnnotation(guid).Id; |
| 58 | + |
| 59 | + switch (section) |
| 60 | + { |
| 61 | + case "fieldtext": |
| 62 | + var jsonString = String.Empty; |
| 63 | + using (var inputStream = new StreamReader(Request.InputStream)) |
| 64 | + { |
| 65 | + jsonString = inputStream.ReadToEnd(); |
| 66 | + } |
| 67 | + TextFieldInfo info = JsonConvert.DeserializeObject<TextFieldInfo>(jsonString); |
| 68 | + |
| 69 | + SaveAnnotationTextResult result = imageHandler.SaveTextField(annotationId, info); |
| 70 | + return Content(JsonConvert.SerializeObject( |
| 71 | + result, |
| 72 | + Formatting.Indented, |
| 73 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 74 | + ), "application/json"); |
| 75 | + case "position": |
| 76 | + var jsonStringPos = String.Empty; |
| 77 | + using (var inputStream = new StreamReader(Request.InputStream)) |
| 78 | + { |
| 79 | + jsonStringPos = inputStream.ReadToEnd(); |
| 80 | + } |
| 81 | + Point point = JsonConvert.DeserializeObject<Point>(jsonStringPos); |
| 82 | + |
| 83 | + MoveAnnotationResult moveresult = imageHandler.MoveAnnotationMarker(annotationId, point, annotationInfo.PageNumber); |
| 84 | + return Content(JsonConvert.SerializeObject( |
| 85 | + moveresult, |
| 86 | + Formatting.Indented, |
| 87 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 88 | + ), "application/json"); |
| 89 | + default: |
| 90 | + return Content(null); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + public ActionResult Index(string guid) |
| 97 | + { |
| 98 | + return View(); |
| 99 | + } |
| 100 | + // GET: Annotation |
| 101 | + public ActionResult List(string file) |
| 102 | + { |
| 103 | + Response.AddHeader("Content-Type", "application/json"); |
| 104 | + AnnotationImageHandler handler = Utils.createAnnotationImageHandler(); |
| 105 | + String filename = file; |
| 106 | + Document doc = Utils.findDocumentByName(filename); |
| 107 | + ListAnnotationsResult listResult = handler.GetAnnotations(doc.Id); |
| 108 | + |
| 109 | + List<GetAnnotationResult> list = new List<GetAnnotationResult>(); |
| 110 | + foreach (AnnotationInfo inf in listResult.Annotations) |
| 111 | + { |
| 112 | + list.Add(handler.GetAnnotation(inf.Guid)); |
| 113 | + } |
| 114 | + return Content(JsonConvert.SerializeObject( |
| 115 | + list, |
| 116 | + Formatting.Indented, |
| 117 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 118 | + ), "application/json"); |
| 119 | + |
| 120 | + |
| 121 | + //Response.Write(list); |
| 122 | + /* |
| 123 | + * |
| 124 | + * response.setHeader("Content-Type", "application/json"); |
| 125 | + AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler(); |
| 126 | + String filename = request.getParameter("file"); |
| 127 | +
|
| 128 | + Document doc = Utils.findDocumentByName(filename); |
| 129 | + ListAnnotationsResult listResult = imageHandler.getAnnotations(doc.getId()); |
| 130 | +
|
| 131 | + ArrayList<GetAnnotationResult> list = new ArrayList<>(); |
| 132 | + for (AnnotationInfo inf : listResult.getAnnotations()) { |
| 133 | + list.add(imageHandler.getAnnotation(inf.getGuid())); |
| 134 | + } |
| 135 | +
|
| 136 | + new ObjectMapper().writeValue(response.getOutputStream(), list);*/ |
| 137 | + |
| 138 | + |
| 139 | + return View(); |
| 140 | + } |
| 141 | + public ActionResult Add2(string file) |
| 142 | + { |
| 143 | + Response.AddHeader("Content-Type", "application/json"); |
| 144 | + AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler(); |
| 145 | + IDocumentDataHandler documentDataHandler = imageHandler.GetDocumentDataHandler(); |
| 146 | + |
| 147 | + String filename = file; |
| 148 | + Document doc = documentDataHandler.GetDocument(filename); |
| 149 | + long documentId = doc != null ? doc.Id : imageHandler.CreateDocument(filename); |
| 150 | + |
| 151 | + //StreamReader stream = new StreamReader(Request.InputStream); |
| 152 | + //string x = stream.ReadToEnd(); // added to view content of input stream |
| 153 | + |
| 154 | + AnnotationInfo annotation = new AnnotationInfo(); //Request.InputStream as AnnotationInfo; |
| 155 | + annotation.DocumentGuid = documentId; |
| 156 | + CreateAnnotationResult result = imageHandler.CreateAnnotation(annotation); |
| 157 | + return Content(JsonConvert.SerializeObject( |
| 158 | + result, |
| 159 | + Formatting.Indented, |
| 160 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } |
| 161 | + ), "application/json"); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments