Пример #1
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);
            }
            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);
            }

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

            if (recordsMap.ContainsKey(header.RecordId))
            {
                recordConstructor = recordsMap[header.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 = header.RecordId;
            escherRecord.Options  = header.Options;
            return(escherRecord);
        }
Пример #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, Zephyr.Utils.NPOI.SS.UserModel.PictureType format)
        {
            InitDrawings();

            byte[] uid;
            using (MD5 md5 = MD5.Create())
            {
                uid = md5.ComputeHash(pictureData);
            }
            EscherBitmapBlip blipRecord = new EscherBitmapBlip();
            blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format);
            
            switch (format)
            {
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.EMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_EMF;
                    break;
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.WMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_WMF;
                    break;
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.PICT:
                    blipRecord.Options = HSSFPictureData.MSOBI_PICT;
                    break;
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.PNG:
                    blipRecord.Options = HSSFPictureData.MSOBI_PNG;
                    break;
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.JPEG:
                    blipRecord.Options = HSSFPictureData.MSOBI_JPEG;
                    break;
                case Zephyr.Utils.NPOI.SS.UserModel.PictureType.DIB:
                    blipRecord.Options = HSSFPictureData.MSOBI_DIB;
                    break;
            }

            blipRecord.UID = uid;
            blipRecord.Marker = (byte)0xFF;
            blipRecord.PictureData = pictureData;

            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 = 1;
            r.Offset = 0;
            r.BlipRecord = blipRecord;

            return workbook.AddBSERecord(r);
        }
        /// <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;
            }
            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;
            }

            //ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
            ConstructorInfo recordConstructor = null;
            if (recordsMap.ContainsKey(header.RecordId))
                recordConstructor = recordsMap[header.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 = header.RecordId;
            escherRecord.Options = header.Options;
            return escherRecord;

        }