This sample demonstrate the following key features of PDF Viewer:
View the PDF document
Core interactions - Scrolling, Zooming, panning and page navigation
Built-in toolbar
Select and copy text from PDF file
Search a text easily across the PDF document
Easy navigation with the help of Bookmarks, thumbnails, hyperlinks and table of contents
View modes - fit to page and fit to width
Print the entire document or a specific page directly from the browser.
More information on the PDF Viewer instantiation can be found in this documentation section.
Source Code
You can download the source code for this project here.
Design-Time
The EJ2 PdfViewer control can be added to a Form or Page from the Visual Studio Toolbox. It should look like this when dropped onto the designer.
The design-time appearance of the EJ2 PdfViewer control will not show a PDF document. The logic for this should be implemented in your code (see below).
Sample Implementation
PdfViewer.cs
/// <summary>/// PDF Viewer Server Side Configuration Example./// https://github.com/SyncfusionExamples/PdfViewer-Server/blob/master/src/ej2-pdfviewer-server/Controllers/PdfViewerController.cs.
/// </summary>publicpartialclassPDFViewer:TestBase{publicPDFViewer(){InitializeComponent();}privatevoidpdfViewer1_Appear(object sender,EventArgs e){this.pdfViewer1.Instance.load("Wisej.pdf",null);}privatevoidpdfViewer1_WebRequest(object sender,WebRequestEventArgs e){ // decode payload.dynamic payload =null;using (StreamReader reader =newStreamReader(e.Request.InputStream)) {var text =reader.ReadToEnd(); payload =JsonConvert.DeserializeObject<Dictionary<string,string>>(text); }switch(payload["action"]) {case"Load":e.Response.Write(LoadPdf(payload));break;case"RenderPdfPages":e.Response.Write(RenderPdfPages(payload));break;case"RenderPdfTexts":e.Response.Write(RenderPdfTexts(payload));break;case"RenderThumbnailImages":e.Response.Write(RenderThumbnailImages(payload));break;case"Download":e.Response.Write(Download(payload));break;case"PrintImages":e.Response.Write(PrintImages(payload));break;case"RenderAnnotationComments":e.Response.Write(RenderAnnotationComments(payload));break;case"Unload":e.Response.Write(PrintImages(payload));break;default:break; }}privatestringRenderPdfTexts(dynamic payload){; var renderer =newPdfRenderer();var result =renderer.GetDocumentText(payload);returnJsonConvert.SerializeObject(result);}privatestringLoadPdf(Dictionary<string,string> payload){MemoryStream ms;PdfRenderer renderer =newPdfRenderer();bool isFileName =bool.Parse(payload["isFileName"]);if (isFileName) {var path =Application.MapPath($"Data/PdfViewer/{payload["document"]}");var bytes =File.ReadAllBytes(path); ms =newMemoryStream(bytes); }else {var bytes =Convert.FromBase64String(payload["document"]); ms =newMemoryStream(bytes); }var result =renderer.Load(ms, payload);returnJsonConvert.SerializeObject(result,Formatting.None);}privatestringRenderPdfPages(Dictionary<string,string> payload){var renderer =newPdfRenderer();var page =renderer.GetPage(payload);returnJsonConvert.SerializeObject(page);}privatestringRenderThumbnailImages(Dictionary<string,string> payload){var renderer =newPdfRenderer();var result =renderer.GetThumbnailImages(payload);returnJsonConvert.SerializeObject(result);}privatestringDownload(Dictionary<string,string> payload){var renderer =newPdfRenderer();returnrenderer.GetDocumentAsBase64(payload);}privatestringPrintImages(Dictionary<string,string> payload){var renderer =newPdfRenderer();var result =renderer.GetPrintImage(payload);returnJsonConvert.SerializeObject(result);}privatestringUnload(Dictionary<string,string> payload){var renderer =newPdfRenderer();renderer.ClearCache(payload);return"Cleared Cache.";}privatestringRenderAnnotationComments(Dictionary<string,string> payload){var renderer =newPdfRenderer();var result =renderer.GetAnnotationComments(payload);returnJsonConvert.SerializeObject(result);}privatevoidbutton1_Click(object sender,EventArgs e){this.pdfViewer1.Instance.load("Annotations.pdf",null);}
Runtime
The resulting EJ2 PdfViewer control will look something like this: