Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL nNumFiles
LOCAL oJson
LOCAL oBd
LOCAL i
LOCAL cFilename
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/embedded_files/my_embedded_files_test.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
// Note: The embedded file functionality was added in Chilkat v9.5.0.95
// How many embedded files exist within the opened PDF?
nNumFiles := oPdf:NumEmbeddedFiles
? "Number of embedded files: " + Str(nNumFiles)
oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0
oBd := CreateObject("Chilkat.BinData")
// Get information about each file, and extract each to the filesystem.
i := 0
DO WHILE i < nNumFiles
nSuccess := oPdf:GetEmbeddedFileInfo(i, oJson)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oBd:destroy()
RETURN
ENDIF
? oJson:Emit()
// Get the filename from the JSON.
cFilename := "someFile.dat"
// The filename SHOULD always be present..
IF (oJson:HasMember("filename") == 1)
cFilename := oJson:StringOf("filename")
ENDIF
// Get the file data.
nSuccess := oPdf:GetEmbeddedFileBd(i, oBd)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oBd:destroy()
RETURN
ENDIF
// Save the contents of the bd to the filename (in the current working directory) in the filesystem.
nSuccess := oBd:WriteFile(cFilename)
IF (nSuccess == 0)
? "Failed to write output file."
ENDIF
i := i + 1
ENDDO
// 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'"
// }
oPdf:destroy()
oJson:destroy()
oBd:destroy()