Ejemplo n.º 1
0
        public void TestCloneSheetWithDrawings()
        {
            HSSFWorkbook wb1 = HSSFTestDataSamples.OpenSampleWorkbook("45720.xls");

            HSSFSheet sheet1 = (HSSFSheet)wb1.GetSheetAt(0);

            wb1.Workbook.FindDrawingGroup();
            DrawingManager2 dm1 = wb1.Workbook.DrawingManager;

            wb1.CloneSheet(0);

            HSSFWorkbook wb2 = HSSFTestDataSamples.WriteOutAndReadBack(wb1);

            wb2.Workbook.FindDrawingGroup();
            DrawingManager2 dm2 = wb2.Workbook.DrawingManager;

            //Check EscherDggRecord - a workbook-level registry of drawing objects
            Assert.AreEqual(dm1.GetDgg().MaxDrawingGroupId + 1, dm2.GetDgg().MaxDrawingGroupId);

            HSSFSheet sheet2 = (HSSFSheet)wb2.GetSheetAt(1);

            //Check that id of the drawing Group was updated
            EscherDgRecord dg1     = (EscherDgRecord)(sheet1.DrawingPatriarch as HSSFPatriarch).GetBoundAggregate().FindFirstWithId(EscherDgRecord.RECORD_ID);
            EscherDgRecord dg2     = (EscherDgRecord)(sheet2.DrawingPatriarch as HSSFPatriarch).GetBoundAggregate().FindFirstWithId(EscherDgRecord.RECORD_ID);
            int            dg_id_1 = dg1.Options >> 4;
            int            dg_id_2 = dg2.Options >> 4;

            Assert.AreEqual(dg_id_1 + 1, dg_id_2);

            //TODO: Check shapeId in the Cloned sheet
        }
Ejemplo n.º 2
0
        /**
         * Converts the Records into UserModel
         *  objects on the bound HSSFPatriarch
         */
        public void ConvertRecordsToUserModel()
        {
            if (patriarch == null)
            {
                throw new InvalidOperationException("Must call SetPatriarch() first");
            }

            // The top level container ought to have
            //  the DgRecord and the container of one container
            //  per shape Group (patriach overall first)
            EscherContainerRecord topContainer =
                (EscherContainerRecord)GetEscherContainer();

            if (topContainer == null)
            {
                return;
            }
            topContainer = (EscherContainerRecord)
                           topContainer.ChildContainers[0];

            IList <EscherContainerRecord> tcc = topContainer.ChildContainers;

            if (tcc.Count == 0)
            {
                throw new InvalidOperationException("No child escher containers at the point that should hold the patriach data, and one container per top level shape!");
            }

            // First up, Get the patriach position
            // This Is in the first EscherSpgrRecord, in
            //  the first container, with a EscherSRecord too
            EscherContainerRecord patriachContainer =
                (EscherContainerRecord)tcc[0];
            EscherSpgrRecord spgr = null;

            for (IEnumerator it = patriachContainer.ChildRecords.GetEnumerator(); it.MoveNext();)
            {
                EscherRecord r = (EscherRecord)it.Current;
                if (r is EscherSpgrRecord)
                {
                    spgr = (EscherSpgrRecord)r;
                    break;
                }
            }
            if (spgr != null)
            {
                patriarch.SetCoordinates(
                    spgr.RectX1, spgr.RectY1,
                    spgr.RectX2, spgr.RectY2
                    );
            }

            // Now Process the containers for each Group
            //  and objects
            for (int i = 1; i < tcc.Count; i++)
            {
                EscherContainerRecord shapeContainer =
                    (EscherContainerRecord)tcc[i];
                //Console.Error.WriteLine("\n\n*****\n\n");
                //Console.Error.WriteLine(shapeContainer);

                // Could be a Group, or a base object
                if (shapeContainer.RecordId == EscherContainerRecord.SPGR_CONTAINER)
                {
                    if (shapeContainer.ChildRecords.Count > 0)
                    {
                        // Group
                        HSSFShapeGroup group =
                            new HSSFShapeGroup(null, new HSSFClientAnchor());
                        patriarch.Children.Add(group);

                        EscherContainerRecord groupContainer =
                            (EscherContainerRecord)shapeContainer.GetChild(0);
                        ConvertRecordsToUserModel(groupContainer, group);
                    }
                }
                else if (shapeContainer.RecordId == EscherContainerRecord.SP_CONTAINER)
                {
                    EscherSpRecord spRecord = shapeContainer.GetChildById(EscherSpRecord.RECORD_ID);
                    int            type     = spRecord.Options >> 4;

                    switch (type)
                    {
                    case ST_TEXTBOX:
                        // TextBox
                        HSSFTextbox box =
                            new HSSFTextbox(null, new HSSFClientAnchor());
                        patriarch.Children.Add(box);

                        ConvertRecordsToUserModel(shapeContainer, box);
                        break;

                    case ST_PICTUREFRAME:
                        // Duplicated from
                        // org.apache.poi.hslf.model.Picture.getPictureIndex()
                        EscherOptRecord      opt  = (EscherOptRecord)GetEscherChild(shapeContainer, EscherOptRecord.RECORD_ID);
                        EscherSimpleProperty prop = (EscherSimpleProperty)opt.Lookup(EscherProperties.BLIP__BLIPTODISPLAY);
                        if (prop != null)
                        {
                            int pictureIndex = prop.PropertyValue;
                            EscherClientAnchorRecord anchorRecord = (EscherClientAnchorRecord)GetEscherChild(shapeContainer, EscherClientAnchorRecord.RECORD_ID);
                            HSSFClientAnchor         anchor       = new HSSFClientAnchor();
                            anchor.Col1 = anchorRecord.Col1;
                            anchor.Col2 = anchorRecord.Col2;
                            anchor.Dx1  = anchorRecord.Dx1;
                            anchor.Dx2  = anchorRecord.Dx2;
                            anchor.Dy1  = anchorRecord.Dy1;
                            anchor.Dy2  = anchorRecord.Dy2;
                            anchor.Row1 = anchorRecord.Row1;
                            anchor.Row2 = anchorRecord.Row2;
                            HSSFPicture picture = new HSSFPicture(null, anchor);
                            picture.PictureIndex = pictureIndex;
                            patriarch.AddShape(picture);
                        }
                        break;
                    }
                }
                else
                {
                    // Base level
                    ConvertRecordsToUserModel(shapeContainer, patriarch);
                }
            }

            // Now, clear any trace of what records make up
            //  the patriarch
            // Otherwise, everything will go horribly wrong
            //  when we try to Write out again....
            //      clearEscherRecords();
            drawingManager.GetDgg().FileIdClusters = new EscherDggRecord.FileIdCluster[0];

            // TODO: Support Converting our records
            //  back into shapes
            log.Log(POILogger.WARN, "Not Processing objects into Patriarch!");
        }
Ejemplo n.º 3
0
        /**
         * Converts the Records into UserModel
         *  objects on the bound HSSFPatriarch
         */
        public void ConvertRecordsToUserModel()
        {
            if (patriarch == null)
            {
                throw new InvalidOperationException("Must call SetPatriarch() first");
            }

            // The top level container ought to have
            //  the DgRecord and the container of one container
            //  per shape Group (patriach overall first)
            EscherContainerRecord topContainer =
                (EscherContainerRecord)GetEscherContainer();

            if (topContainer == null)
            {
                return;
            }
            topContainer = (EscherContainerRecord)
                           topContainer.ChildContainers[0];

            IList tcc = topContainer.ChildContainers;

            if (tcc.Count == 0)
            {
                throw new InvalidOperationException("No child escher containers at the point that should hold the patriach data, and one container per top level shape!");
            }

            // First up, Get the patriach position
            // This Is in the first EscherSpgrRecord, in
            //  the first container, with a EscherSRecord too
            EscherContainerRecord patriachContainer =
                (EscherContainerRecord)tcc[0];
            EscherSpgrRecord spgr = null;

            for (IEnumerator it = patriachContainer.ChildRecords.GetEnumerator(); it.MoveNext();)
            {
                EscherRecord r = (EscherRecord)it.Current;
                if (r is EscherSpgrRecord)
                {
                    spgr = (EscherSpgrRecord)r;
                    break;
                }
            }
            if (spgr != null)
            {
                patriarch.SetCoordinates(
                    spgr.RectX1, spgr.RectY1,
                    spgr.RectX2, spgr.RectY2
                    );
            }

            // Now Process the containers for each Group
            //  and objects
            for (int i = 1; i < tcc.Count; i++)
            {
                EscherContainerRecord shapeContainer =
                    (EscherContainerRecord)tcc[i];
                //Console.Error.WriteLine("\n\n*****\n\n");
                //Console.Error.WriteLine(shapeContainer);

                // Could be a Group, or a base object
                if (shapeContainer.ChildRecords.Count == 1 &&
                    shapeContainer.ChildContainers.Count == 1)
                {
                    // Group
                    HSSFShapeGroup group =
                        new HSSFShapeGroup(null, new HSSFClientAnchor());
                    patriarch.Children.Add(group);

                    EscherContainerRecord groupContainer =
                        (EscherContainerRecord)shapeContainer.GetChild(0);
                    ConvertRecordsToUserModel(groupContainer, group);
                }
                else if (shapeContainer.HasChildOfType(unchecked ((short)0xF00D)))
                {
                    // TextBox
                    HSSFTextbox box =
                        new HSSFTextbox(null, new HSSFClientAnchor());
                    patriarch.Children.Add(box);

                    ConvertRecordsToUserModel(shapeContainer, box);
                }
                else if (shapeContainer.HasChildOfType(unchecked ((short)0xF011)))
                {
                    // Not yet supporting EscherClientDataRecord stuff
                }
                else
                {
                    // Base level
                    ConvertRecordsToUserModel(shapeContainer, patriarch);
                }
            }

            // Now, clear any trace of what records make up
            //  the patriarch
            // Otherwise, everything will go horribly wrong
            //  when we try to Write out again....
            //      clearEscherRecords();
            drawingManager.GetDgg().FileIdClusters = new EscherDggRecord.FileIdCluster[0];

            // TODO: Support Converting our records
            //  back into shapes
            log.Log(POILogger.WARN, "Not Processing objects into Patriarch!");
        }