Skip to content

Commit 03334fc

Browse files
authored
Merge pull request #993 from dynamsoft-docs/docs-faq-password-protected-pdf-loading
docs(faq): add password-protected PDF loading guidance
2 parents 0338844 + 61370e7 commit 03334fc

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

_articles/faq/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ description: Dynamic Web TWAIN SDK Documentation FAQ
142142
8. [How can I separate my documents automatically by barcode?](/_articles/faq/separate-documents-by-barcode.md)
143143
9. [Is there any way to speed up the barcode reading process?](/_articles/faq/speed-up-barcode-reading-process.md)
144144
10. [What types of webcams does the Webcam Capture addon support](/_articles/faq/webcam-supported-by-webcam-capture-addon.md)
145+
11. [How can I prompt users for a password when loading a password-protected PDF?](/_articles/faq/prompt-password-for-protected-pdf.md)
145146

146147
## Project Deployment and End-user Installation
147148

@@ -228,4 +229,4 @@ description: Dynamic Web TWAIN SDK Documentation FAQ
228229

229230
## Annotation
230231

231-
1. [How can I add annotations to an image and then save/upload it?](/_articles/faq/dwt-with-annotation.md)
232+
1. [How can I add annotations to an image and then save/upload it?](/_articles/faq/dwt-with-annotation.md)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
layout: default-layout
3+
noTitleIndex: true
4+
needAutoGenerateSidebar: true
5+
title: How can I prompt users for a password when loading a password-protected PDF?
6+
keywords: Dynamic Web TWAIN, Addon, PDF, password, encrypted, LoadImageEx, OnPostLoad
7+
breadcrumbText: How can I prompt users for a password when loading a password-protected PDF?
8+
description: How can I prompt users for a password when loading a password-protected PDF?
9+
date: 2026-04-09 10:00:00 +0800
10+
last_modified: 2026-04-09 10:00:00 +0800
11+
---
12+
13+
# Addon
14+
15+
## How can I prompt users for a password when loading a password-protected PDF?
16+
17+
Dynamic Web TWAIN does not provide a built-in password prompt UI for encrypted PDFs.
18+
You need to handle the prompt and retry flow in your application code.
19+
20+
### Recommended workflow (`LoadImage()` / `LoadImageEx()`)
21+
22+
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, 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`.
27+
3. Prompt the user for a password.
28+
4. Set the password via [`SetReaderOptions()`](/_articles/info/api/Addon_PDF.md#setreaderoptions).
29+
5. Retry loading the same PDF.
30+
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+
49+
```javascript
50+
function loadProtectedPdf(path, password) {
51+
DWTObject.Addon.PDF.SetReaderOptions({ password: password || "" });
52+
DWTObject.LoadImageEx(
53+
path,
54+
Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF,
55+
function () {
56+
console.log("PDF loaded successfully.");
57+
},
58+
function (errorCode, errorString) {
59+
if (!isPasswordRequired(errorCode, errorString)) {
60+
console.error(errorCode, errorString);
61+
return;
62+
}
63+
64+
var userPassword = window.prompt(
65+
"This PDF is password-protected. Please enter the password:",
66+
"",
67+
);
68+
if (!userPassword) return;
69+
70+
loadProtectedPdf(path, userPassword);
71+
},
72+
);
73+
}
74+
```
75+
76+
### Drag-and-drop workflow (`OnPostLoad`)
77+
78+
Use [`OnPostLoad`](/_articles/info/api/WebTwain_IO.md#onpostload) and check `DWTObject.ErrorCode` / `DWTObject.ErrorString`.
79+
80+
```javascript
81+
DWTObject.RegisterEvent("OnPostLoad", function (path, name, type) {
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;
95+
}
96+
97+
var fullPath = path + "\\" + name;
98+
loadProtectedPdf(fullPath, userPassword);
99+
});
100+
```
101+
102+
> [!NOTE]
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)