import { PdfViewer, Toolbar, Magnification, Navigation, LinkAnnotation,ThumbnailView,BookmarkView,
TextSelection, Annotation} from '@syncfusion/ej2-pdfviewer';
PdfViewer.Inject(Toolbar,Magnification,Navigation, LinkAnnotation,ThumbnailView,BookmarkView,
TextSelection, Annotation);
let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = "http://localhost:1454/api/pdfviewer";
//pdfviewer.serviceUrl = "https://ej2services.syncfusion.com/production/web-services/api/pdfviewer";
// pdfviewer.enableAllModules();
pdfviewer.appendTo('#PdfViewer');
pdfviewer.load('test.pdf', null);
//pdfviewer.load('PDF_Succinctly.pdf', null);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Syncfusion.EJ2.PdfViewer;
using System.IO;
using System.Web;
using System.Web.Http.Cors;
namespace pdfviwer_service.Controllers
{
[EnableCorsAttribute("*","*","*")]
public class PdfViewerController : ApiController
{
[HttpPost]
public object Load(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
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 (jsonObject["document"] + " is not found");
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return (JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public object Bookmarks(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonResult = pdfviewer.GetBookmarks(jsonObject);
return (JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public object RenderPdfPages(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
object jsonResult = pdfviewer.GetPage(jsonObject);
return (JsonConvert.SerializeObject(jsonResult));
}
[HttpPost]
public object RenderThumbnailImages(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
object result = pdfviewer.GetThumbnailImages(jsonObject);
return (JsonConvert.SerializeObject(result));
}
[HttpPost]
public object Unload(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
pdfviewer.ClearCache(jsonObject);
return ("Document cache is cleared");
}
[HttpPost]
public HttpResponseMessage Download(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
return (GetPlainText(documentBase));
}
[HttpPost]
public object PrintImages(Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
object pageImage = pdfviewer.GetPrintImage(jsonObject);
return (JsonConvert.SerializeObject(pageImage));
}
private HttpResponseMessage GetPlainText(string pageImage)
{
var responseText = new HttpResponseMessage(HttpStatusCode.OK);
responseText.Content = new StringContent(pageImage, System.Text.Encoding.UTF8, "text/plain");
return responseText;
}
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
private string GetDocumentPath(string document)
{
string documentPath = string.Empty;
if (!System.IO.File.Exists(document))
{
var path = HttpContext.Current.Request.PhysicalApplicationPath;
if (System.IO.File.Exists(path + "App_Data\\Data\\" + document))
documentPath = path + "App_Data\\Data\\" + document;
}
else
{
documentPath = document;
}
return documentPath;
}
}
}