Skip to content

Extras

PdfViewer

Bit.BlazorUI.ExtrasPdfReaderPdf

BitPdfViewer is a native pure-C# pdf viewer for Blazor: it parses pdf files and paints their pages as plain HTML/CSS (or onto a canvas), with no browser plugin and no js library. It ships a full reading toolbar - navigation, zoom and fit modes, page layouts, rotation, find, thumbnails, bookmarks, attachments and layers, printing, download, presentation and fullscreen - and everything the toolbar does is on the public API too.

API & large documents

Driving the viewer from code, loading a document from a seekable server stream, and fetching it by HTTP Range requests as its pages are needed.

Public API

Every toolbar action is a method, so your own chrome can drive a viewer whose toolbar is hidden. Print takes the whole document, a page range, or (PrintCurrentPage) one page, at each page's true size. GoToDestination lands on the exact spot a bookmark points at, GetSelectedText reads what the reader highlighted, and OpenAsync, ExtractText and RenderPageHtml are API-only.



/

No document loaded.

Seekable stream (server)

FromStreamFactory opens a fresh readable, seekable stream for each viewer load and owns it until replacement, closing or disposal. Password retries reuse that stream. Keep the source in a field; prerendering and interactive rendering each invoke the factory. The initial stream position is the PDF origin, and the remaining length must fit Int32. Keep its bytes and length stable; same-length changes cannot be detected. A caller needing different ownership can return a non-disposing wrapper.

Stream sources use a bounded raw-byte cache. Server parsing, page/thumbnail rendering, search and print work run off the circuit even when BackgroundRendering is false. Download streams the original bytes without retaining a complete server buffer. Decoded resources and cached decrypted stream buffers are outside the raw-byte cache budget. GetBytes() returns null. GetBytesAsync explicitly returns a complete caller-owned copy without retaining it. Use RenderPageHtmlAsync, ExtractPageTextAsync and ExtractTextAsync; their synchronous counterparts throw for an active stream source. The document model, XRef and raw-file streams are valid only during that load; released access throws ObjectDisposedException.

This example is for a file accessible to the server. FromUrl keeps its buffered behavior; add WithRangeLoading() for opt-in HTTP Range loading. Form fields and tagged structure are deferred until LoadDocumentDetailsAsync(). Browser uploads keep the FromStreamAsync snapshot path because IBrowserFile streams are not seekable and do not support synchronous reads. The viewer coordinates copies with rendering and asynchronous helpers. In a browser, await a copy before reading Document or its raw streams directly.

HTTP Range loading

Keep the URL source in a field and add WithRangeLoading() to request PDF bytes as needed. A first page can appear before the complete file downloads; later pages load during navigation. The default URL source still downloads the complete file. A linearized PDF can reduce startup work; validated page and shared-object hints prefetch related bytes. Invalid or stale hints use ordinary Range loading. Page-tree metadata is fetched in bounded batches for distant navigation. The layout and dependencies of each file still determine how many ranges are needed.

CacheSize defaults to 16 MiB of raw bytes, including pinned windows and in-flight buffers. Each page's raw working set, including its largest single stream, must fit in this budget. ChunkSize is at least 4 KiB and defaults to 64 KiB. Decoded resources and explicit caller-owned copies are separate. MaxFallbackSize defaults to null (unlimited); zero disables buffered fallback. Its declared and actual byte limits apply before publication. A first full response is reused for fallback.

Server requirements:
  • Byte 206 responses with a correct Content-Range and total length.
  • A quoted strong ETag, identical on every server/CDN node serving the same bytes.
  • No HTTP content compression that changes byte coordinates.
  • For cross-origin WebAssembly, expose Content-Range and ETag through Access-Control-Expose-Headers. Allow Range, If-Range and any Authorization header in the CORS preflight.
Every later request sends the exact strong tag as If-Range, and every response is validated. Missing, weak or inaccessible validators cause a safe initial full download. Last-Modified dates are not used as validators. The viewer uses the registered HttpClient and the source's headers. After publication, a failed range reports an error; a version change discards the document and raises OnError. Assign a new source to reload it.

OnDocumentLoaded means the document is open and loading has cleared. A page that cannot render reports OnError separately. The title is available then; bookmarks, layers and page labels load in the background after the first page. Properties loads only metadata/XMP. LoadDocumentDetailsAsync() loads all deferred details, including forms and tagged structure; attachments scan when their panel opens or details are requested. Deferred viewer properties return null/empty and never start network reads. Direct Document reads require cached bytes and otherwise throw BitPdfMissingDataException. Use the async rendering/text helpers.

The initial Range loading bar is indeterminate. OnProgress retains its full-download meaning and only runs on buffered fallback. GetBytes() returns null in Range mode; GetBytesAsync() returns an unretained caller-owned copy. Download and whole-document search/print can acquire the remaining bytes. Copies stream outside the page cache. A whole-file copy accepts a full 200 response only from byte zero, with the same strong ETag and exact length; shorter 206 responses continue from the next byte with the same validator.

During prerendering, a URL reachable by the server uses the same Range path to render the current page. A browser-only URL keeps the loading state until interactive rendering can fetch it.



0 page(s).
100%
No document loaded.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.