DwsFileViewer
Shows a PDF, Word or Excel file inside the page, on a themed panel with a toolbar (file name, open in a new tab, download). PDFs use the browser's own PDF viewer; Word and Excel files are converted to HTML in the browser by mammoth.js and SheetJS, which ship with the library, and are sanitized before they are shown. Workbooks get one tab per worksheet. The search and the PDF page field are DwsInputs, the worksheet tabs DwsButtons that scroll sideways on a phone.
API ReferenceFileType PDF embeds the file with the browser's own PDF viewer, which brings page navigation, zoom and printing. The toolbar shows the file name (taken from the address unless FileName is set) with buttons to open the file in a new tab and to download it. Without a Height the viewer is 600px tall and never shrinks below 300px, so the PDF no longer collapses in a container without a height. A browser without an inline PDF viewer, and every iPhone and iPad (which show only the first page of an embedded PDF), gets the PDF drawn with PDF.js instead (PdfRenderer Auto, see the next section).
Show Code
FileViewer.razor
<DwsFileViewer Name="FileViewerDemo_Pdf" FileUrl="files/sample.pdf" FileType="DwsFileType.PDF" />FileViewer.razor.cs
// Configured entirely in Razor: FileUrl, FileType and the optional layout parameters.Size
Width, Height, MinWidth and MinHeight take pixels. Height replaces the 600px default; MinHeight and MinWidth replace the 300px / 280px minimums. An explicit Width never grows wider than the container.
Show Code
FileViewer.razor
<DwsFileViewer Name="FileViewerDemo_Size_Fixed" FileUrl="files/sample.pdf" FileType="DwsFileType.PDF" Width="560" Height="340" />FileViewer.razor.cs
// Configured entirely in Razor: FileUrl, FileType and the optional layout parameters.Hiding parts
ShowBackground='false' drops the panel, ShowToolbar='false' the toolbar, and ShowPdfToolbar='false' asks the browser's PDF viewer to hide its own toolbar (Chromium-based browsers and Acrobat follow it, Firefox and Safari do not). ShowOpenButton, ShowDownloadButton and ShowSheetTabs switch single parts off.
Show Code
FileViewer.razor
<DwsFileViewer Name="FileViewerDemo_Plain" FileUrl="files/sample.pdf" FileType="DwsFileType.PDF" ShowBackground="false" ShowToolbar="false" ShowPdfToolbar="false" Height="320" />FileViewer.razor.cs
// Configured entirely in Razor: FileUrl, FileType and the optional layout parameters.Interface-Description
| Name | Typ | Default | Description |
|---|---|---|---|
| IsLoading | bool | false | Whether a Word or Excel file is being fetched and converted. |
| LastError | DwsFileViewerErrorEventArgs? | null | The error the viewer shows, or null while the file is shown or loading. |
| SheetNames | IReadOnlyList<string> | [] | The worksheet names of the loaded Excel file, in workbook order; empty for other file types. |
| IsPdfJsActive | bool | false | Whether the shown PDF is drawn with PDF.js. |
| PdfPage / PdfPageCount / PdfZoom | int / int / int | 0 / 0 / 0 | PDF.js: the page in view, the number of pages and the zoom in percent (follows scrolling and the + / - keys). |
| SearchText / SearchMatchCount / SearchMatchIndex | string / int / int | "" / 0 / -1 | The text last searched for, the number of matches in the shown document or worksheet, and the match the search stands on. |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| ReloadAsync | Task | Loads the file again (fetches and converts a Word or Excel file anew, re-embeds a PDF); OnLoaded or OnError report the outcome. | |
| DownloadAsync | Task | Downloads the file under FileName. A file on another origin that does not allow reading it (CORS) is opened in a new tab instead. | |
| ShowSheetAsync | Task | int index | Shows another worksheet of the loaded Excel file; an index past the last sheet shows the last one. |
| SearchAsync | Task<int> | string? text | Searches the shown Word document or worksheet (case-insensitive), marks every match and returns their number; an empty text clears the marks. |
| SearchNextAsync / SearchPreviousAsync | Task | Moves to the next or previous match, wrapping around. | |
| GoToPdfPageAsync | Task | int page | PDF.js: scrolls to a page (1-based, clamped). |
| SetPdfZoomAsync / FitPdfToWidthAsync | Task | PDF.js: zooms to a percentage (25 to 500) or to the width of the view. |
| Name | Typ | Default | Description |
|---|---|---|---|
| FileUrl | string | string.Empty | Address of the file, absolute or relative to the page's base address. Changing it loads the new file. A Word or Excel file on another origin can only be read when its server allows it (CORS); a PDF embeds without. |
| FileType | DwsFileType | DwsFileType.PDF | How the file is shown: PDF with the browser's PDF viewer, Document (.docx) as HTML via mammoth.js, Excel (.xlsx, .xls, .ods, .csv) as a table per worksheet via SheetJS. Changing it loads the file again. |
| FileName | string? | null | Name shown in the toolbar and suggested when downloading. Left empty, it is the last segment of FileUrl. |
| ShowBackground | bool | true | Draws the viewer on a themed panel (surface background, border, rounded corners). |
| ShowToolbar | bool | true | Shows the toolbar above the file: file type icon, file name and the open / download buttons. |
| ShowDownloadButton | bool | true | Shows the download button. Registers its own permission namespace <Name>_Download. |
| ShowOpenButton | bool | true | Shows the button that opens the file in a new tab. Registers its own permission namespace <Name>_Open. |
| ShowSheetTabs | bool | true | Shows one tab per worksheet below a workbook with more than one sheet. |
| ShowPdfToolbar | bool | true | Shows the PDF viewer's toolbar: for PDF.js the row with previous / next page, page number and zoom; for the browser's viewer its own toolbar. Switched off, the viewer is asked to hide it (#toolbar=0): Chromium-based browsers and Acrobat follow that, Firefox and Safari do not. It does not stop anyone from saving the file. |
| ShowSearch | bool | true | Shows a search row below the toolbar for Word and Excel files: matches are marked, Enter and the arrow buttons step through them. Needs ShowToolbar and a browser with the CSS Custom Highlight API. |
| PdfRenderer | DwsPdfRenderer | DwsPdfRenderer.Auto | What shows a PDF: Native (the browser's own viewer), PdfJs (PDF.js, vendored under lib/pdfjs/ and loaded only when a PDF is shown with it) or Auto (PDF.js only where the browser's viewer fails: iPhone / iPad and browsers without an inline viewer). PDF.js shows every page with page navigation, zoom (buttons, + and - keys), selectable text and the search row; the browser's viewer also prints and fills forms. |
| ShowPdfFirstPageHint | bool | true | On iPhone and iPad, says above an embedded PDF that the device shows only its first page and links to the whole file. |
| MaxPreviewBytes | long? | null | Largest Word or Excel file in bytes that is converted; a larger one shows a message instead (error type TooLarge). Converting runs on the page thread of the browser. Null: no limit. |
| Color | DwsColor | DwsColor.Primary | Accent of the viewer: toolbar icons, file type icon, loading indicator, selected worksheet tab, focus ring and the retry button's border. It is mixed half and half with the text color, so every theme color keeps 3:1 against the panel in both themes. |
| BackgroundColor | DwsColor | DwsColor.Undefined | Tints the panel with a theme color (a light wash over the surface, so the text keeps its contrast in both themes). Left undefined, the panel is the theme's surface color. |
| Width | int? | null | Width in px, never wider than the container. Left null, the viewer fills its container. |
| Height | int? | null | Height in px. Left null, the viewer is 600px tall; Style="height: 100%" fills a container that has a height of its own. A Height below the 300px minimum lowers the minimum with it (unless MinHeight is set). |
| MinWidth | int? | null | Minimum width in px. Left null, the viewer keeps at least 280px (or its container's width, if narrower). |
| MinHeight | int? | null | Minimum height in px. Left null, the viewer keeps at least 300px, so a PDF never collapses to the browser's 150px default. |
| SheetIndex | int | 0 | Zero-based index of the worksheet shown for an Excel file, two-way bindable (SheetIndexChanged). An index past the last sheet shows the last one and is reported back. |
| Name | Typ | Description |
|---|---|---|
| SheetIndexChanged | EventCallback<int> | Raised when another worksheet is shown: picked by the user, or corrected to one that exists. |
| OnLoaded | EventCallback<DwsFileViewerLoadedEventArgs> | Raised once a file is shown, with its address, type, worksheet names and whether it is empty. For a PDF it is raised when the browser reports the embedded viewer as loaded. |
| OnError | EventCallback<DwsFileViewerErrorEventArgs> | Raised when a file can not be loaded or read, and when a download fails, with the reason (Network, HttpStatus with StatusCode, Unreadable, LibraryMissing, Script, Download, and LoadFailed for a PDF the browser could not load). |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Disables the form control, ensuring it doesn't participate in form submission. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the component's root element. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
