|
| 1 | +--- |
| 2 | +title: Overview |
| 3 | +page_title: PdfViewer - Overview |
| 4 | +description: Overview of the PDF Viewer for Blazor. |
| 5 | +slug: pdfviewer-overview |
| 6 | +tags: telerik,blazor,pdf,pdfviewer |
| 7 | +published: True |
| 8 | +position: 0 |
| 9 | +--- |
| 10 | + |
| 11 | +# Blazor PdfViewer Overview |
| 12 | + |
| 13 | +The <a href = "https://www.telerik.com/blazor-ui/pdf-viewer" target="_blank">Pdf Viewer for Blazor</a> allows users to open PDF files directly in the browser. The component provides features such as paging, zooming, printing, text selection and search. In addition, users can upload and display a PDF file from their local device, or download the currently open file. |
| 14 | + |
| 15 | + |
| 16 | +## Creating Blazor PdfViewer |
| 17 | + |
| 18 | +To use a Telerik PDF Viewer for Blazor: |
| 19 | + |
| 20 | +1. Add the `TelerikPdfViewer` tag. |
| 21 | +1. Set the `Data` parameter to a byte array (`byte[]`) that holds the PDF file contents. |
| 22 | +1. If you are developing a Blazor **Server** app, [increase the maximum SignalR message size](#large-file-support). |
| 23 | +1. (optional) Subscribe to the [PDF Viewer's events]({%slug pdfviewer-events%}). For example, use the `OnDownload` event to set the name of the downloaded file. |
| 24 | +1. (optional) Set [`Width` or `Height`](#pdfviewer-parameters) for the component. |
| 25 | + |
| 26 | +>caption Basic Blazor PDF Viewer |
| 27 | +
|
| 28 | +````CSHTML |
| 29 | +<TelerikPdfViewer Data="@PdfSource" |
| 30 | + OnDownload="@OnPdfDownload" |
| 31 | + Height="600px"> |
| 32 | +</TelerikPdfViewer> |
| 33 | +
|
| 34 | +@code { |
| 35 | + private byte[] PdfSource { get; set; } |
| 36 | +
|
| 37 | + private async Task OnPdfDownload(PdfViewerDownloadEventArgs args) |
| 38 | + { |
| 39 | + args.FileName = "PDF-Viewer-Download"; |
| 40 | + } |
| 41 | +
|
| 42 | + protected override void OnInitialized() |
| 43 | + { |
| 44 | + PdfSource = Convert.FromBase64String(PdfBase64); |
| 45 | +
|
| 46 | + base.OnInitialized(); |
| 47 | + } |
| 48 | +
|
| 49 | + private const string PdfBase64 = "JVBERi0xLjEKMSAwIG9iajw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAyIDAgUj4+ZW5kb2JqCjIgMCBvYmo8PC9UeXBlL1BhZ2VzL0tpZHNbMyAwIFJdL0NvdW50IDEvTWVkaWFCb3ggWy00MCAtNjQgMjYwIDgwXSA+PmVuZG9iagozIDAgb2JqPDwvVHlwZS9QYWdlL1BhcmVudCAyIDAgUi9SZXNvdXJjZXM8PC9Gb250PDwvRjE8PC9UeXBlL0ZvbnQvU3VidHlwZS9UeXBlMS9CYXNlRm9udC9BcmlhbD4+ID4+ID4+L0NvbnRlbnRzIDQgMCBSPj5lbmRvYmoKNCAwIG9iajw8L0xlbmd0aCA1OT4+CnN0cmVhbQpCVAovRjEgMTggVGYKMCAwIFRkCihUZWxlcmlrIFBkZlZpZXdlciBmb3IgQmxhem9yKSBUagpFVAplbmRzdHJlYW0KZW5kb2JqCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZgowMDAwMDAwMDIxIDAwMDAwIG4KMDAwMDAwMDA4NiAwMDAwMCBuCjAwMDAwMDAxOTUgMDAwMDAgbgowMDAwMDAwNDkwIDAwMDAwIG4KdHJhaWxlciA8PCAgL1Jvb3QgMSAwIFIgL1NpemUgNSA+PgpzdGFydHhyZWYKNjA5CiUlRU9G"; |
| 50 | +} |
| 51 | +```` |
| 52 | + |
| 53 | + |
| 54 | +## Toolbar |
| 55 | + |
| 56 | +The [PdfViewer toolbar can render built-in and custom tools]({%slug pdfviewer-toolbar%}). The default tools enable built-in features such as: |
| 57 | + |
| 58 | +* Page, zoom and pan documents |
| 59 | +* Search and select text |
| 60 | +* Print, download and open local PDF files |
| 61 | + |
| 62 | + |
| 63 | +## Large File Support |
| 64 | + |
| 65 | +In Blazor **Server** apps, the PDF Viewer uses the **SignalR WebSocket** to: |
| 66 | + |
| 67 | +* Open PDF files from the server and send them to the browser. |
| 68 | +* Read the PDF file `Stream` from the user device in the [`OnOpen` event handler]({%slug pdfviewer-events%}#onopen). The PDF Viewer uses internally a [FileSelect component]({%slug fileselect-overview%}) to get the user file. |
| 69 | + |
| 70 | +The SignalR WebSocket has a default maximum message size of **32 KB**. To work with larger files in the above two scenarios, [increase the max WebSocket message size for the Blazor application]({%slug fileselect-overview%}#large-file-support). |
| 71 | + |
| 72 | + |
| 73 | +## PdfViewer Parameters |
| 74 | + |
| 75 | +The table below lists the PDF Viewer parameters. Also check the [PDF Viewer API Reference](/blazor-ui/api/Telerik.Blazor.Components.TelerikPdfViewer) for all parameters, methods and events. |
| 76 | + |
| 77 | +@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout) |
| 78 | + |
| 79 | +| Parameter | Type and Default Value | Description | |
| 80 | +| --- | --- | --- | |
| 81 | +| `Class` | `string` | An additional CSS class for the `<div class="k-pdf-viewer">` element. Use it to [customize the component styles and override the theme]({%slug themes-override%}). | |
| 82 | +| `Data` | `byte[]` | The source of the currently displayed PDF file. | |
| 83 | +| `EnableLoaderContainer` | `bool` <br /> (`true`) | Determines if the PDF Viewer will show a loading animation during opening, downloading or zooming a PDF file. | |
| 84 | +| `Height` | `string` | The PdfViewer height as a [CSS length value]({%slug common-features/dimensions%}). If not set, the component will expand vertically, based on the loaded file. `Height` is required for the component paging and scrolling to work. | |
| 85 | +| `MaxZoom` | `decimal` <br /> (`4m`) | The largest possible zoom level. The default value allows zooming in 4 times (400%). | |
| 86 | +| `MinZoom` | `decimal` <br /> (`0.5m`) | The smallest possible zoom level. The default value allows zooming out to 50%. | |
| 87 | +| `Width` | `string` | The PdfViewer width as a [CSS length value]({%slug common-features/dimensions%}). If not set, the component will expand horizontally to fill its parent. | |
| 88 | +| `Zoom` | `decimal` <br /> (`1.25m`) | The current zoom level. Use the parameter with two-way binding or with a [`ZoomChanged` event handler]({%slug pdfviewer-events%}#zoomchanged). | |
| 89 | +| `ZoomRate` | `decimal` <br /> (`0.25m`) | The zoom level change that is used by the zoom in and zoom out buttons. | |
| 90 | + |
| 91 | + |
| 92 | +## PdfViewer Reference and Methods |
| 93 | + |
| 94 | +The PdfViewer exposes methods for programmatic operation. To use them, define a reference to the component instance with the `@ref` directive attribute. |
| 95 | + |
| 96 | +@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout) |
| 97 | + |
| 98 | +| Method | Description | |
| 99 | +| --- | --- | |
| 100 | +| `Rebind` | Refreshes the PDF Viewer and ensures it is displaying the latest file `Data`. [`Rebind` is necessary when the Blazor framework cannot re-render components automatically]({%slug common-features-data-binding-overview%}#refresh-data). | |
| 101 | + |
| 102 | + |
| 103 | +>caption PDF Viewer reference and Rebind method usage |
| 104 | +
|
| 105 | +````CSHTML |
| 106 | +<TelerikPdfViewer @ref="@PdfViewerRef" |
| 107 | + Data="@PdfSource"> |
| 108 | + <PdfViewerToolBar> |
| 109 | + <PdfViewerToolBarCustomTool> |
| 110 | + <TelerikButton OnClick="@OnButtonClick" Icon="refresh">Rebind PDF Viewer</TelerikButton> |
| 111 | + </PdfViewerToolBarCustomTool> |
| 112 | + </PdfViewerToolBar> |
| 113 | +</TelerikPdfViewer> |
| 114 | +
|
| 115 | +@code { |
| 116 | + private TelerikPdfViewer PdfViewerRef { get; set; } |
| 117 | +
|
| 118 | + private async Task OnButtonClick() |
| 119 | + { |
| 120 | + PdfViewerRef.Rebind(); |
| 121 | + } |
| 122 | +
|
| 123 | + private byte[] PdfSource |
| 124 | + { |
| 125 | + get |
| 126 | + { |
| 127 | + return System.Text.Encoding.UTF8.GetBytes( |
| 128 | + PdfSourceRaw.Replace("...", |
| 129 | + PdfUpdateFlag ? "updated at " + DateTime.Now.ToLongTimeString() : "") |
| 130 | + ); |
| 131 | + } |
| 132 | + } |
| 133 | +
|
| 134 | + protected override async Task OnInitializedAsync() |
| 135 | + { |
| 136 | + PdfSourceRaw = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PdfBase64)); |
| 137 | +
|
| 138 | + await Task.Delay(1000); |
| 139 | +
|
| 140 | + // PdfUpdateFlag is used in the PdfSource getter to make the document change more obvious |
| 141 | + PdfUpdateFlag = true; |
| 142 | +
|
| 143 | + PdfViewerRef.Rebind(); |
| 144 | +
|
| 145 | + await base.OnInitializedAsync(); |
| 146 | + } |
| 147 | +
|
| 148 | + private bool PdfUpdateFlag { get; set; } |
| 149 | +
|
| 150 | + private string PdfSourceRaw { get; set; } |
| 151 | +
|
| 152 | + private const string PdfBase64 = "JVBERi0xLjEKMSAwIG9iajw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAyIDAgUj4+ZW5kb2JqCjIgMCBvYmo8PC9UeXBlL1BhZ2VzL0tpZHNbMyAwIFJdL0NvdW50IDEvTWVkaWFCb3ggWy0zMCAtNjQgMjcwIDgwXSA+PmVuZG9iagozIDAgb2JqPDwvVHlwZS9QYWdlL1BhcmVudCAyIDAgUi9SZXNvdXJjZXM8PC9Gb250PDwvRjE8PC9UeXBlL0ZvbnQvU3VidHlwZS9UeXBlMS9CYXNlRm9udC9BcmlhbD4+ID4+ID4+L0NvbnRlbnRzIDQgMCBSPj5lbmRvYmoKNCAwIG9iajw8L0xlbmd0aCA1OT4+CnN0cmVhbQpCVAovRjEgMTggVGYKMCAwIFRkCihQREYgRmlsZSAuLi4pIFRqCkVUCmVuZHN0cmVhbQplbmRvYmoKeHJlZgowIDUKMDAwMDAwMDAwMCA2NTUzNSBmCjAwMDAwMDAwMjEgMDAwMDAgbgowMDAwMDAwMDg2IDAwMDAwIG4KMDAwMDAwMDE5NSAwMDAwMCBuCjAwMDAwMDA0OTAgMDAwMDAgbgp0cmFpbGVyIDw8ICAvUm9vdCAxIDAgUiAvU2l6ZSA1ID4+CnN0YXJ0eHJlZgo2MDkKJSVFT0Y="; |
| 153 | +} |
| 154 | +```` |
| 155 | + |
| 156 | + |
| 157 | +## Next Steps |
| 158 | + |
| 159 | +* [Customize the PDF Viewer toolbar]({%slug pdfviewer-toolbar%}) |
| 160 | +* [Handle PDF Viewer events]({%slug pdfviewer-events%}) |
| 161 | + |
| 162 | + |
| 163 | +## See Also |
| 164 | + |
| 165 | +* [PdfViewer Live Demo](https://demos.telerik.com/blazor-ui/pdfviewer/overview) |
| 166 | +* [PdfViewer API](/blazor-ui/api/Telerik.Blazor.Components.TelerikPdfViewer) |
0 commit comments