We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to save and retrieve the annotations from the database?

Platform: ASP.NET MVC |
Control: PdfViewer |
Published Date: August 1, 2018 |
Last Revised Date: April 9, 2019

Save and retrieve TextMarkup annotations from database

You can save and retrieve the TextMarkup annotations separately from the PDF viewer as a workaround. Refer to the following code,

HTML

<div style="float:right; height:780px;width:30% ; border: solid black 1px; top:3px;" id="Annotations">
        <h2> Annotations List </h2>
    </div>
    <div class="content-container-fluid">
        <div style="width:69.5%;height:780px;">
            <button id="button1" type="button" onclick="annotationsOperation(event)">Save Annotations</button>
            <button id="button2" type="button" onclick="showAnnotation(event)">Show Annotations</button>
            @(Html.EJ().PdfViewer("pdfviewer").ServiceUrl("../api/PdfViewer"))
        </div>       
        </div>    
 

JavaScript

function showAnnotation(event)
            {
                $.ajax({
                    type: 'POST',
                    url: '../api/PdfViewer/ReadID',
                    crossDomain: true,
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: null,
                    traditional: true,
                    success: function (data) {
                        var annotations = data;
                        var annotdiv = document.getElementById('Annotations');
                        var annotlist = document.getElementById('Annotationlist');
                        if (annotlist != null)
                        {
                            annotlist.remove();
                        }
                        var ul = document.createElement('ul');
                        ul.id = "Annotationlist";
                        for (var a = 0; a < annotations.length ; a++) {
                            var li = document.createElement('li');
                            li.innerHTML = annotations[a];
                            li.onclick = function (event) {
                                var id = event.currentTarget.innerHTML;
                                var jsonResult = new Object();
                                var jsonObject = { Id: event.currentTarget.innerHTML };
                                $.ajax({
                                    type: 'POST',
                                    url: '../api/PdfViewer/RetriveAnnotation',
                                    crossDomain: true,
                                    contentType: 'application/json; charset=utf-8',
                                    dataType: 'json',
                                    data: JSON.stringify(jsonObject),
                                    traditional: true,
                                    success: function (data) {
                                        var obj = $("#pdfviewer").data("ejPdfViewer");
                                        var base64 = "data:application/pdf;base64," + data["documentStream"];
                                      obj.load(base64);
                                    }
                                });                              
                            };
                            ul.appendChild(li);
                        }
                        annotdiv.appendChild(ul);
                    }
                });                       
            };
 
            function annotationsOperation(event) {
                var pdfviewer = $("#pdfviewer").data("ejPdfViewer");
                pdfviewer.model.serverActionSettings.download = "DocumentRetrieve";
                pdfviewer.download(false);
                pdfviewer.model.serverActionSettings.download = "Download";
                alert("Annotations saved in Database");     
            }
        

Sample : http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample465159687

In the above sample, a button Save Annotations is provided for saving the annotations included in the PDF viewer control to the database table. The annotations which are added, modified and deleted will be added to the database when the Save Annotations button is clicked.  

Another button Show Annotations is provided for showing the saved annotations as html list, so that user can click on the annotation and the saved annotation will be retrieved from the database and included in the PDF document and the PDF viewer control is launched again. 

Note:

 During this operation, the annotations that are added as single annotation will be saved as multi-line markup annotation and on clicking the html list only that part of the annotation will be rendered in PDF Viewer. Because, at present we do not support to add text markup annotations across multiple lines as single annotation.Modify the web action methods in the API controller as per your entries in database. We have three entries ID, PageNumber and Annotation to store ID of the PDF document and the serialized string with annotation. Also, modify the connection string of the database in the web.config file.  

Refer here to explore the rich set of PDF Viewer features. 

An online demo link to view PDF documents using Syncfusion PDF Viewer.

 

 

2X faster development

The ultimate ASP.NET MVC UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile