Skip to content

Commit 184c3a9

Browse files
authored
v1.4.3
1 parent 3059588 commit 184c3a9

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

examples/v1.4.3_Demo.au3

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#include <GUIConstantsEx.au3>
3+
#include <WindowsConstants.au3>
4+
#include "..\NetWebView2Lib.au3"
5+
6+
; CoreWebView2PdfToolbarItems Enumeration (Bitwise flags)
7+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_NONE = 0
8+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_SAVE = 1
9+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_PRINT = 2
10+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_SAVE_AS = 4
11+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_ZOOM = 8
12+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_PAGE_LAYOUT = 16
13+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_FULL_SCREEN = 32
14+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_MORE_SETTINGS = 64
15+
Global Const $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_SEARCH = 128
16+
17+
Global $hGUI, $idLabelStatus
18+
19+
Main()
20+
21+
Func Main()
22+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
23+
#forceref $oMyError
24+
25+
; === GUI Creation ===
26+
Local $iHeight = 800
27+
Local $sDefTitle = "WebView2 .NET Manager - v1.4.3 Demo"
28+
$hGUI = GUICreate($sDefTitle, 1000, $iHeight, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
29+
GUISetState(@SW_SHOW)
30+
31+
; === WebView2 Initialization ===
32+
Local $oWebV2M = _NetWebView2_CreateManager()
33+
$_g_oWeb = $oWebV2M
34+
If @error Then Return SetError(@error, @extended, $oWebV2M)
35+
36+
Local $sProfileDirectory = @TempDir & "\NetWebView2Lib-UserDataFolder"
37+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, True, 1.2, "0x2B2B2B")
38+
39+
; ### STEP 1: Navigating to AutoIt...
40+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 1: Navigating to AutoIt...")
41+
Local $sUrl = "https://www.autoitscript.com"
42+
_NetWebView2_Navigate($oWebV2M, $sUrl)
43+
44+
Sleep(2000)
45+
46+
; ### STEP 2: Creating MHTML Web Archive...
47+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 2: Creating MHTML Web Archive...")
48+
Local $sMhtmlPath = @ScriptDir & "\AutoIt_archive.mhtml"
49+
; Exports the current page data as HTML (0) or MHTML (1).
50+
; If FilePath is provided, it saves to disk; otherwise, it returns the content as a string.
51+
Local $sDllResponse = $oWebV2M.ExportPageData(1, $sMhtmlPath)
52+
ConsoleWrite("! DLL Response: " & $sDllResponse & @CRLF)
53+
; Check if DLL actually reported success
54+
If Not StringInStr($sDllResponse, "SUCCESS") Then ConsoleWrite("! Critical Error: DLL failed to export MHTML." & @CRLF)
55+
56+
Sleep(2000)
57+
58+
; ### STEP 3: Capturing PDF stream to RAM (Base64)...
59+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 3: Capturing PDF stream to RAM (Base64)...")
60+
;Captures the current page as a PDF and returns the content as a Base64-encoded string.
61+
Local $sBase64PDF = $oWebV2M.PrintToPdfStream()
62+
63+
Sleep(2000)
64+
65+
; ### STEP 4: Exporting traditional PDF file...
66+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 4: Exporting traditional PDF file...")
67+
Local $sClassicPdfPath = @ScriptDir & "\AutoIt_traditional.pdf"
68+
; Saves the current page as a PDF file.
69+
$oWebV2M.ExportToPdf($sClassicPdfPath)
70+
71+
Sleep(2000)
72+
73+
; ### STEP 5: Loading protected PDF (Toolbar buttons hidden)...
74+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 5: Loading protected PDF (Toolbar buttons hidden)...")
75+
Local $iHiddenItems = $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_SAVE + $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_SAVE_AS + $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_PRINT
76+
; Controls the visibility of buttons in the PDF viewer toolbar using a bitwise combination of CoreWebView2PdfToolbarItems (e.g., 1=Save, 2=Print, 4=Search).
77+
$oWebV2M.HiddenPdfToolbarItems = $iHiddenItems
78+
Local $sLocalPdfUrl = "file:///" & StringReplace($sClassicPdfPath, "\", "/")
79+
_NetWebView2_Navigate($oWebV2M, $sLocalPdfUrl)
80+
81+
Sleep(5000)
82+
83+
; ### STEP 6: Show all buttons in the PDF viewer toolbar...
84+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 6: Show all buttons in the PDF viewer toolbar...")
85+
$iHiddenItems = $COREWEBVIEW2_PDF_TOOLBAR_ITEMS_NONE
86+
; Show all buttons in the PDF viewer toolbar
87+
$oWebV2M.HiddenPdfToolbarItems = $iHiddenItems
88+
$oWebV2M.Reload()
89+
90+
Sleep(5000)
91+
92+
; ### STEP 7: Load the MHTML Archive we created in Step 2
93+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 7: Load the MHTML Archive we created in Step 2")
94+
Local $sMhtmlUrl = "file:///" & StringReplace($sMhtmlPath, "\", "/")
95+
_NetWebView2_Navigate($oWebV2M, $sMhtmlUrl)
96+
97+
Sleep(5000)
98+
99+
; ### STEP 8: Clear view and Displaying recovered PDF from memory...
100+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & ">>> STEP 8: Clear view and Displaying recovered PDF from memory...")
101+
_NetWebView2_Navigate($oWebV2M, "about:blank")
102+
Local $oJson = _NetJson_CreateParser()
103+
Local $sRecoveredPath = @ScriptDir & "\AutoIt_From_Memory.pdf"
104+
$oJson.DecodeB64ToFile($sBase64PDF, $sRecoveredPath)
105+
Local $sRecoveredUrl = "file:///" & StringReplace($sRecoveredPath, "\", "/")
106+
_NetWebView2_Navigate($oWebV2M, $sRecoveredUrl)
107+
108+
Sleep(5000)
109+
110+
WinSetTitle($hGUI, "", $sDefTitle & @TAB & "Tested Successfully!")
111+
112+
; === Main Loop ===
113+
While 1
114+
Switch GUIGetMsg()
115+
Case $GUI_EVENT_CLOSE
116+
ExitLoop
117+
EndSwitch
118+
WEnd
119+
120+
GUIDelete($hGUI)
121+
$oWebV2M.Cleanup()
122+
EndFunc ;==>Main

0 commit comments

Comments
 (0)