public Response SetAnnotations(SetAnnotationsRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            return(AnnotationMethods.SetAnnotations(request));
        }
        public static Response SetAnnotations(SetAnnotationsRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (string.IsNullOrEmpty(request.DocumentId))
            {
                throw new ArgumentException("documentId must not be null");
            }

            if (request.PageNumber < 0)
            {
                throw new ArgumentException("'pageNumber' must be a value greater than or equal to 0");
            }

            // If pageNumber 0, set for all pages

            var annCodec = new AnnCodecs();

            AnnContainer[] containers = null;

            if (!string.IsNullOrEmpty(request.Annotations))
            {
                containers = annCodec.LoadAllFromString(request.Annotations);
            }

            // Now load the document
            var cache = ServiceHelper.Cache;

            using (var document = DocumentFactory.LoadFromCache(cache, request.DocumentId))
            {
                DocumentHelper.CheckLoadFromCache(document);

                // If the document is read-only then below will fail. So, temporarily reset the value
                var wasReadOnly = document.IsReadOnly;
                document.IsReadOnly = false;

                if (request.PageNumber == 0)
                {
                    // Set all
                    document.Annotations.SetAnnotations(containers);
                }
                else
                {
                    DocumentHelper.CheckPageNumber(document, request.PageNumber);

                    var          documentPage = document.Pages[request.PageNumber - 1];
                    AnnContainer container    = null;
                    if (containers != null)
                    {
                        if (containers.Length == 1)
                        {
                            container = containers[0];
                        }
                        else
                        {
                            for (var i = 0; i < containers.Length && container == null; i++)
                            {
                                if (containers[i].PageNumber == request.PageNumber)
                                {
                                    container = containers[i];
                                }
                            }
                        }
                    }

                    documentPage.SetAnnotations(container);
                }

                // reset the read-only value before saving into the cache
                document.IsReadOnly = wasReadOnly;
                document.SaveToCache();
            }
            return(new Response());
        }