How to load the PDF file in PDF viewer using VB
The PDF document is loaded in the PDF viewer control using the load() method in client-side. The name, path, and base64 string of the PDF document is used to load the document. Refer to the following code.
JavaScript
var pdfviewer=$(“ #pdfviewer”).data(“ejPdfViewer”); pdfviewer.load(“ HTTP Succinctly”);
VB
Imports Newtonsoft.Json
Imports Syncfusion.EJ.PdfViewer
Imports System.IO
Imports System.Web.Http
Namespace PdfViewerWeb.WebApi
Public Class PdfViewerController
Inherits ApiController
Public Function Load(ByVal jsonResult As Dictionary(Of String, String)) As Object
Dim helper As PdfViewerHelper = New PdfViewerHelper()
If jsonResult.ContainsKey("newFileName") Then
Dim name = jsonResult("newFileName")
Dim pdfName = name.ToString() & ".pdf"
helper.Load(HttpContext.Current.Server.MapPath("~/Data/" & pdfName))
Else
If jsonResult.ContainsKey("isInitialLoading") Then
If jsonResult.ContainsKey("file") Then
Dim name = jsonResult("file")
helper.Load(name)
Else
helper.Load(HttpContext.Current.Server.MapPath("~/Data/HTTP Succinctly.pdf"))
End If
End If
End If
Return JsonConvert.SerializeObject(helper.ProcessPdf(jsonResult))
End Function
Public Function FileUpload(ByVal jsonResult As Dictionary(Of String, String)) As Object
Dim helper As PdfViewerHelper = New PdfViewerHelper()
If jsonResult.ContainsKey("uploadedFile") Then
Dim fileUrl = jsonResult("uploadedFile")
Dim byteArray As Byte() = Convert.FromBase64String(fileUrl)
Dim stream As MemoryStream = New MemoryStream(byteArray)
helper.Load(stream)
End If
Return JsonConvert.SerializeObject(helper.ProcessPdf(jsonResult))
End Function
Public Function Download(ByVal jsonResult As Dictionary(Of String, String)) As Object
Dim helper As PdfViewerHelper = New PdfViewerHelper()
Return helper.GetDocumentData(jsonResult)
End Function
Public Sub Unload()
Dim helper As PdfViewerHelper = New PdfViewerHelper()
helper.UnLoad()
End Sub
End Class
End Namespace
Note:
- If the name of the PDF document is only passed as a parameter in the load() method, the PDF document must be available in the folder specified in the Load action method in the controller.
- If the path of the PDF document is passed as a parameter in load() method, the following code should be used in the Load action method in the controller.
If jsonResult.ContainsKey("newFileName") Then Dim name = jsonResult("newFileName") Dim pdfName = name.ToString() helper.Load(pdfName)
You can download the working sample from PdfViewerWeb.Zip.
By executing the program, you will get the output as follows.
Clicking the Load PDF document button will load the F# Succinctly.pdf document in PDF viewer.