Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract OLE Object (pdf) from Access DB

Tags:

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.

like image 792
Nate Avatar asked Jun 22 '09 20:06

Nate


People also ask

How do I export an OLE Object from Access?

If you already have an OLE object embedded in the field, the right-click menu will allow you to open the object in its corresponding application in a separate window, then you can manually save the document within that application. Export done!

What is OLE Object in Access database?

Earlier versions of Access used a technology called Object Linking and Embedding (OLE) to store images and documents. By default, OLE created a bitmap equivalent of the image or document. Those bitmap files could become quite large — as much as 10 times larger than the original file.


1 Answers

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.

like image 128
Nate Avatar answered Oct 13 '22 23:10

Nate