Exemple #1
0
        public void TestRotation()
        {
            HSSFWorkbook  wb        = new HSSFWorkbook();
            HSSFSheet     sheet     = wb.CreateSheet() as HSSFSheet;
            HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch;

            HSSFSimpleShape rectangle = patriarch.CreateSimpleShape(new HSSFClientAnchor(0, 0, 100, 100, (short)0, 0, (short)5, 5));

            rectangle.ShapeType = HSSFSimpleShape.OBJECT_TYPE_RECTANGLE;

            Assert.AreEqual(rectangle.RotationDegree, 0);
            rectangle.RotationDegree = (short)45;
            Assert.AreEqual(rectangle.RotationDegree, 45);
            rectangle.IsFlipHorizontal = true;

            wb        = HSSFTestDataSamples.WriteOutAndReadBack(wb);
            sheet     = wb.GetSheetAt(0) as HSSFSheet;
            patriarch = sheet.DrawingPatriarch as HSSFPatriarch;
            rectangle = (HSSFSimpleShape)patriarch.Children[0];
            Assert.AreEqual(rectangle.RotationDegree, 45);
            rectangle.RotationDegree = (short)30;
            Assert.AreEqual(rectangle.RotationDegree, 30);

            patriarch.SetCoordinates(0, 0, 10, 10);
            rectangle.String = new HSSFRichTextString("1234");
        }
        /**
         * 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!");
        }
Exemple #3
0
        public void TestGetDataBackAgain()
        {
            HSSFSheet      s;
            HSSFShapeGroup s1;
            HSSFShapeGroup s2;

            patriarch.SetCoordinates(10, 20, 30, 40);

            MemoryStream baos = new MemoryStream();

            workbook.Write(baos);
            workbook = new HSSFWorkbook(new MemoryStream(baos.ToArray()));
            s        = (HSSFSheet)workbook.GetSheetAt(0);

            patriarch = (HSSFPatriarch)s.DrawingPatriarch;

            Assert.IsNotNull(patriarch);
            Assert.AreEqual(10, patriarch.X1);
            Assert.AreEqual(20, patriarch.Y1);
            Assert.AreEqual(30, patriarch.X2);
            Assert.AreEqual(40, patriarch.Y2);

            // Check the two groups too
            Assert.AreEqual(2, patriarch.CountOfAllChildren);
            Assert.IsTrue(patriarch.Children[0] is HSSFShapeGroup);
            Assert.IsTrue(patriarch.Children[1] is HSSFShapeGroup);

            s1 = (HSSFShapeGroup)patriarch.Children[0];
            s2 = (HSSFShapeGroup)patriarch.Children[1];

            Assert.AreEqual(0, s1.X1);
            Assert.AreEqual(0, s1.Y1);
            Assert.AreEqual(1023, s1.X2);
            Assert.AreEqual(255, s1.Y2);
            Assert.AreEqual(0, s2.X1);
            Assert.AreEqual(0, s2.Y1);
            Assert.AreEqual(1023, s2.X2);
            Assert.AreEqual(255, s2.Y2);

            Assert.AreEqual(0, s1.Anchor.Dx1);
            Assert.AreEqual(0, s1.Anchor.Dy1);
            Assert.AreEqual(1022, s1.Anchor.Dx2);
            Assert.AreEqual(255, s1.Anchor.Dy2);
            Assert.AreEqual(20, s2.Anchor.Dx1);
            Assert.AreEqual(30, s2.Anchor.Dy1);
            Assert.AreEqual(500, s2.Anchor.Dx2);
            Assert.AreEqual(200, s2.Anchor.Dy2);


            // Write and re-load once more, to Check that's ok
            baos = new MemoryStream();
            workbook.Write(baos);
            workbook  = new HSSFWorkbook(new MemoryStream(baos.ToArray()));
            s         = (HSSFSheet)workbook.GetSheetAt(0);
            patriarch = (HSSFPatriarch)s.DrawingPatriarch;

            Assert.IsNotNull(patriarch);
            Assert.AreEqual(10, patriarch.X1);
            Assert.AreEqual(20, patriarch.Y1);
            Assert.AreEqual(30, patriarch.X2);
            Assert.AreEqual(40, patriarch.Y2);

            // Check the two groups too
            Assert.AreEqual(2, patriarch.CountOfAllChildren);
            Assert.IsTrue(patriarch.Children[0] is HSSFShapeGroup);
            Assert.IsTrue(patriarch.Children[1] is HSSFShapeGroup);

            s1 = (HSSFShapeGroup)patriarch.Children[0];
            s2 = (HSSFShapeGroup)patriarch.Children[1];

            Assert.AreEqual(0, s1.X1);
            Assert.AreEqual(0, s1.Y1);
            Assert.AreEqual(1023, s1.X2);
            Assert.AreEqual(255, s1.Y2);
            Assert.AreEqual(0, s2.X1);
            Assert.AreEqual(0, s2.Y1);
            Assert.AreEqual(1023, s2.X2);
            Assert.AreEqual(255, s2.Y2);

            Assert.AreEqual(0, s1.Anchor.Dx1);
            Assert.AreEqual(0, s1.Anchor.Dy1);
            Assert.AreEqual(1022, s1.Anchor.Dx2);
            Assert.AreEqual(255, s1.Anchor.Dy2);
            Assert.AreEqual(20, s2.Anchor.Dx1);
            Assert.AreEqual(30, s2.Anchor.Dy1);
            Assert.AreEqual(500, s2.Anchor.Dx2);
            Assert.AreEqual(200, s2.Anchor.Dy2);

            // Change the positions of the first groups,
            //  but not of their anchors
            s1.SetCoordinates(2, 3, 1021, 242);

            baos = new MemoryStream();
            workbook.Write(baos);
            workbook  = new HSSFWorkbook(new MemoryStream(baos.ToArray()));
            s         = (HSSFSheet)workbook.GetSheetAt(0);
            patriarch = (HSSFPatriarch)s.DrawingPatriarch;

            Assert.IsNotNull(patriarch);
            Assert.AreEqual(10, patriarch.X1);
            Assert.AreEqual(20, patriarch.Y1);
            Assert.AreEqual(30, patriarch.X2);
            Assert.AreEqual(40, patriarch.Y2);

            // Check the two groups too
            Assert.AreEqual(2, patriarch.CountOfAllChildren);
            Assert.AreEqual(2, patriarch.Children.Count);
            Assert.IsTrue(patriarch.Children[0] is HSSFShapeGroup);
            Assert.IsTrue(patriarch.Children[1] is HSSFShapeGroup);

            s1 = (HSSFShapeGroup)patriarch.Children[0];
            s2 = (HSSFShapeGroup)patriarch.Children[1];

            Assert.AreEqual(2, s1.X1);
            Assert.AreEqual(3, s1.Y1);
            Assert.AreEqual(1021, s1.X2);
            Assert.AreEqual(242, s1.Y2);
            Assert.AreEqual(0, s2.X1);
            Assert.AreEqual(0, s2.Y1);
            Assert.AreEqual(1023, s2.X2);
            Assert.AreEqual(255, s2.Y2);

            Assert.AreEqual(0, s1.Anchor.Dx1);
            Assert.AreEqual(0, s1.Anchor.Dy1);
            Assert.AreEqual(1022, s1.Anchor.Dx2);
            Assert.AreEqual(255, s1.Anchor.Dy2);
            Assert.AreEqual(20, s2.Anchor.Dx1);
            Assert.AreEqual(30, s2.Anchor.Dy1);
            Assert.AreEqual(500, s2.Anchor.Dx2);
            Assert.AreEqual(200, s2.Anchor.Dy2);


            // Now Add some text to one group, and some more
            //  to the base, and Check we can get it back again
            HSSFTextbox tbox1 =
                patriarch.CreateTextbox(new HSSFClientAnchor(1, 2, 3, 4, (short)0, 0, (short)0, 0)) as HSSFTextbox;

            tbox1.String = (new HSSFRichTextString("I am text box 1"));
            HSSFTextbox tbox2 =
                s2.CreateTextbox(new HSSFChildAnchor(41, 42, 43, 44)) as HSSFTextbox;

            tbox2.String = (new HSSFRichTextString("This is text box 2"));

            Assert.AreEqual(3, patriarch.Children.Count);


            baos = new MemoryStream();
            workbook.Write(baos);
            workbook = new HSSFWorkbook(new MemoryStream(baos.ToArray()));
            s        = (HSSFSheet)workbook.GetSheetAt(0);

            patriarch = (HSSFPatriarch)s.DrawingPatriarch;

            Assert.IsNotNull(patriarch);
            Assert.AreEqual(10, patriarch.X1);
            Assert.AreEqual(20, patriarch.Y1);
            Assert.AreEqual(30, patriarch.X2);
            Assert.AreEqual(40, patriarch.Y2);

            // Check the two groups and the text
            // Result of patriarch.countOfAllChildren() makes no sense:
            // Returns 4 for 2 empty groups + 1 TextBox.
            //Assert.AreEqual(3, patriarch.CountOfAllChildren);
            Assert.AreEqual(3, patriarch.Children.Count);

            // Should be two groups and a text
            Assert.IsTrue(patriarch.Children[0] is HSSFShapeGroup);
            Assert.IsTrue(patriarch.Children[1] is HSSFShapeGroup);
            Assert.IsTrue(patriarch.Children[2] is HSSFTextbox);


            s1    = (HSSFShapeGroup)patriarch.Children[0];
            tbox1 = (HSSFTextbox)patriarch.Children[2];

            s2 = (HSSFShapeGroup)patriarch.Children[1];

            Assert.AreEqual(2, s1.X1);
            Assert.AreEqual(3, s1.Y1);
            Assert.AreEqual(1021, s1.X2);
            Assert.AreEqual(242, s1.Y2);
            Assert.AreEqual(0, s2.X1);
            Assert.AreEqual(0, s2.Y1);
            Assert.AreEqual(1023, s2.X2);
            Assert.AreEqual(255, s2.Y2);

            Assert.AreEqual(0, s1.Anchor.Dx1);
            Assert.AreEqual(0, s1.Anchor.Dy1);
            Assert.AreEqual(1022, s1.Anchor.Dx2);
            Assert.AreEqual(255, s1.Anchor.Dy2);
            Assert.AreEqual(20, s2.Anchor.Dx1);
            Assert.AreEqual(30, s2.Anchor.Dy1);
            Assert.AreEqual(500, s2.Anchor.Dx2);
            Assert.AreEqual(200, s2.Anchor.Dy2);

            // Not working just yet
            //Assert.AreEqual("I am text box 1", tbox1.String.String);
        }
Exemple #4
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!");
        }