Thanks for Downloading Dynamic Web TWAIN 30-Day Trial!
Your download will start shortly. If your download does not begin, click here to retry.
Addon
How can I prompt users for a password when loading a password-protected PDF?
Dynamic Web TWAIN does not provide a built-in password prompt UI for encrypted PDFs.
You need to handle the prompt and retry flow in your application code.
Recommended workflow (LoadImage() / LoadImageEx())
- Call
LoadImage()orLoadImageEx(). - In the failure callback, treat the error as “password required” only when either condition is true:
- (
errorCodeis-1119anderrorStringcontains
"Failed to read the PDF file because it's encrypted and the correct password is not provided.") - OR
errorCodeis-1120.
- (
- Prompt the user for a password.
- Set the password via
SetReaderOptions(). - Retry loading the same PDF.
Helper function for password-required errors
Use this helper in both API-based loading and drag-and-drop handling:
function isPasswordRequired(errorCode, errorString) {
return (
(errorCode === -1119 &&
(errorString || "").includes(
"Failed to read the PDF file because it's encrypted and the correct password is not provided.",
)) ||
errorCode === -1120
);
}
LoadImage() / LoadImageEx() example
function loadProtectedPdf(path, password) {
DWTObject.Addon.PDF.SetReaderOptions({ password: password || "" });
DWTObject.LoadImageEx(
path,
Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF,
function () {
console.log("PDF loaded successfully.");
},
function (errorCode, errorString) {
if (!isPasswordRequired(errorCode, errorString)) {
console.error(errorCode, errorString);
return;
}
var userPassword = window.prompt(
"This PDF is password-protected. Please enter the password:",
"",
);
if (!userPassword) return;
loadProtectedPdf(path, userPassword);
},
);
}
Drag-and-drop workflow (OnPostLoad)
Use OnPostLoad and check DWTObject.ErrorCode / DWTObject.ErrorString.
DWTObject.RegisterEvent("OnPostLoad", function (path, name, type) {
if (!isPasswordRequired(DWTObject.ErrorCode, DWTObject.ErrorString)) return;
var userPassword = window.prompt(
"This PDF is password-protected. Please enter the password:",
"",
);
if (!userPassword) return;
DWTObject.Addon.PDF.SetReaderOptions({ password: userPassword });
if (!path) {
alert("Password saved. Please drag and drop the file again.");
return;
}
var fullPath = path + "\\" + name;
loadProtectedPdf(fullPath, userPassword);
});
-1119can represent multiple PDF load issues, so do not use it alone. Starting in Dynamic Web TWAIN 19.4, encrypted-PDF load failures will use-1120.In
OnPostLoad,pathis empty for drag-and-drop files, so the code cannot directly retry with a file path. In this case, prompt for password, save it withSetReaderOptions(), and ask the user to drag and drop the file again.
Original post creation date: Apr 09, 2026
Last modified date: Apr 09, 2026