New to Telerik UI for ASP.NET Core? Download free 30-day trial

Getting Started with the PDFViewer

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core PDFViewer and highlights the major steps in the configuration of the component.

You will initialize a PDFViewer component and visualize a PDF document in it. Finally, you can run the sample code in Telerik REPL and continue exploring the components.

Sample Telerik UI for ASP.NET Core PDFViewer

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

  • You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.

  • You can prepare a Visual Studio project by following either of these guides:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET Core HtmlHelpers:

    @using Kendo.Mvc.UI
    
  • To use the Telerik UI for ASP.NET Core TagHelpers:

    @addTagHelper *, Kendo.Mvc
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

    @using Kendo.Mvc.UI

    <h4>PDFViewer</h4>
    <div>

    </div>
    @addTagHelper *, Kendo.Mvc

    <h4>PDFViewer</h4>
    <div>

    </div>

2. Initialize the PDFViewer

Load the pdf.js library and use the PDFViewer HtmlHelper or TagHelper to add the component to the view:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the PDFViewer element.
  • The Height() configuration allows you to control the height of the component.
@using Kendo.Mvc.UI

<h4>PDFViewer</h4>
<div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

    <div>
        @(Html.Kendo().PDFViewer()
            .Name("pdfviewer")
            .Height(400)
        )
    </div>

    <style>
        html body #pdfviewer {
            width: 100% !important;
        }
    </style>
</div>

@addTagHelper *, Kendo.Mvc

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

<div id="example">
    <kendo-pdfviewer name="pdfviewer"
        height="400">
    </kendo-pdfviewer>
</div>

<style>
    html body #pdfviewer {
        width: 100% !important;
    }
</style>

3. Load an Initial PDF Document

To load and visualize an initial PDF document, set the PdfjsProcessing configuration and specify the path to the PDF file.

@using Kendo.Mvc.UI

<h4>PDFViewer</h4>
<div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

    <div>
        @(Html.Kendo().PDFViewer()
            .Name("pdfviewer")
            .PdfjsProcessing(pdf => pdf.File(Url.Content("~/shared/web/pdfViewer/sample.pdf")))
            .Height(400)
        )
    </div>

    <style>
        html body #pdfviewer {
            width: 100% !important;
        }
    </style>
</div>

@addTagHelper *, Kendo.Mvc

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

<div id="example">
    <kendo-pdfviewer name="pdfviewer"
        height="400">
        <pdfjs-processing file="@Url.Content("~/shared/web/pdfViewer/sample.pdf")" />
    </kendo-pdfviewer>
</div>

<style>
    html body #pdfviewer {
        width: 100% !important;
    }
</style>

4. Handle a PDFViewer Event

The PDFViewer exposes various events that you can handle and further customize the functionality of the component. In this example, you will use the Open event, which fires when a PDF is opened in the PDFViewer. As a result, the browser console will log a message when you open a PDF file.

@using Kendo.Mvc.UI

<h4>PDFViewer</h4>
<div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

    <div>
        @(Html.Kendo().PDFViewer()
            .Name("pdfviewer")
            .PdfjsProcessing(pdf => pdf.File(Url.Content("~/shared/web/pdfViewer/sample.pdf")))
            .Height(400)
            .Events(events => events
                .Open("onOpen")
            )
        )
    </div>

    <script>
        function onOpen(e) {
            console.log("file open: " + e.file.name);
        }
    </script>

    <style>
        html body #pdfviewer {
            width: 100% !important;
        }
    </style>
</div>

@addTagHelper *, Kendo.Mvc

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script>

<div id="example">
    <kendo-pdfviewer name="pdfviewer"
        <pdfjs-processing file="@Url.Content("~/shared/web/pdfViewer/sample.pdf")" />
        height="400"
        on-open="onOpen">
    </kendo-pdfviewer>
</div>

<script>
    function onOpen(e) {
        console.log("file open: " + e.file.name);
    }
</script>

<style>
    html body #pdfviewer {
        width: 100% !important;
    }
</style>

5. (Optional) Reference Existing PDFViewer Instances

You can reference the PDFViewer instances that you have created and build on top of their existing configuration:

  1. Use the id attribute of the component instance to establish a reference.

    <script>
        var pdfviewerReference = $("#pdfviewer").data("kendoPDFViewer"); // pdfviewerReference is a reference to the existing PDFViewer instance of the helper.
    </script>
    
  2. Use the PDFViewer client-side API to control the behavior of the widget. In this example, you will use the activatePage method to activate a page.

    <input id="btn1" type="button" value="Load page 3" onclick="changePdfViewerPage()" />
    
    <script>
        function changePdfViewerPage() {
            //get a reference to the PDFViewer instance
            var pdfViewer = $("#pdfviewer").data("kendoPDFViewer");
            // activate page 3
            pdfViewer.activatePage(3);
        }
    </script>
    

Next Steps

See Also

In this article