Esempio n. 1
0
        public void TestBug56835CellComment()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            try
            {
                XSSFSheet   sheet   = wb.CreateSheet() as XSSFSheet;
                XSSFDrawing Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;

                // first comment works
                IClientAnchor anchor  = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
                XSSFComment   comment = Drawing.CreateCellComment(anchor) as XSSFComment;
                Assert.IsNotNull(comment);

                try
                {
                    Drawing.CreateCellComment(anchor);
                    Assert.Fail("Should fail if we try to add the same comment for the same cell");
                }
                catch (ArgumentException)
                {
                    // expected
                }
            }
            finally
            {
                wb.Close();
            }
        }
Esempio n. 2
0
        /**
         * Create and Initialize a CT_TwoCellAnchor that anchors a shape against top-left and bottom-right cells.
         *
         * @return a new CT_TwoCellAnchor
         */
        private CT_TwoCellAnchor CreateTwoCellAnchor(IClientAnchor anchor)
        {
            CT_TwoCellAnchor ctAnchor   = drawing.AddNewTwoCellAnchor();
            XSSFClientAnchor xssfanchor = (XSSFClientAnchor)anchor;

            ctAnchor.from = (xssfanchor.From);
            ctAnchor.to   = (xssfanchor.To);
            ctAnchor.AddNewClientData();
            xssfanchor.To   = ctAnchor.to;
            xssfanchor.From = ctAnchor.from;
            ST_EditAs aditAs;

            switch (anchor.AnchorType)
            {
            case AnchorType.DontMoveAndResize:
                aditAs = ST_EditAs.absolute; break;

            case AnchorType.MoveAndResize:
                aditAs = ST_EditAs.twoCell; break;

            case AnchorType.MoveDontResize:
                aditAs = ST_EditAs.oneCell; break;

            default:
                aditAs = ST_EditAs.oneCell;
                break;
            }
            ctAnchor.editAs          = aditAs;
            ctAnchor.editAsSpecified = true;
            return(ctAnchor);
        }
Esempio n. 3
0
        public void TestReadAnchors()
        {
            XSSFWorkbook wb      = new XSSFWorkbook();
            XSSFSheet    sheet   = wb.CreateSheet() as XSSFSheet;
            XSSFDrawing  Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;

            XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
            XSSFShape        shape1  = Drawing.CreateTextbox(anchor1) as XSSFShape;

            Assert.IsNotNull(shape1);

            XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 5);
            XSSFShape        shape2  = Drawing.CreateTextbox(anchor2) as XSSFShape;

            Assert.IsNotNull(shape2);

            int pictureIndex         = wb.AddPicture(new byte[] { }, XSSFWorkbook.PICTURE_TYPE_PNG);
            XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
            XSSFShape        shape3  = Drawing.CreatePicture(anchor3, pictureIndex) as XSSFShape;

            Assert.IsNotNull(shape3);

            wb      = XSSFTestDataSamples.WriteOutAndReadBack(wb) as XSSFWorkbook;
            sheet   = wb.GetSheetAt(0) as XSSFSheet;
            Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;
            List <XSSFShape> shapes = Drawing.GetShapes();

            Assert.AreEqual(3, shapes.Count);
            Assert.AreEqual(shapes[0].GetAnchor(), anchor1);
            Assert.AreEqual(shapes[1].GetAnchor(), anchor2);
            Assert.AreEqual(shapes[2].GetAnchor(), anchor3);

            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
Esempio n. 4
0
        public void Create()
        {
            XSSFWorkbook wb      = new XSSFWorkbook();
            XSSFSheet    sheet   = (XSSFSheet)wb.CreateSheet();
            XSSFDrawing  drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            byte[] jpegData = Encoding.UTF8.GetBytes("test jpeg data");

            IList pictures = wb.GetAllPictures();

            Assert.AreEqual(0, pictures.Count);

            int jpegIdx = wb.AddPicture(jpegData, PictureType.JPEG);

            Assert.AreEqual(1, pictures.Count);
            Assert.AreEqual("jpeg", ((XSSFPictureData)pictures[jpegIdx]).SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(jpegData, ((XSSFPictureData)pictures[jpegIdx]).Data));

            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 10, 30);

            Assert.AreEqual(AnchorType.MoveAndResize, (AnchorType)anchor.AnchorType);
            anchor.AnchorType = AnchorType.DontMoveAndResize;
            Assert.AreEqual(AnchorType.DontMoveAndResize, (AnchorType)anchor.AnchorType);

            XSSFPicture shape = (XSSFPicture)drawing.CreatePicture(anchor, jpegIdx);

            Assert.IsTrue(anchor.Equals(shape.GetAnchor()));
            Assert.IsNotNull(shape.PictureData);
            Assert.IsTrue(Arrays.Equals(jpegData, shape.PictureData.Data));

            CT_TwoCellAnchor ctShapeHolder = (CT_TwoCellAnchor)drawing.GetCTDrawing().CellAnchors[0];

            // STEditAs.ABSOLUTE corresponds to ClientAnchor.DONT_MOVE_AND_RESIZE
            Assert.AreEqual(ST_EditAs.absolute, ctShapeHolder.editAs);
        }
Esempio n. 5
0
        /**
         * Creates a comment.
         * @param anchor the client anchor describes how this comment is attached
         *               to the sheet.
         * @return the newly Created comment.
         */
        public IComment CreateCellComment(IClientAnchor anchor)
        {
            XSSFClientAnchor ca    = (XSSFClientAnchor)anchor;
            XSSFSheet        sheet = (XSSFSheet)GetParent();

            //create comments and vmlDrawing parts if they don't exist
            CommentsTable  comments = sheet.GetCommentsTable(true);
            XSSFVMLDrawing vml      = sheet.GetVMLDrawing(true);

            Npoi.Core.OpenXmlFormats.Vml.CT_Shape vmlShape = vml.newCommentShape();
            if (ca.IsSet())
            {
                // convert offsets from emus to pixels since we get a DrawingML-anchor
                // but create a VML Drawing
                int    dx1Pixels = ca.Dx1 / Units.EMU_PER_PIXEL;
                int    dy1Pixels = ca.Dy1 / Units.EMU_PER_PIXEL;
                int    dx2Pixels = ca.Dx2 / Units.EMU_PER_PIXEL;
                int    dy2Pixels = ca.Dy2 / Units.EMU_PER_PIXEL;
                String position  =
                    ca.Col1 + ", " + dx1Pixels + ", " +
                    ca.Row1 + ", " + dy1Pixels + ", " +
                    ca.Col2 + ", " + dx2Pixels + ", " +
                    ca.Row2 + ", " + dy2Pixels;
                vmlShape.GetClientDataArray(0).SetAnchorArray(0, position);
            }
            String ref1 = new CellReference(ca.Row1, ca.Col1).FormatAsString();

            if (comments.FindCellComment(ref1) != null)
            {
                throw new ArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref1);
            }

            return(new XSSFComment(comments, comments.NewComment(ref1), vmlShape));
        }
Esempio n. 6
0
        /**
         * Creates a simple shape.  This includes such shapes as lines, rectangles,
         * and ovals.
         *
         * @param anchor    the client anchor describes how this group is attached
         *                  to the sheet.
         * @return  the newly Created shape.
         */
        public XSSFShapeGroup CreateGroup(XSSFClientAnchor anchor)
        {
            CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
            CT_GroupShape    ctGroup  = ctAnchor.AddNewGrpSp();

            ctGroup.Set(XSSFShapeGroup.Prototype());

            XSSFShapeGroup shape = new XSSFShapeGroup(this, ctGroup);

            shape.anchor = anchor;
            return(shape);
        }
Esempio n. 7
0
        /**
         * Creates a simple shape.  This includes such shapes as lines, rectangles,
         * and ovals.
         *
         * @param anchor    the client anchor describes how this group is attached
         *                  to the sheet.
         * @return  the newly Created shape.
         */
        public XSSFConnector CreateConnector(XSSFClientAnchor anchor)
        {
            CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
            CT_Connector     ctShape  = ctAnchor.AddNewCxnSp();

            ctShape.Set(XSSFConnector.Prototype());

            XSSFConnector shape = new XSSFConnector(this, ctShape);

            shape.anchor = anchor;
            return(shape);
        }
Esempio n. 8
0
        /**
         * Creates a simple shape.  This includes such shapes as lines, rectangles,
         * and ovals.
         *
         * @param anchor    the client anchor describes how this group is attached
         *                  to the sheet.
         * @return  the newly Created shape.
         */
        public XSSFSimpleShape CreateSimpleShape(XSSFClientAnchor anchor)
        {
            long             shapeId  = newShapeId();
            CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
            CT_Shape         ctShape  = ctAnchor.AddNewSp();

            ctShape.Set(XSSFSimpleShape.GetPrototype());
            ctShape.nvSpPr.cNvPr.id = (uint)(shapeId);
            XSSFSimpleShape shape = new XSSFSimpleShape(this, ctShape);

            shape.anchor = anchor;
            return(shape);
        }
Esempio n. 9
0
        /**
         * Creates a new graphic frame.
         *
         * @param anchor    the client anchor describes how this frame is attached
         *                  to the sheet
         * @return  the newly Created graphic frame
         */
        private XSSFGraphicFrame CreateGraphicFrame(XSSFClientAnchor anchor)
        {
            CT_TwoCellAnchor        ctAnchor       = CreateTwoCellAnchor(anchor);
            CT_GraphicalObjectFrame ctGraphicFrame = ctAnchor.AddNewGraphicFrame();

            ctGraphicFrame.Set(XSSFGraphicFrame.Prototype());

            long             frameId      = numOfGraphicFrames++;
            XSSFGraphicFrame graphicFrame = new XSSFGraphicFrame(this, ctGraphicFrame);

            graphicFrame.Anchor = anchor;
            graphicFrame.Id     = frameId;
            graphicFrame.Name   = "Diagramm" + frameId;
            return(graphicFrame);
        }
Esempio n. 10
0
        /**
         * Creates a picture.
         *
         * @param anchor       the client anchor describes how this picture is attached to the sheet.
         * @param pictureIndex the index of the picture in the workbook collection of pictures,
         *                     {@link XSSFWorkbook#getAllPictures()} .
         * @return the newly Created picture shape.
         */

        public XSSFPicture CreatePicture(XSSFClientAnchor anchor, int pictureIndex)
        {
            PackageRelationship rel = GetDrawing().AddPictureReference(pictureIndex);

            CT_Picture ctShape = ctGroup.AddNewPic();

            ctShape.Set(XSSFPicture.Prototype());

            XSSFPicture shape = new XSSFPicture(GetDrawing(), ctShape);

            shape.parent = this;
            shape.anchor = anchor;
            shape.SetPictureReference(rel);
            return(shape);
        }
Esempio n. 11
0
        private XSSFAnchor GetAnchorFromIEGAnchor(IEG_Anchor ctAnchor)
        {
            CT_Marker ctFrom = null, ctTo = null;

            if (ctAnchor is CT_TwoCellAnchor)
            {
                ctFrom = ((CT_TwoCellAnchor)ctAnchor).from;
                ctTo   = ((CT_TwoCellAnchor)ctAnchor).to;
            }
            else if (ctAnchor is CT_OneCellAnchor)
            {
                ctFrom = ((CT_OneCellAnchor)ctAnchor).from;
            }
            XSSFAnchor anchor = new XSSFClientAnchor(ctFrom, ctTo);

            return(anchor);
        }
Esempio n. 12
0
        public override bool Equals(object o)
        {
            if (o == null || !(o is XSSFClientAnchor))
            {
                return(false);
            }

            XSSFClientAnchor anchor = (XSSFClientAnchor)o;

            return(Dx1 == anchor.Dx1 &&
                   Dx2 == anchor.Dx2 &&
                   Dy1 == anchor.Dy1 &&
                   Dy2 == anchor.Dy2 &&
                   Col1 == anchor.Col1 &&
                   Col2 == anchor.Col2 &&
                   Row1 == anchor.Row1 &&
                   Row2 == anchor.Row2);
        }
Esempio n. 13
0
        /**
         * Creates a picture.
         *
         * @param anchor    the client anchor describes how this picture is attached to the sheet.
         * @param pictureIndex the index of the picture in the workbook collection of pictures,
         *   {@link Npoi.Core.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
         *
         * @return  the newly Created picture shape.
         */
        public IPicture CreatePicture(XSSFClientAnchor anchor, int pictureIndex)
        {
            PackageRelationship rel = AddPictureReference(pictureIndex);

            long             shapeId  = newShapeId();
            CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
            CT_Picture       ctShape  = ctAnchor.AddNewPic();

            ctShape.Set(XSSFPicture.Prototype());

            ctShape.nvPicPr.cNvPr.id   = (uint)shapeId;
            ctShape.nvPicPr.cNvPr.name = "Picture " + shapeId;

            XSSFPicture shape = new XSSFPicture(this, ctShape);

            shape.anchor = anchor;
            shape.SetPictureReference(rel);
            return(shape);
        }
Esempio n. 14
0
        public void TestAddChartsToNewWorkbook()
        {
            XSSFWorkbook     wb = new XSSFWorkbook();
            XSSFSheet        s1 = (XSSFSheet)wb.CreateSheet();
            XSSFDrawing      d1 = (XSSFDrawing)s1.CreateDrawingPatriarch();
            XSSFClientAnchor a1 = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 10, 30);
            XSSFChart        c1 = (XSSFChart)d1.CreateChart(a1);

            Assert.AreEqual(1, d1.GetCharts().Count);

            Assert.IsNotNull(c1.GetGraphicFrame());
            Assert.IsNotNull(c1.GetOrCreateLegend());

            XSSFClientAnchor a2 = new XSSFClientAnchor(0, 0, 0, 0, 1, 11, 10, 60);
            XSSFChart        c2 = (XSSFChart)d1.CreateChart(a2);

            Assert.IsNotNull(c2);
            Assert.AreEqual(2, d1.GetCharts().Count);
            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
Esempio n. 15
0
        public void IncrementShapeId()
        {
            XSSFWorkbook wb      = new XSSFWorkbook();
            XSSFSheet    sheet   = (XSSFSheet)wb.CreateSheet();
            XSSFDrawing  drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 10, 30);

            byte[] jpegData = Encoding.UTF8.GetBytes("picture1");
            int    jpegIdx  = wb.AddPicture(jpegData, PictureType.JPEG);

            XSSFPicture shape1 = (XSSFPicture)drawing.CreatePicture(anchor, jpegIdx);

            Assert.AreEqual((uint)1, shape1.GetCTPicture().nvPicPr.cNvPr.id);

            jpegData = Encoding.UTF8.GetBytes("picture2");
            jpegIdx  = wb.AddPicture(jpegData, PictureType.JPEG);
            XSSFPicture shape2 = (XSSFPicture)drawing.CreatePicture(anchor, jpegIdx);

            Assert.AreEqual((uint)2, shape2.GetCTPicture().nvPicPr.cNvPr.id);
        }