Skip to content

Commit 61370e7

Browse files
committed
docs(faq): clarify encrypted PDF error handling and drag-drop flow
1 parent 8cfd84d commit 61370e7

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

_articles/faq/prompt-password-for-protected-pdf.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,37 @@ last_modified: 2026-04-09 10:00:00 +0800
1515
## How can I prompt users for a password when loading a password-protected PDF?
1616

1717
Dynamic Web TWAIN does not provide a built-in password prompt UI for encrypted PDFs.
18-
You need to implement your own prompt flow in application code.
18+
You need to handle the prompt and retry flow in your application code.
1919

20-
### Recommended flow for `LoadImage()` / `LoadImageEx()`
20+
### Recommended workflow (`LoadImage()` / `LoadImageEx()`)
2121

2222
1. Call [`LoadImage()`](/_articles/info/api/WebTwain_IO.md#loadimage) or [`LoadImageEx()`](/_articles/info/api/WebTwain_IO.md#loadimageex).
23-
2. In the failure callback, check whether `errorCode` is `-1119`.
23+
2. In the failure callback, treat the error as "password required" only when either condition is true:
24+
- (`errorCode` is `-1119` and `errorString` contains
25+
`"Failed to read the PDF file because it's encrypted and the correct password is not provided."`)
26+
- OR `errorCode` is `-1120`.
2427
3. Prompt the user for a password.
25-
4. Call [`SetReaderOptions()`](/_articles/info/api/Addon_PDF.md#setreaderoptions) with that password.
28+
4. Set the password via [`SetReaderOptions()`](/_articles/info/api/Addon_PDF.md#setreaderoptions).
2629
5. Retry loading the same PDF.
2730

31+
### Helper function for password-required errors
32+
33+
Use this helper in both API-based loading and drag-and-drop handling:
34+
35+
```javascript
36+
function isPasswordRequired(errorCode, errorString) {
37+
return (
38+
(errorCode === -1119 &&
39+
(errorString || "").includes(
40+
"Failed to read the PDF file because it's encrypted and the correct password is not provided.",
41+
)) ||
42+
errorCode === -1120
43+
);
44+
}
45+
```
46+
47+
### `LoadImage()` / `LoadImageEx()` example
48+
2849
```javascript
2950
function loadProtectedPdf(path, password) {
3051
DWTObject.Addon.PDF.SetReaderOptions({ password: password || "" });
@@ -35,7 +56,7 @@ function loadProtectedPdf(path, password) {
3556
console.log("PDF loaded successfully.");
3657
},
3758
function (errorCode, errorString) {
38-
if (errorCode !== -1119) {
59+
if (!isPasswordRequired(errorCode, errorString)) {
3960
console.error(errorCode, errorString);
4061
return;
4162
}
@@ -52,20 +73,35 @@ function loadProtectedPdf(path, password) {
5273
}
5374
```
5475

55-
### For drag-and-drop files
76+
### Drag-and-drop workflow (`OnPostLoad`)
5677

57-
You can detect load failures in [`OnPostLoad`](/_articles/info/api/WebTwain_IO.md#onpostload) by checking `DWTObject.ErrorCode` and `DWTObject.ErrorString`.
78+
Use [`OnPostLoad`](/_articles/info/api/WebTwain_IO.md#onpostload) and check `DWTObject.ErrorCode` / `DWTObject.ErrorString`.
5879

5980
```javascript
6081
DWTObject.RegisterEvent("OnPostLoad", function (path, name, type) {
61-
if (DWTObject.ErrorCode === -1119) {
62-
console.log(DWTObject.ErrorString);
63-
// Add your own password prompt flow here.
82+
if (!isPasswordRequired(DWTObject.ErrorCode, DWTObject.ErrorString)) return;
83+
84+
var userPassword = window.prompt(
85+
"This PDF is password-protected. Please enter the password:",
86+
"",
87+
);
88+
if (!userPassword) return;
89+
90+
DWTObject.Addon.PDF.SetReaderOptions({ password: userPassword });
91+
92+
if (!path) {
93+
alert("Password saved. Please drag and drop the file again.");
94+
return;
6495
}
96+
97+
var fullPath = path + "\\" + name;
98+
loadProtectedPdf(fullPath, userPassword);
6599
});
66100
```
67101

68102
> [!NOTE]
69-
> In `OnPostLoad`, `path` is empty for drag-and-drop files.
70-
> If you need a retry flow after entering a password, prompt the user to select the file again
71-
> (for example, by using [`ShowFileDialog()`](/_articles/info/api/WebTwain_IO.md#showfiledialog) + [`LoadImageEx()`](/_articles/info/api/WebTwain_IO.md#loadimageex)).
103+
> `-1119` can represent multiple PDF load issues, so do not use it alone.
104+
> Starting in Dynamic Web TWAIN 19.4, encrypted-PDF load failures will use `-1120`.
105+
>
106+
> In `OnPostLoad`, `path` is empty for drag-and-drop files, so the code cannot directly retry with a file path.
107+
> In this case, prompt for password, save it with `SetReaderOptions()`, and ask the user to drag and drop the file again.

0 commit comments

Comments
 (0)