Beispiel #1
0
        public void TestCreate()
        {
            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 = (int)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);
        }
Beispiel #2
0
        public void Test53568()
        {
            XSSFWorkbook           wb       = XSSFTestDataSamples.OpenSampleWorkbook("53568.xlsx");
            List <XSSFPictureData> pictures = wb.GetAllPictures() as List <XSSFPictureData>;

            Assert.IsNotNull(pictures);
            Assert.AreEqual(4, pictures.Count);

            XSSFSheet        sheet1  = wb.GetSheetAt(0) as XSSFSheet;
            List <XSSFShape> shapes1 = (sheet1.CreateDrawingPatriarch() as XSSFDrawing).GetShapes();

            Assert.IsNotNull(shapes1);
            Assert.AreEqual(5, shapes1.Count);

            for (int i = 0; i < wb.NumberOfSheets; i++)
            {
                XSSFSheet   sheet   = wb.GetSheetAt(i) as XSSFSheet;
                XSSFDrawing Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;
                foreach (XSSFShape shape in Drawing.GetShapes())
                {
                    if (shape is XSSFPicture)
                    {
                        XSSFPicture     pic     = (XSSFPicture)shape;
                        XSSFPictureData picData = pic.PictureData as XSSFPictureData;
                        Assert.IsNotNull(picData);
                    }
                }
            }
        }
Beispiel #3
0
        /**
         *
         * @return list of shapes in this drawing
         */
        public List <XSSFShape> GetShapes()
        {
            List <XSSFShape> lst = new List <XSSFShape>();

            foreach (IEG_Anchor anchor in drawing.CellAnchors)
            {
                XSSFShape shape = null;
                if (anchor.picture != null)
                {
                    shape = new XSSFPicture(this, anchor.picture);
                }
                else if (anchor.connector != null)
                {
                    shape = new XSSFConnector(this, anchor.connector);
                }
                else if (anchor.groupShape != null)
                {
                    shape = new XSSFShapeGroup(this, anchor.groupShape);
                }
                else if (anchor.graphicFrame != null)
                {
                    shape = new XSSFGraphicFrame(this, anchor.graphicFrame);
                }
                else if (anchor.sp != null)
                {
                    shape = new XSSFSimpleShape(this, anchor.sp);
                }
                if (shape != null)
                {
                    shape.anchor = GetAnchorFromIEGAnchor(anchor);
                    lst.Add(shape);
                }
            }
            //foreach (XmlNode obj in xmldoc.SelectNodes("./*/*/*"))
            //{
            //    XSSFShape shape = null;
            //    if (obj.LocalName == "sp")
            //    {
            //        shape = new XSSFSimpleShape(this, obj);
            //    }
            //    else if (obj.LocalName == "pic")
            //    {
            //        shape = new XSSFPicture(this, obj);
            //    }
            //    else if (obj.LocalName == "cxnSp")
            //    {
            //        shape = new XSSFConnector(this, obj);
            //    }
            //    //    else if (obj is CT_GraphicalObjectFrame) shape = new XSSFGraphicFrame(this, (CT_GraphicalObjectFrame)obj);
            //    //    else if (obj is CT_GroupShape) shape = new XSSFShapeGroup(this, (CT_GroupShape)obj);
            //    if (shape != null)
            //    {
            //        shape.anchor = GetAnchorFromParent(obj);
            //        lst.Add(shape);
            //    }
            //}
            return(lst);
        }
Beispiel #4
0
        public void TestNew()
        {
            XSSFWorkbook wb      = new XSSFWorkbook();
            XSSFSheet    sheet   = (XSSFSheet)wb.CreateSheet();
            XSSFDrawing  Drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            byte[] jpegData = Encoding.UTF8.GetBytes("test jpeg data");
            byte[] wmfData  = Encoding.UTF8.GetBytes("test wmf data");
            byte[] pngData  = Encoding.UTF8.GetBytes("test png 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));

            int wmfIdx = wb.AddPicture(wmfData, PictureType.WMF);

            Assert.AreEqual(2, pictures.Count);
            Assert.AreEqual("wmf", ((XSSFPictureData)pictures[wmfIdx]).SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(wmfData, ((XSSFPictureData)pictures[wmfIdx]).Data));

            int pngIdx = wb.AddPicture(pngData, PictureType.PNG);

            Assert.AreEqual(3, pictures.Count);
            Assert.AreEqual("png", ((XSSFPictureData)pictures[pngIdx]).SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(pngData, ((XSSFPictureData)pictures[pngIdx]).Data));

            //TODO finish usermodel API for XSSFPicture
            XSSFPicture p1 = (XSSFPicture)Drawing.CreatePicture(new XSSFClientAnchor(), jpegIdx);

            Assert.IsNotNull(p1);
            XSSFPicture p2 = (XSSFPicture)Drawing.CreatePicture(new XSSFClientAnchor(), wmfIdx);

            Assert.IsNotNull(p1);
            XSSFPicture p3 = (XSSFPicture)Drawing.CreatePicture(new XSSFClientAnchor(), pngIdx);

            Assert.IsNotNull(p1);

            //check that the Added pictures are accessible After write
            wb = (XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack(wb);
            IList pictures2 = wb.GetAllPictures();

            Assert.AreEqual(3, pictures2.Count);

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

            Assert.AreEqual("wmf", ((XSSFPictureData)pictures2[wmfIdx]).SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(wmfData, ((XSSFPictureData)pictures2[wmfIdx]).Data));

            Assert.AreEqual("png", ((XSSFPictureData)pictures2[pngIdx]).SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(pngData, ((XSSFPictureData)pictures2[pngIdx]).Data));
        }
Beispiel #5
0
        public void Resize()
        {
            XSSFWorkbook     wb     = XSSFITestDataProvider.instance.OpenSampleWorkbook("resize_Compare.xlsx") as XSSFWorkbook;
            XSSFDrawing      dp     = wb.GetSheetAt(0).CreateDrawingPatriarch() as XSSFDrawing;
            List <XSSFShape> pics   = dp.GetShapes();
            XSSFPicture      inpPic = (XSSFPicture)pics[(0)];
            XSSFPicture      cmpPic = (XSSFPicture)pics[(0)];

            BaseTestResize(inpPic, cmpPic, 2.0, 2.0);
            wb.Close();
        }
Beispiel #6
0
        public XSSFPicture CreatePicture(XSSFClientAnchor anchor, int pictureIndex)
        {
            PackageRelationship rel       = this.GetDrawing().AddPictureReference(pictureIndex);
            CT_Picture          ctPicture = this.ctGroup.AddNewPic();

            ctPicture.Set(XSSFPicture.Prototype());
            XSSFPicture xssfPicture = new XSSFPicture(this.GetDrawing(), ctPicture);

            xssfPicture.parent = this;
            xssfPicture.anchor = (XSSFAnchor)anchor;
            xssfPicture.SetPictureReference(rel);
            return(xssfPicture);
        }
Beispiel #7
0
        public IPicture CreatePicture(XSSFClientAnchor anchor, int pictureIndex)
        {
            PackageRelationship rel = this.AddPictureReference(pictureIndex);
            long       num          = this.newShapeId();
            CT_Picture ctPicture    = this.CreateTwoCellAnchor((IClientAnchor)anchor).AddNewPic();

            ctPicture.Set(XSSFPicture.Prototype());
            ctPicture.nvPicPr.cNvPr.id = (uint)num;
            XSSFPicture xssfPicture = new XSSFPicture(this, ctPicture);

            xssfPicture.anchor = (XSSFAnchor)anchor;
            xssfPicture.SetPictureReference(rel);
            return((IPicture)xssfPicture);
        }
Beispiel #8
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);
        }
Beispiel #9
0
        public void TestMultiRelationShips()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            byte[] pic1Data = Encoding.UTF8.GetBytes("test jpeg data");
            byte[] pic2Data = Encoding.UTF8.GetBytes("test png data");

            List <XSSFPictureData> pictures = wb.GetAllPictures() as List <XSSFPictureData>;

            Assert.AreEqual(0, pictures.Count);

            int pic1 = wb.AddPicture(pic1Data, XSSFWorkbook.PICTURE_TYPE_JPEG);
            int pic2 = wb.AddPicture(pic2Data, XSSFWorkbook.PICTURE_TYPE_PNG);

            XSSFSheet   sheet1   = wb.CreateSheet() as XSSFSheet;
            XSSFDrawing drawing1 = sheet1.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFPicture shape1   = drawing1.CreatePicture(new XSSFClientAnchor(), pic1) as XSSFPicture;
            XSSFPicture shape2   = drawing1.CreatePicture(new XSSFClientAnchor(), pic2) as XSSFPicture;

            XSSFSheet   sheet2   = wb.CreateSheet() as XSSFSheet;
            XSSFDrawing drawing2 = sheet2.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFPicture shape3   = drawing2.CreatePicture(new XSSFClientAnchor(), pic2) as XSSFPicture;
            XSSFPicture shape4   = drawing2.CreatePicture(new XSSFClientAnchor(), pic1) as XSSFPicture;

            Assert.AreEqual(2, pictures.Count);

            wb       = XSSFTestDataSamples.WriteOutAndReadBack(wb) as XSSFWorkbook;
            pictures = wb.GetAllPictures() as List <XSSFPictureData>;
            Assert.AreEqual(2, pictures.Count);

            sheet1   = wb.GetSheetAt(0) as XSSFSheet;
            drawing1 = sheet1.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFPicture shape11 = (XSSFPicture)drawing1.GetShapes()[0];

            Assert.IsTrue(Arrays.Equals(shape1.PictureData.Data, shape11.PictureData.Data));
            XSSFPicture shape22 = (XSSFPicture)drawing1.GetShapes()[0];

            Assert.IsTrue(Arrays.Equals(shape2.PictureData.Data, shape22.PictureData.Data));

            sheet2   = wb.GetSheetAt(1) as XSSFSheet;
            drawing2 = sheet2.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFPicture shape33 = (XSSFPicture)drawing2.GetShapes()[0];

            Assert.IsTrue(Arrays.Equals(shape3.PictureData.Data, shape33.PictureData.Data));
            XSSFPicture shape44 = (XSSFPicture)drawing2.GetShapes()[1];

            Assert.IsTrue(Arrays.Equals(shape4.PictureData.Data, shape44.PictureData.Data));
        }
Beispiel #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 NPOI.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);

            NPOI.OpenXmlFormats.Dml.Spreadsheet.CT_Picture ctShape = ctAnchor.AddNewPic();
            ctShape.Set(XSSFPicture.Prototype());

            ctShape.nvPicPr.cNvPr.id = (uint)shapeId;

            XSSFPicture shape = new XSSFPicture(this, ctShape);

            shape.anchor = anchor;
            shape.SetPictureReference(rel);
            return(shape);
        }
Beispiel #11
0
        public void TestShapeId()
        {
            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);
        }
Beispiel #12
0
    /**
 *
 * @return list of shapes in this drawing
 */
    public List<XSSFShape> GetShapes()
    {
        List<XSSFShape> lst = new List<XSSFShape>();
        foreach (IEG_Anchor anchor in drawing.CellAnchors)
        {
            XSSFShape shape = null;
            if (anchor.picture != null)
            {
                shape = new XSSFPicture(this, anchor.picture);
            }
            else if (anchor.connector != null)
            {
                shape = new XSSFConnector(this, anchor.connector);
            }
            else if (anchor.groupShape != null)
            {
                shape = new XSSFShapeGroup(this, anchor.groupShape);
            }
            else if (anchor.graphicFrame != null)
            {
                shape = new XSSFGraphicFrame(this, anchor.graphicFrame);
            }
            else if (anchor.sp != null)
            {
               shape = new XSSFSimpleShape(this, anchor.sp);
            }
            if (shape != null)
            {
                shape.anchor = GetAnchorFromIEGAnchor(anchor);
                lst.Add(shape);
            }
        }
        //foreach (XmlNode obj in xmldoc.SelectNodes("./*/*/*"))
        //{
        //    XSSFShape shape = null;
        //    if (obj.LocalName == "sp")
        //    {
        //        shape = new XSSFSimpleShape(this, obj);
        //    }
        //    else if (obj.LocalName == "pic")
        //    {
        //        shape = new XSSFPicture(this, obj);
        //    }
        //    else if (obj.LocalName == "cxnSp")
        //    {
        //        shape = new XSSFConnector(this, obj);
        //    }
        //    //    else if (obj is CT_GraphicalObjectFrame) shape = new XSSFGraphicFrame(this, (CT_GraphicalObjectFrame)obj);
        //    //    else if (obj is CT_GroupShape) shape = new XSSFShapeGroup(this, (CT_GroupShape)obj);
        //    if (shape != null)
        //    {
        //        shape.anchor = GetAnchorFromParent(obj);
        //        lst.Add(shape);
        //    }
        //}
        return lst;
    }
Beispiel #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.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;
        }
Beispiel #14
0
        public IClientAnchor GetPreferredSize(double scale)
        {
            XSSFClientAnchor anchor         = (XSSFClientAnchor)this.GetAnchor();
            XSSFPictureData  pictureData    = (XSSFPictureData)this.PictureData;
            Size             imageDimension = XSSFPicture.GetImageDimension(pictureData.GetPackagePart(), pictureData.GetPictureType());
            double           num1           = (double)imageDimension.Width * scale;
            double           num2           = (double)imageDimension.Height * scale;
            float            num3           = 0.0f;
            int col1 = anchor.Col1;
            int num4 = 0;

            while (true)
            {
                num3 += this.GetColumnWidthInPixels(col1);
                if ((double)num3 <= num1)
                {
                    ++col1;
                }
                else
                {
                    break;
                }
            }
            if ((double)num3 > num1)
            {
                double columnWidthInPixels = (double)this.GetColumnWidthInPixels(col1);
                double num5 = (double)num3 - num1;
                num4 = (int)((double)XSSFShape.EMU_PER_PIXEL * (columnWidthInPixels - num5));
            }
            anchor.Col2 = col1;
            anchor.Dx2  = num4;
            double num6 = 0.0;
            int    row1 = anchor.Row1;
            int    num7 = 0;

            while (true)
            {
                num6 += (double)this.GetRowHeightInPixels(row1);
                if (num6 <= num2)
                {
                    ++row1;
                }
                else
                {
                    break;
                }
            }
            if (num6 > num2)
            {
                double rowHeightInPixels = (double)this.GetRowHeightInPixels(row1);
                double num5 = num6 - num2;
                num7 = (int)((double)XSSFShape.EMU_PER_PIXEL * (rowHeightInPixels - num5));
            }
            anchor.Row2 = row1;
            anchor.Dy2  = num7;
            CT_PositiveSize2D ext = this.ctPicture.spPr.xfrm.ext;

            ext.cx = (long)(num1 * (double)XSSFShape.EMU_PER_PIXEL);
            ext.cy = (long)(num2 * (double)XSSFShape.EMU_PER_PIXEL);
            return((IClientAnchor)anchor);
        }
Beispiel #15
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;
        }