/// <summary>
        /// Generates an escher record including the any children contained under that record.
        /// An exception is thrown if the record could not be generated.
        /// </summary>
        /// <param name="data">The byte array containing the records</param>
        /// <param name="offset">The starting offset into the byte array</param>
        /// <returns>The generated escher record</returns>
        public virtual EscherRecord CreateRecord(byte[] data, int offset)
        {
            short options = LittleEndian.GetShort(data, offset);
            short recordId = LittleEndian.GetShort(data, offset + 2);

            // int remainingBytes = LittleEndian.getInt( data, offset + 4 );
            // Options of 0x000F means container record
            // However, EscherTextboxRecord are containers of records for the
            //  host application, not of other Escher records, so treat them
            //  differently
            if (IsContainer(options, recordId))
            {
                EscherContainerRecord r = new EscherContainerRecord();
                r.RecordId = recordId;
                r.Options = options;
                return r;
            }
            if (recordId >= EscherBlipRecord.RECORD_ID_START && recordId <= EscherBlipRecord.RECORD_ID_END)
            {
                EscherBlipRecord r;
                if (recordId == EscherBitmapBlip.RECORD_ID_DIB ||
                        recordId == EscherBitmapBlip.RECORD_ID_JPEG ||
                        recordId == EscherBitmapBlip.RECORD_ID_PNG)
                {
                    r = new EscherBitmapBlip();
                }
                else if (recordId == EscherMetafileBlip.RECORD_ID_EMF ||
                        recordId == EscherMetafileBlip.RECORD_ID_WMF ||
                        recordId == EscherMetafileBlip.RECORD_ID_PICT)
                {
                    r = new EscherMetafileBlip();
                }
                else
                {
                    r = new EscherBlipRecord();
                }
                r.RecordId = recordId;
                r.Options = options;
                return r;
            }

            //ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
            ConstructorInfo recordConstructor = null;
            if (recordsMap.ContainsKey(recordId))
                recordConstructor = recordsMap[recordId];

            EscherRecord escherRecord = null;
            if (recordConstructor == null)
            {
                return new UnknownEscherRecord();
            }

            try
            {
                escherRecord = (EscherRecord)recordConstructor.Invoke(new object[] { });
                //escherRecord = (EscherRecord)Activator.CreateInstance(recordConstructor);
            }
            catch (Exception)
            {
                return new UnknownEscherRecord();
            }
            escherRecord.RecordId = recordId;
            escherRecord.Options = options;
            return escherRecord;

        }
Example #2
0
        /// <summary>
        /// Adds a picture to the workbook.
        /// </summary>
        /// <param name="pictureData">The bytes of the picture</param>
        /// <param name="format">The format of the picture.  One of 
        /// PictureType.</param>
        /// <returns>the index to this picture (1 based).</returns>
        public int AddPicture(byte[] pictureData, NPOI.SS.UserModel.PictureType format)
        {
            InitDrawings();

            byte[] uid;
            using (MD5 hasher = MD5.Create())
            {
                uid = hasher.ComputeHash(pictureData);
            }
            EscherBlipRecord blipRecord;
            int blipSize;
            short escherTag;
            switch (format) {
                case PictureType.WMF:
                    // remove first 22 bytes if file starts with magic bytes D7-CD-C6-9A
                    // see also http://de.wikipedia.org/wiki/Windows_Metafile#Hinweise_zur_WMF-Spezifikation
                    if (LittleEndian.GetInt(pictureData) == unchecked((int)0x9AC6CDD7)) {
                        byte[] picDataNoHeader = new byte[pictureData.Length-22];
                        Array.Copy(pictureData, 22, picDataNoHeader, 0, pictureData.Length-22);
                        pictureData = picDataNoHeader;
                    }
                    EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip();
                    blipRecord = blipRecordMeta;
                    blipRecordMeta.UID=(/*setter*/uid);
                    blipRecordMeta.SetPictureData(pictureData);
                    // taken from libre office export, it won't open, if this is left to 0
                    blipRecordMeta.Filter=(/*setter*/unchecked((byte)-2));
                    blipSize = blipRecordMeta.CompressedSize + 58;
                    escherTag = 0;
                    break;
                case PictureType.EMF:
                    blipRecordMeta = new EscherMetafileBlip();
                    blipRecord = blipRecordMeta;
                    blipRecordMeta.UID=(/*setter*/uid);
                    blipRecordMeta.SetPictureData(pictureData);
                    // taken from libre office export, it won't open, if this is left to 0
                    blipRecordMeta.Filter=(/*setter*/unchecked((byte)-2));
                    blipSize = blipRecordMeta.CompressedSize + 58;
                    escherTag = 0;
                    break;
                default:
                    EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip();
                    blipRecord = blipRecordBitmap;
                    blipRecordBitmap.UID=(/*setter*/ uid );
                    blipRecordBitmap.Marker=(/*setter*/ (byte) 0xFF );
                    blipRecordBitmap.PictureData=(pictureData);
                    blipSize = pictureData.Length + 25;
                    escherTag = (short) 0xFF;
    	            break;
            }

            blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format);
            
            switch (format)
            {
                case NPOI.SS.UserModel.PictureType.EMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_EMF;
                    break;
                case NPOI.SS.UserModel.PictureType.WMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_WMF;
                    break;
                case NPOI.SS.UserModel.PictureType.PICT:
                    blipRecord.Options = HSSFPictureData.MSOBI_PICT;
                    break;
                case NPOI.SS.UserModel.PictureType.PNG:
                    blipRecord.Options = HSSFPictureData.MSOBI_PNG;
                    break;
                case NPOI.SS.UserModel.PictureType.JPEG:
                    blipRecord.Options = HSSFPictureData.MSOBI_JPEG;
                    break;
                case NPOI.SS.UserModel.PictureType.DIB:
                    blipRecord.Options = HSSFPictureData.MSOBI_DIB;
                    break;
            }

            EscherBSERecord r = new EscherBSERecord();
            r.RecordId = EscherBSERecord.RECORD_ID;
            r.Options = (short)(0x0002 | ((int)format << 4));
            r.BlipTypeMacOS = (byte)format;
            r.BlipTypeWin32 = (byte)format;
            r.UID = uid;
            r.Tag = (short)0xFF;
            r.Size = pictureData.Length + 25;
            r.Ref = 0;
            r.Offset = 0;
            r.BlipRecord = blipRecord;

            return workbook.AddBSERecord(r);
        }
Example #3
0
        /// <summary>
        /// Generates an escher record including the any children contained under that record.
        /// An exception is thrown if the record could not be generated.
        /// </summary>
        /// <param name="data">The byte array containing the records</param>
        /// <param name="offset">The starting offset into the byte array</param>
        /// <returns>The generated escher record</returns>
        public virtual EscherRecord CreateRecord(byte[] data, int offset)
        {
            EscherRecord.EscherRecordHeader header = EscherRecord.EscherRecordHeader.ReadHeader(data, offset);

            // Options of 0x000F means container record
            // However, EscherTextboxRecord are containers of records for the
            //  host application, not of other Escher records, so treat them
            //  differently
            if ((header.Options & (short)0x000F) == (short)0x000F &&
                header.RecordId != EscherTextboxRecord.RECORD_ID)
            {
                EscherContainerRecord r = new EscherContainerRecord();
                r.RecordId = header.RecordId;
                r.Options  = header.Options;
                return(r);
            }
            else if (header.RecordId >= EscherBlipRecord.RECORD_ID_START && header.RecordId <= EscherBlipRecord.RECORD_ID_END)
            {
                EscherBlipRecord r;
                if (header.RecordId == EscherBitmapBlip.RECORD_ID_DIB ||
                    header.RecordId == EscherBitmapBlip.RECORD_ID_JPEG ||
                    header.RecordId == EscherBitmapBlip.RECORD_ID_PNG)
                {
                    r = new EscherBitmapBlip();
                }
                else if (header.RecordId == EscherMetafileBlip.RECORD_ID_EMF ||
                         header.RecordId == EscherMetafileBlip.RECORD_ID_WMF ||
                         header.RecordId == EscherMetafileBlip.RECORD_ID_PICT)
                {
                    r = new EscherMetafileBlip();
                }
                else
                {
                    r = new EscherBlipRecord();
                }
                r.RecordId = header.RecordId;
                r.Options  = header.Options;
                return(r);
            }
            else
            {
                //ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
                Type         record       = (Type)recordsMap[header.RecordId];
                EscherRecord escherRecord = null;
                //if ( recordConstructor != null )
                if (record != null)
                {
                    try
                    {
                        escherRecord          = (EscherRecord)Activator.CreateInstance(record);
                        escherRecord.RecordId = header.RecordId;
                        escherRecord.Options  = header.Options;
                    }
                    catch (Exception)
                    {
                        escherRecord = null;
                    }
                }
                return(escherRecord == null ? new UnknownEscherRecord() : escherRecord);
            }
        }
        /// <summary>
        /// Generates an escher record including the any children contained under that record.
        /// An exception is thrown if the record could not be generated.
        /// </summary>
        /// <param name="data">The byte array containing the records</param>
        /// <param name="offset">The starting offset into the byte array</param>
        /// <returns>The generated escher record</returns>
        public virtual EscherRecord CreateRecord(byte[] data, int offset)
        {
            short options = LittleEndian.GetShort(data, offset);
            short recordId = LittleEndian.GetShort(data, offset + 2);

            // int remainingBytes = LittleEndian.getInt( data, offset + 4 );
            // Options of 0x000F means container record
            // However, EscherTextboxRecord are containers of records for the
            //  host application, not of other Escher records, so treat them
            //  differently
            if (IsContainer(options, recordId))
            {
                EscherContainerRecord r = new EscherContainerRecord();
                r.RecordId = recordId;
                r.Options = options;
                return r;
            }
            if (recordId >= EscherBlipRecord.RECORD_ID_START && recordId <= EscherBlipRecord.RECORD_ID_END)
            {
                EscherBlipRecord r;
                if (recordId == EscherBitmapBlip.RECORD_ID_DIB ||
                        recordId == EscherBitmapBlip.RECORD_ID_JPEG ||
                        recordId == EscherBitmapBlip.RECORD_ID_PNG)
                {
                    r = new EscherBitmapBlip();
                }
                else if (recordId == EscherMetafileBlip.RECORD_ID_EMF ||
                        recordId == EscherMetafileBlip.RECORD_ID_WMF ||
                        recordId == EscherMetafileBlip.RECORD_ID_PICT)
                {
                    r = new EscherMetafileBlip();
                }
                else
                {
                    r = new EscherBlipRecord();
                }
                r.RecordId = recordId;
                r.Options = options;
                return r;
            }

            //ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
            ConstructorInfo recordConstructor = null;
            if (recordsMap.ContainsKey(recordId))
                recordConstructor = recordsMap[recordId];

            EscherRecord escherRecord = null;
            if (recordConstructor == null)
            {
                return new UnknownEscherRecord();
            }

            try
            {
                escherRecord = (EscherRecord)recordConstructor.Invoke(new object[] { });
                //escherRecord = (EscherRecord)Activator.CreateInstance(recordConstructor);
            }
            catch (Exception)
            {
                return new UnknownEscherRecord();
            }
            escherRecord.RecordId = recordId;
            escherRecord.Options = options;
            return escherRecord;

        }
        /// <summary>
        /// Generates an escher record including the any children contained under that record.
        /// An exception is thrown if the record could not be generated.
        /// </summary>
        /// <param name="data">The byte array containing the records</param>
        /// <param name="offset">The starting offset into the byte array</param>
        /// <returns>The generated escher record</returns>
        public virtual EscherRecord CreateRecord(byte[] data, int offset)
        {
            EscherRecord.EscherRecordHeader header = EscherRecord.EscherRecordHeader.ReadHeader(data, offset);

            // Options of 0x000F means container record
            // However, EscherTextboxRecord are containers of records for the
            //  host application, not of other Escher records, so treat them
            //  differently
            if ((header.Options & (short)0x000F) == (short)0x000F
                 && header.RecordId != EscherTextboxRecord.RECORD_ID)
            {
                EscherContainerRecord r = new EscherContainerRecord();
                r.RecordId = header.RecordId;
                r.Options = header.Options;
                return r;
            }
            else if (header.RecordId >= EscherBlipRecord.RECORD_ID_START && header.RecordId <= EscherBlipRecord.RECORD_ID_END)
            {
                EscherBlipRecord r;
                if (header.RecordId == EscherBitmapBlip.RECORD_ID_DIB ||
                        header.RecordId == EscherBitmapBlip.RECORD_ID_JPEG ||
                        header.RecordId == EscherBitmapBlip.RECORD_ID_PNG)
                {
                    r = new EscherBitmapBlip();
                }
                else if (header.RecordId == EscherMetafileBlip.RECORD_ID_EMF ||
                        header.RecordId == EscherMetafileBlip.RECORD_ID_WMF ||
                        header.RecordId == EscherMetafileBlip.RECORD_ID_PICT)
                {
                    r = new EscherMetafileBlip();
                }
                else
                {
                    r = new EscherBlipRecord();
                }
                r.RecordId = header.RecordId;
                r.Options = header.Options;
                return r;
            }
            else
            {
                //ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
                Type record = (Type)recordsMap[header.RecordId];
                EscherRecord escherRecord = null;
                //if ( recordConstructor != null )
                if (record != null)
                {
                    try
                    {
                        escherRecord = (EscherRecord)Activator.CreateInstance(record);
                        escherRecord.RecordId = header.RecordId;
                        escherRecord.Options = header.Options;
                    }
                    catch (Exception)
                    {
                        escherRecord = null;
                    }
                }
                return escherRecord == null ? new UnknownEscherRecord() : escherRecord;
            }
        }