Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@if (showPdfViewer)
{
<PdfViewer Class="mb-3" Url="@($"../{DemoStringConstants.StaticAssets_Docs_Path}/pdf_password_protected.pdf")" PromptForPassword="true" />
<PdfViewer Class="mb-3"
Url="@($"../{DemoStringConstants.StaticAssets_Docs_Path}/pdf_password_protected.pdf")"
OnDocumentLoadError="OnDocumentLoadError" />
}
else
{
Expand All @@ -9,4 +11,6 @@ else

@code {
private bool showPdfViewer = false;

private void OnDocumentLoadError(string errorMessage) => showPdfViewer = false;
}
15 changes: 15 additions & 0 deletions blazorbootstrap/wwwroot/blazor.bootstrap.pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,30 @@ export function initialize(dotNetHelper, elementId, scale, rotation, url, passwo

// begin loading document
const loadingTask = pdfJS.getDocument(options);
let passwordPromptCanceled = false;

// handle password only when required (optional password support)
loadingTask.onPassword = function (updatePassword, reason) {
if (passwordPromptCanceled) return;

if (reason === pdfJS.PasswordResponses.NEED_PASSWORD) {
// only prompt if PDF actually requires password
const password = prompt("This PDF is password protected. Enter password:");
if (password === null) {
passwordPromptCanceled = true;
loadingTask.destroy();
dotNetHelper.invokeMethodAsync('DocumentLoadError', "Password prompt canceled.");
return;
}
updatePassword(password);
} else if (reason === pdfJS.PasswordResponses.INCORRECT_PASSWORD) {
const password = prompt("Incorrect password. Please try again:");
if (password === null) {
passwordPromptCanceled = true;
loadingTask.destroy();
dotNetHelper.invokeMethodAsync('DocumentLoadError', "Password prompt canceled.");
return;
}
updatePassword(password);
}
};
Expand Down
Loading