Sample code for 30+ languages & platforms
AutoIt

Extract Embedded Files from PDF

Demonstrates how to get information about the embedded files (if any) contained within a PDF, and shows how to extract each file.

Note: This example requires Chilkat v9.5.0.95 or greater.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oPdf = ObjCreate("Chilkat.Pdf")

$bSuccess = $oPdf.LoadFile("qa_data/pdf/embedded_files/my_embedded_files_test.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

; Note: The embedded file functionality was added in Chilkat v9.5.0.95

; How many embedded files exist within the opened PDF?
Local $iNumFiles = $oPdf.NumEmbeddedFiles
ConsoleWrite("Number of embedded files: " & $iNumFiles & @CRLF)

$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False

$oBd = ObjCreate("Chilkat.BinData")

; Get information about each file, and extract each to the filesystem.
Local $i = 0
While $i < $iNumFiles

    $bSuccess = $oPdf.GetEmbeddedFileInfo($i,$oJson)
    If ($bSuccess = False) Then
        ConsoleWrite($oPdf.LastErrorText & @CRLF)
        Exit
    EndIf

    ConsoleWrite($oJson.Emit() & @CRLF)

    ; Get the filename from the JSON.
Local $sFilename = "someFile.dat"

    ; The filename SHOULD always be present..
    If ($oJson.HasMember("filename") = True) Then
        $sFilename = $oJson.StringOf("filename")
    EndIf

    ; Get the file data.
    $bSuccess = $oPdf.GetEmbeddedFileBd($i,$oBd)
    If ($bSuccess = False) Then
        ConsoleWrite($oPdf.LastErrorText & @CRLF)
        Exit
    EndIf

    ; Save the contents of the bd to the filename (in the current working directory) in the filesystem.
    $bSuccess = $oBd.WriteFile($sFilename)
    If ($bSuccess = False) Then
        ConsoleWrite("Failed to write output file." & @CRLF)
    EndIf

    $i = $i + 1
Wend

; Sample output for the above code:

; Number of embedded files: 3
; {
;   "filename": "employees.json",
;   "desc": "JSON",
;   "subType": "application/json",
;   "size": 159,
;   "creationDate": "D:20230715170506-05'00'",
;   "modDate": "D:20160207153838-05'00'"
; }
; 
; {
;   "filename": "rsaPubKey.pem",
;   "desc": "RSA Public Key PEM",
;   "size": 451,
;   "creationDate": "D:20230715170546-05'00'",
;   "modDate": "D:20150724133153-05'00'"
; }
; 
; {
;   "filename": "starfish.jpg",
;   "desc": "Starfish JPG",
;   "subType": "image/jpeg",
;   "size": 6229,
;   "creationDate": "D:20230715170356-05'00'",
;   "modDate": "D:20080529103055-05'00'"
; }