Save original document at the server side

21 Dec 20222 minutes to read

The PDF Viewer server library allows to save original PDF document pages at the server side. The following steps are used to save original PDF document at the server side using button click event.

The following steps are used to save original document at the server side.

Step 1: Follow the steps provided in the link to create simple PDF Viewer sample.

Step 2: Use the following code snippet to create a button click event in PDF Viewer Control.

<button onclick="save()">SaveDocument</button>
<script>
    function save() {
        var pdfViewer = document.getElementById('pdfviewer1').ej2_instances[0];
        pdfViewer.serverActionSettings.download = "SaveDocument";
        pdfViewer.download();
        pdfViewer.serverActionSettings.download = "Download";
    }
</script>

Step 3: Add the following code snippet in PDFViewerController.cs to save original PDF document at the server side.

public ActionResult SaveDocument([FromBody] Dictionary<string, string> jsonObject)
{
    PdfRenderer pdfviewer = new PdfRenderer(_cache);
    string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
    string base64String = documentBase.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1];
    if (base64String != null || base64String != string.Empty)
    {
        byte[] byteArray = Convert.FromBase64String(base64String);

        MemoryStream ms = new MemoryStream(byteArray);
        var path = _hostingEnvironment.ContentRootPath;
        System.IO.File.WriteAllBytes(path + "/ouptut.pdf", byteArray);
    }
    return Content(string.Empty);
}

Download the sample how to save original document at the server side

NOTE

Ensure the provided document path and output saved locations in your application level.