Save PDF server side

Hi,

Is it possible to save an annotated pdf server side using ES5 javascript?

Thanks . . .


13 Replies

VS Vasugi Sivajothi Syncfusion Team July 16, 2021 09:51 AM UTC

Hi Sean, 
Thank you for contacting Syncfusion support. 
You can save the PDF document on the server-side after adding annotations by using the SaveDocument method and we can access the web action method on the client-side by using the ServerActionSettings API. We have shared the code snippet and sample for ES5 javascript. 
  
Code Snippet for client side: 
  
<button id="id">Save</button> 
document.getElementById('id').addEventListener('click', function() { 
  viewer.serverActionSettings.download = 'SaveDocument'; 
  viewer.download(); 
  viewer.serverActionSettings.download = 'Download'; 
}); 
  
  
  
Code Snippet for server side: 
  
        [HttpPost] 
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] 
        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); 
        } 
  
  
  
  
  
  
Note: In the provided JS sample, we have a button (Save). On button-click, the newly created method (SaveDocument) in the PdfViewerController.cs file is invoked, and then the document is saved to the project location. 
  
Regards, 
Vasugi. 



SE Sean August 1, 2021 03:49 PM UTC

Hi  Vasugi,


Many thanks for your earlier response. The provided example works perfectly however the web service is .net core. Do you have a .net framework example of the web service?


Also, do you have an example where only the annotations toolbar is displayed - ideally only the "highlight", "underline", "strikethrough", "delete annotation" and "comment panel" buttons are displayed (see image)? 


Thanks,

Sean

PdfViewer.png


 



VS Vasugi Sivajothi Syncfusion Team August 2, 2021 12:23 PM UTC

Hi Sean, 
 
Thank you for the update. 
 
Please find the details. 
Query 
Details 
 
Many thanks for your earlier response. The provided example works perfectly however the web service is .net core. Do you have a .net framework example of the web service? 
 
We have shared the MVC sample and documentation for your reference. Please refer to the below links to create a web service using the .net framework. 
 
 
 
 
 
 
 
Also, do you have an example where only the annotations toolbar is displayed - ideally only the "highlight", "underline", "strikethrough", "delete annotation" and "comment panel" buttons are displayed (see image)?  
 
 
You can hide and show the toolbar items using the toolbarSettings API. Please refer to the below code snippet and sample. 
 
Code Snippet: 
 
 
toolbarSettings: { 
    showTooltip: true, 
    toolbarItems: [ 
      'OpenOption', 
      'PageNavigationTool', 
      'MagnificationTool', 
      'PanTool', 
      'SelectionTool', 
      'SearchOption', 
      'PrintOption', 
      'DownloadOption', 
      'UndoRedoTool', 
      'AnnotationEditTool', 
      'FormDesignerEditTool', 
      'CommentTool', 
      'SubmitForm' 
    ], 
    annotationToolbarItems: [ 
      'HighlightTool', 
      'UnderlineTool', 
      'StrikethroughTool', 
      'AnnotationDeleteTool', 
      'CommentPanelTool' 
    ] 
  } 
 
 
 
 
 
 
Regards, 
Vasugi. 




SE Sean replied to Vasugi Sivajothi August 10, 2021 12:39 PM UTC

Hi  Vasugi,

I have implemented all of your suggestions and mostly have the control working.

The web service however fails when I try to save the document. It fails on this line . . .

string base64String = documentBase.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1];

The exception is System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

I think this is being caused by the line before . . .

string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);

If I interrogate documentBase I see the following error

Could not load file or assembly 'Syncfusion.Licensing, Version=19.2450.0.51, Culture=neutral, PublicKeyToken=632609b4d040f6b4' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

In line with the documentation and various support queries I have check the packages and all look ok . . .

  <package id="Syncfusion.Compression.Base" version="19.2.0.51" targetFramework="net46" />

  <package id="Syncfusion.EJ2.JavaScript" version="19.2.0.51" targetFramework="net46" />

  <package id="Syncfusion.EJ2.PdfViewer.AspNet.Mvc5" version="19.2.0.51" targetFramework="net46" />

  <package id="Syncfusion.Licensing" version="19.2.0.51" targetFramework="net46" />

  <package id="Syncfusion.Pdf.AspNet.Mvc5" version="19.2.0.51" targetFramework="net46" />

Can you suggest where the problem is? 

Thanks,

Sean 



VS Vasugi Sivajothi Syncfusion Team August 11, 2021 11:08 AM UTC

Hi Sean, 
 
Thank you for the update. 
 
We suspect that the reported issue is due to base64string is not getting properly while saving the document. Could you please confirm whether the base64string of the document gets properly in documentBase variable? And please ensure whether the assembly reference is in the same version of the target framework. If not, kindly follow the below steps to add the assembly in the same version of the target framework. 
 
Steps: 
    1. You need to remove the 'Syncfusion.Licensing’ package from the references.
    2. Then add the 'Syncfusion.Licensing.dll’ with same version of target framework from the package in project folder.
    3. And please clean and build the project.
 
We have shared the video for your reference which can be downloaded from the below link. 
 
 
Kindly try this and let us know if the issue resolves or not. If not, please share the modified sample to replicate the issue on our end and more details about the issue. This will be helpful to investigate further and provide the solution at the earliest. 
 
Regards, 
Vasugi. 



SE Sean August 11, 2021 01:00 PM UTC

Hi  Vasugi,


Unfortunately your suggestion did not work. 

I have created a stripped down version of the app but the smallest I can make it is 80MB. This exceeds your max attachment size so I cannot upload. Do you have an alternate mechanism to upload our sample web service so you can review?


Alternatively can you upload a working version of the web service with the correct licensing package installed? 


Thanks,

Sean



SE Sean August 12, 2021 12:45 PM UTC

Hi  Vasugi,

Just wondering if you have an update on this item - we are approaching our deployment date and need to make a decision on the feasibility of using this control. 

Many Thanks,

Sean



VS Vasugi Sivajothi Syncfusion Team August 12, 2021 01:54 PM UTC

Hi Sean,  
 
We suspect that the reported issue is due to mismatch of target framework. We have shared the Web service sample with the framework 4.5 for your reference. Please refer to the below sample. 
 
 
 
Kindly ensure the framework version with the assembly reference and let us know if the issue resolves or not. 
 
Regards, 
Vasugi. 



SE Sean August 12, 2021 02:53 PM UTC

Hi  Vasugi,


I'm  not sure if we are doing something wrong at our end but your sample application does not compile. See attached screen shots. Is there something that we should be doing here before building the solution?


Thanks,

Sean.

Capture.PNG



VS Vasugi Sivajothi Syncfusion Team August 13, 2021 11:25 AM UTC


Hi Sean,   
 
We suspect that the reported issue occurs because the script is not referred while building the application on your end. Kindly delete the ej2 file and build the application again. We have shared the video for your reference which can be downloaded from the below link. 
 
 
Kindly try the provided sample and let us know if the issue resolves or not. If you have still further assistance to resolve this issue, we can setup a web meeting to check the issue in your environment and provide the solution. Please let us know your availability. We will make every effort to have this scheduled on a date and time of your convenience. 
 
Regards, 
Vasugi. 



SE Sean August 16, 2021 04:30 AM UTC

Hi Vasugi,

Thanks for the updated application. This has helped us move forward but we are still having multiple problems when we extend your sample into a real world solution . . .

  • When we save a pdf, the file will only save if there are no annotations. Once we add annotations we see that documentBase  = pdfviewer.GetDocumentAsBase64(jsonObject) returns "object reference not set to an instance of an object" and we then get an "index was outside the bounds of the array error" here - 
  • Dim base64String As String = documentBase.Split(New String() {"data:application/pdf;base64,"}, StringSplitOptions.None)(1)

  • Our desired solution for the component is  . . .
    • we pass any file name for a docx, xlsx, pdf, pptx
    • for a docx, xlsx or pptx we convert the file to a pdf on the fly and pass to the viewer
    • when the save option is clicked, the annotated pdf is save to the server

When we try to add the further required packages to deliver the conversion logic we end up with some severe package errors when we try to execute the application (eg. Could not load file or assembly 'Syncfusion.Licensing' or one of its dependencies).  We get various different cuts of these errors depending on the order in which we install the further packages. 


We would greatly appreciate your assistance and a web meeting would be ideal. We are available any time from 1pm BST (UTC +1).

Many Thanks,

Sean.



SE Sean August 16, 2021 04:32 AM UTC

Apologies Vasugi,

I should have added that we are using .net framework 4.6.2. and Syncfusion version 19.2.0.55.

Thanks . . .



VS Vasugi Sivajothi Syncfusion Team August 17, 2021 09:15 AM UTC

Hi Sean,  
 
We have created a new incident under your Direct Trac account. Please find and follow up the incident, we have requested meeting in incident. 
 
Regards, 
Vasugi. 


Loader.
Up arrow icon