How to Load and Save PDF using PdfViewer?

How to load the PDF AcroForm in Asp.net using PdfViewer and saving form values after editing on server side?

Regards.
Max

9 Replies

SA Sabari Anand Senthamarai Kannan Syncfusion Team November 27, 2017 06:08 AM UTC

Hi, 

Thank you for using Syncfusion products. 

We can load the PDF documents with AcroForms in the PDF viewer control using the Load web action method in the web API controller(server side). Please find the UG documentation for the Load action method below for further details. 


We can use load() API of the PDF viewer control to load the PDF document from the client side.    

We can save the PDF document on the server side by modifying the Download action method of the web API controller. We can retrieve the edited PDF document as a stream from the PDF viewer control and save the same to a specific location on a local disk. 

We have created a sample to demonstrate the same. Please find the sample from the following link. 


Please let us know that the above sample meets your requirements. If not, please let us know your requirements. It will be helpful for us to analyze further and assist you better. 

Regards, 
Sabari Anand 



VM vmax December 2, 2017 05:40 AM UTC

Hi Sabari Anand Senthamarai Kannan,

Thank you very much for your valuable assistance, it runs perfectly. 

Regards.
Vmax 


SA Sabari Anand Senthamarai Kannan Syncfusion Team December 4, 2017 06:05 AM UTC

Hi, 

Thank you for your update. 

We are happy to hear the sample application, we have provided, met your requirements. Please get back to us if you need any further assistance. 

Regards, 
Sabari Anand 



VM vmax January 25, 2018 01:43 PM UTC

Dear abari Anand,

Could you check this exception? 

Inside Webapi folder

PdfViewerController.cs ]

using Newtonsoft.Json;
using Syncfusion.EJ;
using Syncfusion.EJ.PdfViewer;
using Syncfusion.Pdf.Parsing;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;


namespace hive
{
    public class PdfViewerController : ApiController
    {
        PdfViewerHelper helper = new PdfViewerHelper();
        public object Load(Dictionary<string, string> jsonResult)
        {
            //load the multiple document from client side 
            if (jsonResult.ContainsKey("newFileName"))
            {
                var name = jsonResult["newFileName"];
                var pdfName = name.ToString();
                helper.Load(HttpContext.Current.Server.MapPath("~/Data/" + pdfName));
            }
            else
            {
                if (jsonResult.ContainsKey("isInitialLoading"))
                {
                    if (jsonResult.ContainsKey("file"))
                    {
                        var name = jsonResult["file"];
                        helper.Load(name);
                    }
                    else
                    {
                        helper.Load(HttpContext.Current.Server.MapPath("~/Data/theform.pdf"));
                    }
                }
            }
            string output = JsonConvert.SerializeObject(helper.ProcessPdf(jsonResult));
            return output;
        }
        public object FileUpload(Dictionary<string, string> jsonResult)
        {
            if (jsonResult.ContainsKey("uploadedFile"))
            {
                var fileurl = jsonResult["uploadedFile"];
                byte[] byteArray = Convert.FromBase64String(fileurl);
                MemoryStream stream = new MemoryStream(byteArray);
                helper.Load(stream);
            }
            string output = JsonConvert.SerializeObject(helper.ProcessPdf(jsonResult));
            return output;
        }
        public object Download(Dictionary<string, string> jsonResult)
        {
            var newdata = helper.GetDocumentData(jsonResult);
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(helper.DocumentStream);
            PdfLoadedForm loadedForm = loadedDocument.Form;
            if (loadedForm != null)
            {
                DirectoryInfo dtInfo = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath);
                if (!Directory.Exists(dtInfo + "/output"))
                    Directory.CreateDirectory(dtInfo + "/output");
                loadedDocument.Save(dtInfo + "/output/FormDocument.pdf");
                loadedDocument.Close(true);
            }
            return null;
        }
    }
}


[ WebFormView.aspx ]


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormView.aspx.cs" Inherits="hive.WebFormView" %>
<%@ Register Assembly="Syncfusion.EJ.Web" Namespace="Syncfusion.JavaScript.Web" TagPrefix="ej" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    
    <script src='<%= Page.ResolveClientUrl("~/Scripts/jquery-1.11.1.min.js")%>' type="text/javascript"></script>
    <script src='<%= Page.ResolveClientUrl("~/Scripts/jquery.easing-1.3.min.js")%>' type="text/javascript"></script>
    <script src='<%= Page.ResolveClientUrl("~/Scripts/ej/web/ej.web.all.min.js")%>' type="text/javascript"></script>
    <link rel='nofollow' href="Content/ej/flat-hive/ej.web.all.min.css" rel="stylesheet" type="text/css"/>
    
    <script src='<%= Page.ResolveClientUrl("~/Scripts/ej/common/jquery.globalize.min.js")%>' type="text/javascript"></script>
    <script src='<%= Page.ResolveClientUrl("~/Scripts/jsrender.min.js")%>' type="text/javascript"></script>
    <script src='<%= Page.ResolveClientUrl("~/Scripts/ej/common/ej.webform.min.js")%>' type="text/javascript"></script>



</head>
<body>
    <form id="form1" runat="server">
<div>
<ej:pdfviewer id="PdfViewer1" runat="server" serviceurl="../api/PdfViewer"></ej:pdfviewer>
</div>
  
    </form>
</body>
</html>


[ Global.asax.cs  ]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

using System.Web.Routing;
using System.Web.Security;

using System.Web.Http;
using System.Web.SessionState;

namespace hive
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            System.Web.Http.GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });
        }


        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }
    }
}



Attachment: ex_1c3759de.rar


AA Akshaya Arivoli Syncfusion Team January 26, 2018 05:20 PM UTC

Hi, 
 
Thank you for your update. 

We are unable to reproduce the reported exception with the details provided in the incident. However, on analyzing the details we found that you are using the older version of the ej.web.all.min.js in the sample. In the older version of Essential Studio we have used the PostViewerAction() in PdfViewerController.cs to load the PDF documents, but in the latest version of the PDF Viewer we are using the Load() API to load the PDF documents in the PDF Viewer, so kindly refer the correct version of wj.web.all.min.js in you project.  We have created the sample and shared the same in the following location, 


Please let us know that the above sample meets your requirements. If not, please provide us the Essential Studio Version and the sample and more details about your requirements. It will be helpful for us to analyze further and assist you better.  

Regards, 
Akshaya 



VM vmax replied to Akshaya Arivoli January 29, 2018 10:41 AM UTC

Hi, 
 
Thank you for your update. 

We are unable to reproduce the reported exception with the details provided in the incident. However, on analyzing the details we found that you are using the older version of the ej.web.all.min.js in the sample. In the older version of Essential Studio we have used the PostViewerAction() in PdfViewerController.cs to load the PDF documents, but in the latest version of the PDF Viewer we are using the Load() API to load the PDF documents in the PDF Viewer, so kindly refer the correct version of wj.web.all.min.js in you project.  We have created the sample and shared the same in the following location, 


Please let us know that the above sample meets your requirements. If not, please provide us the Essential Studio Version and the sample and more details about your requirements. It will be helpful for us to analyze further and assist you better.  

Regards, 
Akshaya 


Dear Akshaya ,

Thank you for your valuable response, But there is no luck with PdfViewer control version 15.4.0.17 to show PDF files in my solution, it's empty. also this warning appears in error list .. ( unrecognized tag prefix or device filter 'ej' ). 

attached my work on external link :  

Regards,
Max


SA Sabari Anand Senthamarai Kannan Syncfusion Team January 30, 2018 09:28 AM UTC

Hi, 

We have analyzed the provided sample application and suspect that the assemblies from the GAC are not referred properly to the sample application. Due to the assembly conflicts, the application thrown a runtime error. When we referred the assemblies manually to the application, the PDF viewer control is displayed without any error. Please find the video that we have recorded with the provided sample for your reference. 


Please ensure that the dependent assemblies for the PDF viewer control are referred properly in the application. Please find the UG documentation for managing the Syncfusion assemblies that are installed in the GAC from the following link. 


Also, please ensure all the dependent assemblies referred to the application are same version of Essential Studio.  

Please let us know if you need any further assistance. 

Regards, 
Sabari Anand 



TU Tusar April 29, 2020 08:02 AM UTC

Cannot able to register the PdfViewerController in the WebApi folder.The viewer is loading correctly but it is empty, since the webapi controller is not called it can't load the document.How to call the Webapi/PdfViewerController?

I am using the lkatest version of 18.1451.0.42


AA Akshaya Arivoli Syncfusion Team April 30, 2020 01:40 PM UTC

Hi Tusar, 

Based on the provided details, we suspect that the serviceUrl is not mapped properly so the Web API controller is called. We have created a simple PDF Viewer sample and shared the sample in the below link, 


Please find the below UG link to work with PDF Viewer, 


  
Please try it and if you need any further assistance, revert us with more details about your issue , modified sample These details will be helpful for us to investigate further and assist you better. 

Regards, 
Akshaya 


Loader.
Up arrow icon