Add custom data to the load function (pdfViewer)

Hello,

I use the EJS component to edit my pdf files. I would like add custom data in the front call and get it in the Dictionary<string, string> jsonObject like the documentPath contained in the "document" entry.

To call the load, i use the DocumentPath in the declaration of ejs in html (and open document is hided) :


<ejs-pdfviewer id="pdfviewer" style="height:1200px" serviceUrl="/api/Signature" zoomValue="100" documentPath="@Model.Filename"></ejs-pdfviewer>


Thanks


3 Replies

SK Shamini Kiruba Sobers Syncfusion Team March 11, 2022 11:31 AM UTC

Hi LAURENT, 

You can add the custom data to the jsonObject in the Load method using the AjaxRequestInitiate event. Please refer to the below code snippet and the sample. 

Code snippet: 

Client-side: 

<div style="width:100%;height:1200px"> 
    <ejs-pdfviewer id="pdfviewer1" serviceUrl="/api/PdfViewer" documentPath="HTTP Succinctly.pdf" ajaxRequestInitiate ="ajaxRequestInitiate"></ejs-pdfviewer> 
 
</div> 
 
<script> 
    function ajaxRequestInitiate(args) { 
        var viewer = document.getElementById("pdfviewer1").ej2_instances[0]; 
        if (viewer.serverActionSettings.load == "Load") { 
            args.JsonData['documentId'] = '1001'; 
            viewer.setJsonData(args.JsonData); 
        } 
 
    } 
</script> 

Server-side: 

[AcceptVerbs("Post")] 
[HttpPost] 
[Route("api/[controller]/Load")] 
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject) 
{ 
    Console.WriteLine("Load called"); 
    //Initialize the PDF viewer object with memory cache object 
    PdfRenderer pdfviewer = new PdfRenderer(_cache); 
    MemoryStream stream = new MemoryStream(); 
    object jsonResult = new object(); 
    string id = jsonObject["documentId"]; 
    if (jsonObject != null && jsonObject.ContainsKey("document")) 
    { 
        if (bool.Parse(jsonObject["isFileName"])) 
        { 
            string documentPath = GetDocumentPath(jsonObject["document"]); 
            if (!string.IsNullOrEmpty(documentPath)) 
            { 
                byte[] bytes = System.IO.File.ReadAllBytes(documentPath); 
                stream = new MemoryStream(bytes); 
            } 
            else 
            { 
                return this.Content(jsonObject["document"] + " is not found"); 
            } 
        } 
        else 
        { 
            byte[] bytes = Convert.FromBase64String(jsonObject["document"]); 
            stream = new MemoryStream(bytes); 
        } 
    } 
    jsonResult = pdfviewer.Load(stream, jsonObject); 
    return Content(JsonConvert.SerializeObject(jsonResult)); 
} 


Kindly try this and revert to us if you have any concerns. 

Regards, 
Shamini 



AS Alex Santos January 24, 2025 02:14 AM UTC

Thank you!



SK Sathiyaseelan Kannigaraj Syncfusion Team January 24, 2025 05:15 AM UTC

Hi Alex Santos,

You're welcome. If you need any further assistance, please don't hesitate to reach out to us.


Regards,
Sathiyaseelan K


Loader.
Up arrow icon