電子簽名是手寫簽名的數位形式,允許個人以電子方式簽署文件。它已成為企業和組織簡化簽名流程的重要工具,只需在世界任何地方單擊幾下即可簽署文件。在本文中,我們將探討如何使用 HTML5、JavaScript 和 Dynamsoft Document Viewer 將簽章插入 PDF 文件。
示範影片:使用電子簽名簽署 PDF 文檔
線上示範
https://yushulx.me/web-document-annotation/
先決條件
Dynamsoft Capture Vision 試用許可證:取得 30 天免費試用許可證以解鎖 Dynamsoft 產品的全部功能。
Dynamsoft 文件檢視器:此JavaScript SDK 可無縫檢視PDF、JPEG、PNG、 > TIFF 和BMP 檔。它還具有 PDF 註釋渲染和保存功能。下載連結:https://www.npmjs.com/package/dynamsoft-document-viewer。
實作Web PDF編輯器的簽章功能
我們的 Web 文件註解項目是使用 Dynamsoft Document Viewer 建構的,具有高度可擴展性。目前它包括 PDF 文件的條碼檢測。在以下部分中,我們將逐步引導您完成在 PDF 文件中新增電子簽名的過程。
第 1 步:取得原始碼
-
從 GitHub 儲存庫複製原始碼:
git clone https://github.com/yushulx/web-twain-document-scan-management.git
-
導覽至 document_annotation 目錄:
cd web-twain-document-scan-management/examples/document_annotation
在 Visual Studio Code 中開啟專案。
第 2 步:新增簽名按鈕
-
在main.css中,為簽名按鈕新增材質圖示:
.icon-stylus::before { content: "edit"; } .icon-stylus { display: flex; font-size: 1.5em; }
-
定義簽名按鈕並將其新增至main.js中的工具列:
const signatureButton = { type: Dynamsoft.DDV.Elements.Button, className: "material-icons icon-stylus", tooltip: "Sign the document", events: { click: "sign", } } const pcEditViewerUiConfig = { type: Dynamsoft.DDV.Elements.Layout, flexDirection: "column", className: "ddv-edit-viewer-desktop", children: [ { type: Dynamsoft.DDV.Elements.Layout, className: "ddv-edit-viewer-header-desktop", children: [ { type: Dynamsoft.DDV.Elements.Layout, children: [ Dynamsoft.DDV.Elements.ThumbnailSwitch, Dynamsoft.DDV.Elements.Zoom, Dynamsoft.DDV.Elements.FitMode, Dynamsoft.DDV.Elements.Crop, Dynamsoft.DDV.Elements.Filter, Dynamsoft.DDV.Elements.Undo, Dynamsoft.DDV.Elements.Redo, Dynamsoft.DDV.Elements.DeleteCurrent, Dynamsoft.DDV.Elements.DeleteAll, Dynamsoft.DDV.Elements.Pan, Dynamsoft.DDV.Elements.AnnotationSet, qrButton, checkButton, scanButton, clearButton, signatureButton, ], }, { type: Dynamsoft.DDV.Elements.Layout, children: [ { type: Dynamsoft.DDV.Elements.Pagination, className: "ddv-edit-viewer-pagination-desktop", }, loadButton, downloadButton, ], }, ], }, Dynamsoft.DDV.Elements.MainView, ], };
-
新增簽名按鈕的點擊事件處理程序:
editViewer.on("sign", sign); function sign() { ... }
步驟 3:建立用於簽名輸入的彈出對話框
簽章輸入的彈出對話框包含以下元素:
- 用來繪製簽名的畫布元素。
- 用於更改簽名顏色的顏色選項。
- 用來調整簽名粗細的筆畫滑桿。
- 用於定位簽名的 X 和 Y 座標。
- 用於將簽名插入 PDF 文件的儲存按鈕。
- 用於關閉對話框的取消按鈕。
- 用於擦除簽名的重畫按鈕。
HTML 代碼
git clone https://github.com/yushulx/web-twain-document-scan-management.git
為滑鼠事件新增事件監聽器:
cd web-twain-document-scan-management/examples/document_annotation
新增顏色和描邊選項的事件偵聽器:
.icon-stylus::before { content: "edit"; } .icon-stylus { display: flex; font-size: 1.5em; }
實現繪圖功能:
const signatureButton = { type: Dynamsoft.DDV.Elements.Button, className: "material-icons icon-stylus", tooltip: "Sign the document", events: { click: "sign", } } const pcEditViewerUiConfig = { type: Dynamsoft.DDV.Elements.Layout, flexDirection: "column", className: "ddv-edit-viewer-desktop", children: [ { type: Dynamsoft.DDV.Elements.Layout, className: "ddv-edit-viewer-header-desktop", children: [ { type: Dynamsoft.DDV.Elements.Layout, children: [ Dynamsoft.DDV.Elements.ThumbnailSwitch, Dynamsoft.DDV.Elements.Zoom, Dynamsoft.DDV.Elements.FitMode, Dynamsoft.DDV.Elements.Crop, Dynamsoft.DDV.Elements.Filter, Dynamsoft.DDV.Elements.Undo, Dynamsoft.DDV.Elements.Redo, Dynamsoft.DDV.Elements.DeleteCurrent, Dynamsoft.DDV.Elements.DeleteAll, Dynamsoft.DDV.Elements.Pan, Dynamsoft.DDV.Elements.AnnotationSet, qrButton, checkButton, scanButton, clearButton, signatureButton, ], }, { type: Dynamsoft.DDV.Elements.Layout, children: [ { type: Dynamsoft.DDV.Elements.Pagination, className: "ddv-edit-viewer-pagination-desktop", }, loadButton, downloadButton, ], }, ], }, Dynamsoft.DDV.Elements.MainView, ], };
所有路徑都儲存在drawingHistory數組中。 redrawCanvas 函數會迭代數組並在畫布上重繪路徑。
為「確定」、「重繪」和「取消」按鈕新增事件偵聽器:
editViewer.on("sign", sign); function sign() { ... }
步驟 4:將簽名插入文檔
要將簽章插入 PDF 文件:
-
將畫布轉換為斑點:
<div> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173421499729336.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How to Insert Signatures into PDF Documents with HTMLnd JavaScript"></p> <p><strong>JavaScript code for the signature input dialog</strong></p> <ol> <li> <p>Initialize the canvas and drawing context:<br> </p> <pre class="brush:php;toolbar:false">let canvas = document.getElementById("signatureCanvas"); let ctx = canvas.getContext("2d"); let isDrawing = false; let color = "black"; let strokeWidth = 3; let drawingHistory = []; canvas.width = 500; canvas.height = 300;
-
取得目前頁面ID和頁面資料:
canvas.addEventListener("mousedown", startDrawing); canvas.addEventListener("mousemove", draw); canvas.addEventListener("mouseup", stopDrawing); canvas.addEventListener("mouseout", stopDrawing);
-
使用簽名影像建立新的圖章註解:
document.getElementById("blue").addEventListener("click", () => { color = "blue"; redrawCanvas(); }); document.getElementById("red").addEventListener("click", () => { color = "red"; redrawCanvas(); }); document.getElementById("black").addEventListener("click", () => { color = "black"; redrawCanvas(); }); document.getElementById("strokeSlider").addEventListener("input", (e) => { strokeWidth = e.target.value; redrawCanvas(); });
原始碼
https://github.com/yushulx/web-twain-document-scan-management/tree/main/examples/document_annotation
以上是如何使用 HTMLnd JavaScript 將簽名插入 PDF 文檔的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3漢化版
中文版,非常好用