Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPdf.pb"

Procedure ChilkatExample()

    success.i = 0

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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/embedded_files/my_embedded_files_test.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

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

    ; How many embedded files exist within the opened PDF?
    numFiles.i = CkPdf::ckNumEmbeddedFiles(pdf)
    Debug "Number of embedded files: " + Str(numFiles)

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)

    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

        success = CkPdf::ckGetEmbeddedFileInfo(pdf,i,json)
        If success = 0
            Debug CkPdf::ckLastErrorText(pdf)
            CkPdf::ckDispose(pdf)
            CkJsonObject::ckDispose(json)
            CkBinData::ckDispose(bd)
            ProcedureReturn
        EndIf

        Debug CkJsonObject::ckEmit(json)

        ; Get the filename from the JSON.
        filename.s = "someFile.dat"

        ; The filename SHOULD always be present..
        If CkJsonObject::ckHasMember(json,"filename") = 1
            filename = CkJsonObject::ckStringOf(json,"filename")
        EndIf

        ; Get the file data.
        success = CkPdf::ckGetEmbeddedFileBd(pdf,i,bd)
        If success = 0
            Debug CkPdf::ckLastErrorText(pdf)
            CkPdf::ckDispose(pdf)
            CkJsonObject::ckDispose(json)
            CkBinData::ckDispose(bd)
            ProcedureReturn
        EndIf

        ; Save the contents of the bd to the filename (in the current working directory) in the filesystem.
        success = CkBinData::ckWriteFile(bd,filename)
        If success = 0
            Debug "Failed to write output file."
        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'"
    ; }


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure