|
| 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 implement your own prompt flow in application code. |
| 19 | + |
| 20 | +### Recommended flow for `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, check whether `errorCode` is `-1119`. |
| 24 | +3. Prompt the user for a password. |
| 25 | +4. Call [`SetReaderOptions()`](/_articles/info/api/Addon_PDF.md#setreaderoptions) with that password. |
| 26 | +5. Retry loading the same PDF. |
| 27 | + |
| 28 | +```javascript |
| 29 | +function loadProtectedPdf(path, password) { |
| 30 | + DWTObject.Addon.PDF.SetReaderOptions({ password: password || "" }); |
| 31 | + DWTObject.LoadImageEx( |
| 32 | + path, |
| 33 | + Dynamsoft.DWT.EnumDWT_ImageType.IT_PDF, |
| 34 | + function () { |
| 35 | + console.log("PDF loaded successfully."); |
| 36 | + }, |
| 37 | + function (errorCode, errorString) { |
| 38 | + if (errorCode !== -1119) { |
| 39 | + console.error(errorCode, errorString); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + var userPassword = window.prompt( |
| 44 | + "This PDF is password-protected. Please enter the password:", |
| 45 | + "", |
| 46 | + ); |
| 47 | + if (!userPassword) return; |
| 48 | + |
| 49 | + loadProtectedPdf(path, userPassword); |
| 50 | + }, |
| 51 | + ); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### For drag-and-drop files |
| 56 | + |
| 57 | +You can detect load failures in [`OnPostLoad`](/_articles/info/api/WebTwain_IO.md#onpostload) by checking `DWTObject.ErrorCode` and `DWTObject.ErrorString`. |
| 58 | + |
| 59 | +```javascript |
| 60 | +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. |
| 64 | + } |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +> [!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)). |
0 commit comments