public void TestRead()
        {
            XSSFWorkbook wb       = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            IList        pictures = wb.GetAllPictures();

            //wb.GetAllPictures() should return the same instance across multiple calls
            Assert.AreSame(pictures, wb.GetAllPictures());

            Assert.AreEqual(5, pictures.Count);
            String[] ext      = { "jpeg", "emf", "png", "emf", "wmf" };
            String[] mimetype = { "image/jpeg", "image/x-emf", "image/png", "image/x-emf", "image/x-wmf" };
            for (int i = 0; i < pictures.Count; i++)
            {
                Assert.AreEqual(ext[i], ((XSSFPictureData)pictures[i]).SuggestFileExtension());
                Assert.AreEqual(mimetype[i], ((XSSFPictureData)pictures[i]).MimeType);
            }

            int num = pictures.Count;

            byte[] pictureData = { 0xA, 0xB, 0XC, 0xD, 0xE, 0xF };

            int idx = wb.AddPicture(pictureData, PictureType.JPEG);

            Assert.AreEqual(num + 1, pictures.Count);
            //idx is 0-based index in the #pictures array
            Assert.AreEqual(pictures.Count - 1, idx);
            XSSFPictureData pict = (XSSFPictureData)pictures[idx];

            Assert.AreEqual("jpeg", pict.SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(pictureData, pict.Data));
        }
        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);
                    }
                }
            }
        }
Exemple #3
0
        internal PackageRelationship AddPictureReference(int pictureIndex)
        {
            XSSFPictureData     allPicture = (XSSFPictureData)((XSSFWorkbook)this.GetParent().GetParent()).GetAllPictures()[pictureIndex];
            PackageRelationship rel        = this.GetPackagePart().AddRelationship(allPicture.GetPackagePart().PartName, TargetMode.Internal, XSSFRelation.IMAGES.Relation);

            this.AddRelation(rel.Id, (POIXMLDocumentPart) new XSSFPictureData(allPicture.GetPackagePart(), rel));
            return(rel);
        }
Exemple #4
0
        //public XSSFChart CreateChart(IClientAnchor anchor)
        //{
        //    return CreateChart((XSSFClientAnchor)anchor);
        //}

        /**
         * Add the indexed picture to this Drawing relations
         *
         * @param pictureIndex the index of the picture in the workbook collection of pictures,
         *   {@link NPOI.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
         */
        internal PackageRelationship AddPictureReference(int pictureIndex)
        {
            XSSFWorkbook    wb   = (XSSFWorkbook)GetParent().GetParent();
            XSSFPictureData data = (XSSFPictureData)wb.GetAllPictures()[pictureIndex];
            XSSFPictureData pic  = new XSSFPictureData(data.GetPackagePart());
            RelationPart    rp   = AddRelation(null, XSSFRelation.IMAGES, pic);

            return(rp.Relationship);
        }
Exemple #5
0
        //public XSSFChart CreateChart(IClientAnchor anchor)
        //{
        //    return CreateChart((XSSFClientAnchor)anchor);
        //}

        /**
         * Add the indexed picture to this Drawing relations
         *
         * @param pictureIndex the index of the picture in the workbook collection of pictures,
         *   {@link NPOI.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
         */
        internal PackageRelationship AddPictureReference(int pictureIndex)
        {
            XSSFWorkbook        wb     = (XSSFWorkbook)GetParent().GetParent();
            XSSFPictureData     data   = (XSSFPictureData)wb.GetAllPictures()[pictureIndex];
            PackagePartName     ppName = data.GetPackagePart().PartName;
            PackageRelationship rel    = GetPackagePart().AddRelationship(ppName, TargetMode.Internal, XSSFRelation.IMAGES.Relation);

            AddRelation(rel.Id, new XSSFPictureData(data.GetPackagePart(), rel));
            return(rel);
        }
Exemple #6
0
        public int AddPicture(Stream picStream, int format)
        {
            int             idx          = this.GetAllPictures().Count + 1;
            XSSFPictureData relationship = (XSSFPictureData)this.CreateRelationship(XSSFPictureData.RELATIONS[format], (POIXMLFactory)XSSFFactory.GetInstance(), idx, true);
            Stream          outputStream = relationship.GetPackagePart().GetOutputStream();

            IOUtils.Copy(picStream, outputStream);
            outputStream.Close();
            this.pictures.Add(relationship);
            return(idx - 1);
        }
Exemple #7
0
        public int AddPicture(byte[] pictureData, PictureType format)
        {
            int             idx          = this.GetAllPictures().Count + 1;
            XSSFPictureData relationship = (XSSFPictureData)this.CreateRelationship(XSSFPictureData.RELATIONS[(int)format], (POIXMLFactory)XSSFFactory.GetInstance(), idx, true);

            try
            {
                Stream outputStream = relationship.GetPackagePart().GetOutputStream();
                outputStream.Write(pictureData, 0, pictureData.Length);
                outputStream.Close();
            }
            catch (IOException ex)
            {
                throw new POIXMLException((Exception)ex);
            }
            this.pictures.Add(relationship);
            return(idx - 1);
        }
Exemple #8
0
        public void Bug47668()
        {
            XSSFWorkbook workbook    = XSSFTestDataSamples.OpenSampleWorkbook("47668.xlsx");
            IList        allPictures = workbook.GetAllPictures();

            Assert.AreEqual(1, allPictures.Count);

            PackagePartName imagePartName = PackagingUriHelper
                                            .CreatePartName("/xl/media/image1.jpeg");
            PackagePart imagePart = workbook.Package.GetPart(imagePartName);

            Assert.IsNotNull(imagePart);

            foreach (XSSFPictureData pictureData in allPictures)
            {
                PackagePart picturePart = pictureData.GetPackagePart();
                Assert.AreSame(imagePart, picturePart);
            }

            XSSFSheet       sheet0       = (XSSFSheet)workbook.GetSheetAt(0);
            XSSFDrawing     Drawing0     = (XSSFDrawing)sheet0.CreateDrawingPatriarch();
            XSSFPictureData pictureData0 = (XSSFPictureData)Drawing0.GetRelations()[0];

            byte[] data0 = pictureData0.Data;
            CRC32  crc0  = new CRC32();

            crc0.Update(data0);

            XSSFSheet       sheet1       = workbook.GetSheetAt(1) as XSSFSheet;
            XSSFDrawing     Drawing1     = sheet1.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFPictureData pictureData1 = (XSSFPictureData)Drawing1.GetRelations()[0];

            byte[] data1 = pictureData1.Data;
            CRC32  crc1  = new CRC32();

            crc1.Update(data1);

            Assert.AreEqual(crc0.Value, crc1.Value);
            workbook.Close();
        }
Exemple #9
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);
        }
Exemple #10
0
        /**
         * Return the dimension of the embedded image in pixel
         *
         * @return image dimension in pixels
         */
        public Size GetImageDimension()
        {
            XSSFPictureData picData = PictureData as XSSFPictureData;

            return(GetImageDimension(picData.GetPackagePart(), picData.PictureType));
        }
Exemple #11
0
        /**
         * Calculate the preferred size for this picture.
         *
         * @param scale the amount by which image dimensions are multiplied relative to the original size.
         * @return XSSFClientAnchor with the preferred size for this image
         */
        public IClientAnchor GetPreferredSize(double scale)
        {
            XSSFClientAnchor anchor = (XSSFClientAnchor)GetAnchor();

            XSSFPictureData data         = (XSSFPictureData)this.PictureData;
            Size            size         = GetImageDimension(data.GetPackagePart(), data.GetPictureType());
            double          scaledWidth  = size.Width * scale;
            double          scaledHeight = size.Height * scale;

            float w    = 0;
            int   col2 = anchor.Col1;
            int   dx2  = 0;

            for (; ;)
            {
                w += GetColumnWidthInPixels(col2);
                if (w > scaledWidth)
                {
                    break;
                }
                col2++;
            }

            if (w > scaledWidth)
            {
                double cw    = GetColumnWidthInPixels(col2);
                double delta = w - scaledWidth;
                dx2 = (int)(EMU_PER_PIXEL * (cw - delta));
            }
            anchor.Col2 = (col2);
            anchor.Dx2  = (dx2);

            double h    = 0;
            int    row2 = anchor.Row1;
            int    dy2  = 0;

            for (; ;)
            {
                h += GetRowHeightInPixels(row2);
                if (h > scaledHeight)
                {
                    break;
                }
                row2++;
            }

            if (h > scaledHeight)
            {
                double ch    = GetRowHeightInPixels(row2);
                double delta = h - scaledHeight;
                dy2 = (int)(EMU_PER_PIXEL * (ch - delta));
            }
            anchor.Row2 = (row2);
            anchor.Dy2  = (dy2);

            CT_PositiveSize2D size2d = ctPicture.spPr.xfrm.ext;

            size2d.cx = ((long)(scaledWidth * EMU_PER_PIXEL));
            size2d.cy = ((long)(scaledHeight * EMU_PER_PIXEL));

            return(anchor);
        }