Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/embedded_files/my_embedded_files_test.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

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

    -- How many embedded files exist within the opened PDF?
    DECLARE @numFiles int
    EXEC sp_OAGetProperty @pdf, 'NumEmbeddedFiles', @numFiles OUT

    PRINT 'Number of embedded files: ' + @numFiles

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OASetProperty @json, 'EmitCompact', 0

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    -- Get information about each file, and extract each to the filesystem.
    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numFiles
      BEGIN

        EXEC sp_OAMethod @pdf, 'GetEmbeddedFileInfo', @success OUT, @i, @json
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @pdf
            EXEC @hr = sp_OADestroy @json
            EXEC @hr = sp_OADestroy @bd
            RETURN
          END

        EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
        PRINT @sTmp0

        -- Get the filename from the JSON.
        DECLARE @filename nvarchar(4000)
        SELECT @filename = 'someFile.dat'

        -- The filename SHOULD always be present..
        EXEC sp_OAMethod @json, 'HasMember', @iTmp0 OUT, 'filename'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'filename'
          END

        -- Get the file data.
        EXEC sp_OAMethod @pdf, 'GetEmbeddedFileBd', @success OUT, @i, @bd
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @pdf
            EXEC @hr = sp_OADestroy @json
            EXEC @hr = sp_OADestroy @bd
            RETURN
          END

        -- Save the contents of the bd to the filename (in the current working directory) in the filesystem.
        EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, @filename
        IF @success = 0
          BEGIN

            PRINT 'Failed to write output file.'
          END

        SELECT @i = @i + 1
      END

    -- 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'"
    -- }

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @bd


END
GO