Esempio n. 1
0
        /**
         * Adds a picture to the document.
         *
         * @param pictureData       The picture data
         * @param format            The format of the picture.
         *
         * @return the index to this picture (0 based), the Added picture can be obtained from {@link #getAllPictures()} .
         * @throws InvalidFormatException
         */
        public String AddPictureData(byte[] pictureData, int format)
        {
            XWPFPictureData xwpfPicData = document.FindPackagePictureData(pictureData, format);
            POIXMLRelation  relDesc     = XWPFPictureData.RELATIONS[format];

            if (xwpfPicData == null)
            {
                /* Part doesn't exist, create a new one */
                int idx = document.GetNextPicNameNumber(format);
                xwpfPicData = (XWPFPictureData)CreateRelationship(relDesc, XWPFFactory.GetInstance(), idx);
                /* write bytes to new part */
                PackagePart picDataPart = xwpfPicData.GetPackagePart();
                Stream      out1        = null;
                try
                {
                    out1 = picDataPart.GetOutputStream();
                    out1.Write(pictureData, 0, pictureData.Length);
                }
                catch (IOException e)
                {
                    throw new POIXMLException(e);
                }
                finally
                {
                    try
                    {
                        if (out1 != null)
                        {
                            out1.Close();
                        }
                    }
                    catch (IOException)
                    {
                        // ignore
                    }
                }

                document.RegisterPackagePictureData(xwpfPicData);
                pictures.Add(xwpfPicData);
                return(GetRelationId(xwpfPicData));
            }
            else if (!GetRelations().Contains(xwpfPicData))
            {
                /*
                 * Part already existed, but was not related so far. Create
                 * relationship to the already existing part and update
                 * POIXMLDocumentPart data.
                 */
                PackagePart picDataPart = xwpfPicData.GetPackagePart();
                // TODO add support for TargetMode.EXTERNAL relations.
                RelationPart rp = AddRelation(null, XWPFRelation.IMAGES, xwpfPicData);
                pictures.Add(xwpfPicData);
                return(rp.Relationship.Id);
            }
            else
            {
                /* Part already existed, Get relation id and return it */
                return(GetRelationId(xwpfPicData));
            }
        }
Esempio n. 2
0
        public void TestReadTextBox()
        {
            XSSFWorkbook wb    = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            XSSFSheet    sheet = wb.GetSheetAt(0) as XSSFSheet;
            //the sheet has one relationship and it is XSSFDrawing
            List <RelationPart> rels = sheet.RelationParts;

            Assert.AreEqual(1, rels.Count);
            RelationPart rp = rels[0];

            Assert.IsTrue(rp.DocumentPart is XSSFDrawing);

            XSSFDrawing drawing = (XSSFDrawing)rp.DocumentPart;

            //sheet.CreateDrawingPatriarch() should return the same instance of XSSFDrawing
            Assert.AreSame(drawing, sheet.CreateDrawingPatriarch());
            String drawingId = rp.Relationship.Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);

            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(6, shapes.Count);

            Assert.IsTrue(shapes[4] is XSSFSimpleShape);

            XSSFSimpleShape textbox = (XSSFSimpleShape)shapes[4];

            Assert.AreEqual("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.Text);

            checkRewrite(wb);
            wb.Close();
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /**
         * Returns the target {@link POIXMLDocumentPart}, where a
         * {@link PackageRelationship} is set from the {@link PackagePart} of this
         * {@link POIXMLDocumentPart} to the {@link PackagePart} of the target
         * {@link POIXMLDocumentPart} with a {@link PackageRelationship#GetId()}
         * matching the given parameter value.
         *
         * @param id
         *            The relation id to look for
         * @return the target part of the relation, or null, if none exists
         */
        public POIXMLDocumentPart GetRelationById(String id)
        {
            if (string.IsNullOrEmpty(id) || !relations.ContainsKey(id))
            {
                return(null);
            }

            RelationPart rp = relations[id];

            return((rp == null) ? null : rp.DocumentPart);
        }
Esempio n. 5
0
        private static RelationPart CreateRelationPart(Relation relation, Column column, RelationPartType type)
        {
            var relationPart = new RelationPart
            {
                Relation = relation,
                Column   = column,
                Type     = type
            };

            column.RelationParts.Add(relationPart);

            return(relationPart);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a chart.
        /// </summary>
        /// <param name="anchor">the client anchor describes how this chart is attached to</param>
        /// <returns>the newly created chart</returns>
        public IChart CreateChart(IClientAnchor anchor)
        {
            int chartNumber = GetPackagePart().Package.
                              GetPartsByContentType(XSSFRelation.CHART.ContentType).Count + 1;

            RelationPart rp = CreateRelationship(
                XSSFRelation.CHART, XSSFFactory.GetInstance(), chartNumber, false);
            XSSFChart chart      = rp.DocumentPart as XSSFChart;
            String    chartRelId = rp.Relationship.Id;

            XSSFGraphicFrame frame = CreateGraphicFrame((XSSFClientAnchor)anchor);

            frame.SetChart(chart, chartRelId);

            return(chart);
        }
Esempio n. 7
0
        public void TestRead()
        {
            XSSFWorkbook wb    = XSSFTestDataSamples.OpenSampleWorkbook("WithDrawing.xlsx");
            XSSFSheet    sheet = (XSSFSheet)wb.GetSheetAt(0);
            //the sheet has one relationship and it is XSSFDrawing
            List <RelationPart> rels = sheet.RelationParts;

            Assert.AreEqual(1, rels.Count);
            RelationPart rp = rels[0];

            Assert.IsTrue(rp.DocumentPart is XSSFDrawing);

            XSSFDrawing drawing = (XSSFDrawing)rp.DocumentPart;

            //sheet.CreateDrawingPatriarch() should return the same instance of XSSFDrawing
            Assert.AreSame(drawing, sheet.CreateDrawingPatriarch());
            String drawingId = rp.Relationship.Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);


            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(6, shapes.Count);

            Assert.IsTrue(shapes[(0)] is XSSFPicture);
            Assert.IsTrue(shapes[(1)] is XSSFPicture);
            Assert.IsTrue(shapes[(2)] is XSSFPicture);
            Assert.IsTrue(shapes[(3)] is XSSFPicture);
            Assert.IsTrue(shapes[(4)] is XSSFSimpleShape);
            Assert.IsTrue(shapes[(5)] is XSSFPicture);

            foreach (XSSFShape sh in shapes)
            {
                Assert.IsNotNull(sh.GetAnchor());
            }

            checkRewrite(wb);
            wb.Close();
        }
Esempio n. 8
0
        public void TestNew()
        {
            XSSFWorkbook wb1   = new XSSFWorkbook();
            XSSFSheet    sheet = (XSSFSheet)wb1.CreateSheet();
            //multiple calls of CreateDrawingPatriarch should return the same instance of XSSFDrawing
            XSSFDrawing dr1 = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFDrawing dr2 = (XSSFDrawing)sheet.CreateDrawingPatriarch();

            Assert.AreSame(dr1, dr2);

            List <RelationPart> rels = sheet.RelationParts;

            Assert.AreEqual(1, rels.Count);
            RelationPart rp = rels[0];

            Assert.IsTrue(rp.DocumentPart is XSSFDrawing);

            XSSFDrawing drawing   = (XSSFDrawing)rp.DocumentPart;
            String      drawingId = rp.Relationship.Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);

            //XSSFClientAnchor anchor = new XSSFClientAnchor();

            XSSFConnector c1 = drawing.CreateConnector(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 2, 2));

            c1.LineWidth = 2.5;
            c1.LineStyle = LineStyle.DashDotSys;

            XSSFShapeGroup c2 = drawing.CreateGroup(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 5, 5));

            Assert.IsNotNull(c2);

            XSSFSimpleShape c3 = drawing.CreateSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));

            c3.SetText(new XSSFRichTextString("Test String"));
            c3.SetFillColor(128, 128, 128);

            XSSFTextBox        c4 = (XSSFTextBox)drawing.CreateTextbox(new XSSFClientAnchor(0, 0, 0, 0, 4, 4, 5, 6));
            XSSFRichTextString rt = new XSSFRichTextString("Test String");

            rt.ApplyFont(0, 5, wb1.CreateFont());
            rt.ApplyFont(5, 6, wb1.CreateFont());
            c4.SetText(rt);

            c4.IsNoFill = (true);

            Assert.AreEqual(4, drawing.GetCTDrawing().SizeOfTwoCellAnchorArray());

            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(4, shapes.Count);
            Assert.IsTrue(shapes[(0)] is XSSFConnector);
            Assert.IsTrue(shapes[(1)] is XSSFShapeGroup);
            Assert.IsTrue(shapes[(2)] is XSSFSimpleShape);
            Assert.IsTrue(shapes[(3)] is XSSFSimpleShape);

            // Save and re-load it
            XSSFWorkbook wb2 = XSSFTestDataSamples.WriteOutAndReadBack(wb1) as XSSFWorkbook;

            wb1.Close();

            sheet = wb2.GetSheetAt(0) as XSSFSheet;

            // Check
            dr1 = sheet.CreateDrawingPatriarch() as XSSFDrawing;
            CT_Drawing ctDrawing = dr1.GetCTDrawing();

            // Connector, shapes and text boxes are all two cell anchors
            Assert.AreEqual(0, ctDrawing.SizeOfAbsoluteAnchorArray());
            Assert.AreEqual(0, ctDrawing.SizeOfOneCellAnchorArray());
            Assert.AreEqual(4, ctDrawing.SizeOfTwoCellAnchorArray());

            shapes = dr1.GetShapes();
            Assert.AreEqual(4, shapes.Count);
            Assert.IsTrue(shapes[0] is XSSFConnector);
            Assert.IsTrue(shapes[1] is XSSFShapeGroup);
            Assert.IsTrue(shapes[2] is XSSFSimpleShape);
            Assert.IsTrue(shapes[3] is XSSFSimpleShape); //

            // Ensure it got the right namespaces
            //String xml = ctDrawing.ToString();
            //Assert.IsTrue(xml.Contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
            //Assert.IsTrue(xml.Contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));

            checkRewrite(wb2);
            wb2.Close();
        }
Esempio n. 9
0
        public void TestReadTextBoxParagraphs()
        {
            XSSFWorkbook wb    = XSSFTestDataSamples.OpenSampleWorkbook("WithTextBox.xlsx");
            XSSFSheet    sheet = wb.GetSheetAt(0) as XSSFSheet;
            //the sheet has one relationship and it is XSSFDrawing
            List <RelationPart> rels = sheet.RelationParts;

            Assert.AreEqual(1, rels.Count);
            RelationPart rp = rels[0];

            Assert.IsTrue(rp.DocumentPart is XSSFDrawing);

            XSSFDrawing drawing = (XSSFDrawing)rp.DocumentPart;

            //sheet.CreateDrawingPatriarch() should return the same instance of XSSFDrawing
            Assert.AreSame(drawing, sheet.CreateDrawingPatriarch());
            String drawingId = rp.Relationship.Id;

            //there should be a relation to this Drawing in the worksheet
            Assert.IsTrue(sheet.GetCTWorksheet().IsSetDrawing());
            Assert.AreEqual(drawingId, sheet.GetCTWorksheet().drawing.id);

            List <XSSFShape> shapes = drawing.GetShapes();

            Assert.AreEqual(1, shapes.Count);

            Assert.IsTrue(shapes[0] is XSSFSimpleShape);

            XSSFSimpleShape textbox = (XSSFSimpleShape)shapes[0];

            List <XSSFTextParagraph> paras = textbox.TextParagraphs;

            Assert.AreEqual(3, paras.Count);

            Assert.AreEqual("Line 2", paras[1].Text);                // check content of second paragraph

            Assert.AreEqual("Line 1\nLine 2\nLine 3", textbox.Text); // check content of entire textbox

            // check attributes of paragraphs
            Assert.AreEqual(TextAlign.LEFT, paras[0].TextAlign);
            Assert.AreEqual(TextAlign.CENTER, paras[1].TextAlign);
            Assert.AreEqual(TextAlign.RIGHT, paras[2].TextAlign);

            Color clr = paras[0].TextRuns[0].FontColor;

            Assert.IsTrue(Arrays.Equals(
                              new int[] { 255, 0, 0 },
                              new int[] { clr.R, clr.G, clr.B }));

            clr = paras[1].TextRuns[0].FontColor;
            Assert.IsTrue(Arrays.Equals(
                              new int[] { 0, 255, 0 },
                              new int[] { clr.R, clr.G, clr.B }));

            clr = paras[2].TextRuns[0].FontColor;
            Assert.IsTrue(Arrays.Equals(
                              new int[] { 0, 0, 255 },
                              new int[] { clr.R, clr.G, clr.B }));

            checkRewrite(wb);
            wb.Close();
        }