views:

1334

answers:

4

We are upgrading/converting several old Access databases to MS-SQL. Many of these databases have OLE Object fields that store PDF files. I'm looking for a way to extract these files and store them in our SQL database. I've seen similar questions that answer how you might do this with image files (jpg, bmp, gif, etc) but I haven't found a way that works with PDF.

+1  A: 

OLEtoDisk

"This version saves the entire contents of a table containing OLE Objects to disk. Does NOT require the original application that served as the OLE server to insert the object. Supports all MS Office documents, PDF, All images inserted by MS Photo Editor, MS Paint, and Paint Shop Pro. Also supports extraction of PACKAGE class including original Filename. Contains function to produce a full Inventory of the OLE field including LINKED path and Filenames. Uses Structured Storage API's to read the actual contents of the field"

http://lebans.com/oletodisk.htm

Tony Toews
I've seen (and tried) that. It works to pull out the PDFs but I am trying to find something that I can integrate into my own (c#) code. Some of these Access DBs have 4+ columns that store PDF files and ultimately, I just want to copy the file into a table on our SQL server with all of the other data.
Nate
Ah, you never said you were looking for C# code.
Tony Toews
A: 

I finally got some code working for what I want it to do. The trick is determining what part is the OLE Header and removing it. Here is what is working for me (based on code found here)

    public static byte[] StripOleHeader(byte[] fileData)
    {
        const string START_BLOCK = "%PDF-1.3";
        int startPos = -1;

        Encoding u8 = Encoding.UTF7;
        string strEncoding = u8.GetString(fileData);

        if (strEncoding.IndexOf(START_BLOCK) != -1)
        {
            startPos = strEncoding.IndexOf(START_BLOCK);
        }

        if (startPos == -1)
        {
            throw new Exception("Could not find PDF Header");
        }

        byte[] retByte = new byte[fileData.LongLength - startPos];

        Array.Copy(fileData, startPos, retByte, 0, fileData.LongLength - startPos);

        return retByte;
    }

Note that this only works for PDF files.

Nate
+1  A: 

Hello! You can try to use free .NET library Intasphere (http://www.intasphere.ru/en/oleextract.php). It has function to extract different type of data from MS Access OLE fields.

Sergey
A: 

Hi Nate,

I'm recently doing a project that involves extracting pdf files that are attached as ole objects in MS Access to a file path of choice.

I followed the guide at http://msdn.microsoft.com/en-us/library/87z0hy49(VS.80).aspx and managed to copy the file out to a file path, but the pdf file is corrupted.

I am finding it hard to understand the code you wrote and was wondering if you could elaborate further on it.

What is "fileData" and "retByte" and how do I tie it to the code in the link above?

Hope you can help me and I would truly appreciate it.

Best regards,

Chris

Chris
File data is the contents of the ole object. retByte is the pdf file that you can save off with File.WriteAllBytes
Nate