Exemplo n.º 1
0
        /**
         * Adds a picture to the run. This method handles
         *  attaching the picture data to the overall file.
         *
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_EMF
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_WMF
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_PICT
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_JPEG
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_PNG
         * @see Npoi.Core.XWPF.UserModel.Document#PICTURE_TYPE_DIB
         *
         * @param pictureData The raw picture data
         * @param pictureType The type of the picture, eg {@link Document#PICTURE_TYPE_JPEG}
         * @param width width in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}
         * @param height height in EMUs. To convert to / from points use {@link org.apache.poi.util.Units}
         * @throws Npoi.Core.Openxml4j.exceptions.InvalidFormatException
         * @throws IOException
         */
        public XWPFPicture AddPicture(Stream pictureData, int pictureType, String filename, int width, int height)
        {
            XWPFDocument doc = parent.Document;

            // Add the picture + relationship
            String          relationId = doc.AddPictureData(pictureData, pictureType);
            XWPFPictureData picData    = (XWPFPictureData)doc.GetRelationById(relationId);

            // Create the Drawing entry for it
            CT_Drawing Drawing = run.AddNewDrawing();
            CT_Inline  inline  = Drawing.AddNewInline();

            // Do the fiddly namespace bits on the inline
            // (We need full control of what goes where and as what)
            //CT_GraphicalObject tmp = new CT_GraphicalObject();
            //String xml =
            //    "<a:graphic xmlns:a=\"" + "http://schemas.openxmlformats.org/drawingml/2006/main" + "\">" +
            //    "<a:graphicData uri=\"" + "http://schemas.openxmlformats.org/drawingml/2006/picture" + "\">" +
            //    "<pic:pic xmlns:pic=\"" + "http://schemas.openxmlformats.org/drawingml/2006/picture" + "\" />" +
            //    "</a:graphicData>" +
            //    "</a:graphic>";
            //inline.Set((xml));

            XDocument xmlDoc = new XDocument();

            //XmlElement el = xmlDoc.CreateElement("pic", "pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

            inline.graphic                 = new CT_GraphicalObject();
            inline.graphic.graphicData     = new CT_GraphicalObjectData();
            inline.graphic.graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";


            // Setup the inline
            inline.distT = (0);
            inline.distR = (0);
            inline.distB = (0);
            inline.distL = (0);

            Npoi.Core.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
            long id = parent.Document.DrawingIdManager.ReserveNew();

            docPr.id = (uint)(id);
            /* This name is not visible in Word 2010 anywhere. */
            docPr.name  = ("Drawing " + id);
            docPr.descr = (filename);

            Npoi.Core.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();
            extent.cx = (width);
            extent.cy = (height);

            // Grab the picture object
            Npoi.Core.OpenXmlFormats.Dml.Picture.CT_Picture pic = new OpenXmlFormats.Dml.Picture.CT_Picture();

            // Set it up
            Npoi.Core.OpenXmlFormats.Dml.Picture.CT_PictureNonVisual nvPicPr = pic.AddNewNvPicPr();

            Npoi.Core.OpenXmlFormats.Dml.CT_NonVisualDrawingProps cNvPr = nvPicPr.AddNewCNvPr();
            /* use "0" for the id. See ECM-576, 20.2.2.3 */
            cNvPr.id = (0);
            /* This name is not visible in Word 2010 anywhere */
            cNvPr.name  = ("Picture " + id);
            cNvPr.descr = (filename);

            CT_NonVisualPictureProperties cNvPicPr = nvPicPr.AddNewCNvPicPr();

            cNvPicPr.AddNewPicLocks().noChangeAspect = true;

            CT_BlipFillProperties blipFill = pic.AddNewBlipFill();
            CT_Blip blip = blipFill.AddNewBlip();

            blip.embed = (picData.GetPackageRelationship().Id);
            blipFill.AddNewStretch().AddNewFillRect();

            CT_ShapeProperties spPr = pic.AddNewSpPr();
            CT_Transform2D     xfrm = spPr.AddNewXfrm();

            CT_Point2D off = xfrm.AddNewOff();

            off.x = (0);
            off.y = (0);

            Npoi.Core.OpenXmlFormats.Dml.CT_PositiveSize2D ext = xfrm.AddNewExt();
            ext.cx = (width);
            ext.cy = (height);

            CT_PresetGeometry2D prstGeom = spPr.AddNewPrstGeom();

            prstGeom.prst = (ST_ShapeType.rect);
            prstGeom.AddNewAvLst();

            using (var ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                pic.Write(sw, "pic:pic");
                sw.Flush();
                ms.Position = 0;
                var sr     = new StreamReader(ms);
                var picXml = sr.ReadToEnd();
                inline.graphic.graphicData.AddPicElement(picXml);
            }
            // Finish up
            XWPFPicture xwpfPicture = new XWPFPicture(pic, this);

            pictures.Add(xwpfPicture);
            return(xwpfPicture);
        }
Exemplo n.º 2
0
        /**
         * @param r the CT_R bean which holds the run.attributes
         * @param p the parent paragraph
         */
        public XWPFRun(CT_R r, IRunBody p)
        {
            this.run    = r;
            this.parent = p;

            /**
             * reserve already occupied Drawing ids, so reserving new ids later will
             * not corrupt the document
             */
            IList <CT_Drawing> drawingList = r.GetDrawingList();

            foreach (CT_Drawing ctDrawing in drawingList)
            {
                List <CT_Anchor> anchorList = ctDrawing.GetAnchorList();
                foreach (CT_Anchor anchor in anchorList)
                {
                    if (anchor.docPr != null)
                    {
                        this.Document.DrawingIdManager.Reserve(anchor.docPr.id);
                    }
                }
                List <CT_Inline> inlineList = ctDrawing.GetInlineList();
                foreach (CT_Inline inline in inlineList)
                {
                    if (inline.docPr != null)
                    {
                        this.Document.DrawingIdManager.Reserve(inline.docPr.id);
                    }
                }
            }

            //// Look for any text in any of our pictures or Drawings
            StringBuilder text         = new StringBuilder();
            List <object> pictTextObjs = new List <object>();

            foreach (CT_Picture pic in r.GetPictList())
            {
                pictTextObjs.Add(pic);
            }
            foreach (CT_Drawing draw in drawingList)
            {
                pictTextObjs.Add(draw);
            }
            //foreach (object o in pictTextObjs)
            //{
            //todo:: imlement this
            //XmlObject[] t = o.SelectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
            //for (int m = 0; m < t.Length; m++)
            //{
            //    NodeList kids = t[m].DomNode.ChildNodes;
            //    for (int n = 0; n < kids.Length; n++)
            //    {
            //        if (kids.Item(n) is Text)
            //        {
            //            if (text.Length > 0)
            //                text.Append("\n");
            //            text.Append(kids.Item(n).NodeValue);
            //        }
            //    }
            //}
            //}
            pictureText = text.ToString();

            // Do we have any embedded pictures?
            // (They're a different CT_Picture, under the Drawingml namespace)
            pictures = new List <XWPFPicture>();
            foreach (object o in pictTextObjs)
            {
                foreach (OpenXmlFormats.Dml.Picture.CT_Picture pict in GetCTPictures(o))
                {
                    XWPFPicture picture = new XWPFPicture(pict, this);
                    pictures.Add(picture);
                }
            }
        }
Exemplo n.º 3
0
        public void TestPictures()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("VariousPictures.docx");

            Assert.AreEqual(7, doc.Paragraphs.Count);

            XWPFParagraph p;
            XWPFRun       r;

            // Text paragraphs
            Assert.AreEqual("Sheet with various pictures", doc.Paragraphs[0].Text);
            Assert.AreEqual("(jpeg, png, wmf, emf and pict) ", doc.Paragraphs[1].Text);

            // Spacer ones
            Assert.AreEqual("", doc.Paragraphs[2].Text);
            Assert.AreEqual("", doc.Paragraphs[3].Text);
            Assert.AreEqual("", doc.Paragraphs[4].Text);

            // Image one
            p = doc.Paragraphs[5];
            Assert.AreEqual(6, p.Runs.Count);

            r = p.Runs[0];
            Assert.AreEqual("", r.ToString());
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
            Assert.IsNotNull(r.GetEmbeddedPictures()[0].GetPictureData());
            Assert.AreEqual("image1.wmf", r.GetEmbeddedPictures()[0].GetPictureData().FileName);

            r = p.Runs[1];
            Assert.AreEqual("", r.ToString());
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
            Assert.IsNotNull(r.GetEmbeddedPictures()[0].GetPictureData());
            Assert.AreEqual("image2.png", r.GetEmbeddedPictures()[0].GetPictureData().FileName);

            r = p.Runs[2];
            Assert.AreEqual("", r.ToString());
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
            Assert.IsNotNull(r.GetEmbeddedPictures()[0].GetPictureData());
            Assert.AreEqual("image3.emf", r.GetEmbeddedPictures()[0].GetPictureData().FileName);

            r = p.Runs[3];
            Assert.AreEqual("", r.ToString());
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
            Assert.IsNotNull(r.GetEmbeddedPictures()[0].GetPictureData());
            Assert.AreEqual("image4.emf", r.GetEmbeddedPictures()[0].GetPictureData().FileName);

            r = p.Runs[4];
            Assert.AreEqual("", r.ToString());
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
            Assert.IsNotNull(r.GetEmbeddedPictures()[0].GetPictureData());
            Assert.AreEqual("image5.jpeg", r.GetEmbeddedPictures()[0].GetPictureData().FileName);

            r = p.Runs[5];
            //Is there a bug about XmlSerializer? it can not Deserialize the tag which inner text is only one whitespace
            //e.g. <w:t> </w:t> to CT_Text;
            //TODO
            Assert.AreEqual(" ", r.ToString());
            Assert.AreEqual(0, r.GetEmbeddedPictures().Count);

            // Final spacer
            Assert.AreEqual("", doc.Paragraphs[(6)].Text);


            // Look in detail at one
            r = p.Runs[4];
            XWPFPicture pict = r.GetEmbeddedPictures()[0];

            //CT_Picture picture = pict.GetCTPicture();
            Npoi.Core.OpenXmlFormats.Dml.Picture.CT_Picture picture = pict.GetCTPicture();
            //Assert.Fail("picture.blipFill.blip.embed is missing from wordprocessing CT_Picture.");
            Assert.AreEqual("rId8", picture.blipFill.blip.embed);

            // Ensure that the ooxml compiler Finds everything we need
            r.GetCTR().GetDrawingArray(0);
            r.GetCTR().GetDrawingArray(0).GetInlineArray(0);
            Npoi.Core.OpenXmlFormats.Dml.CT_GraphicalObject     go  = r.GetCTR().GetDrawingArray(0).GetInlineArray(0).graphic;
            Npoi.Core.OpenXmlFormats.Dml.CT_GraphicalObjectData god = r.GetCTR().GetDrawingArray(0).GetInlineArray(0).graphic.graphicData;
            //PicDocument pd = new PicDocumentImpl(null);
            //assertTrue(pd.isNil());
        }