Exemple #1
0
        public XWPFPicture AddPicture(Stream pictureData, int pictureType, string filename, int width, int height)
        {
            XWPFDocument    document     = this.paragraph.GetDocument();
            string          id           = document.AddPictureData(pictureData, pictureType);
            XWPFPictureData relationById = (XWPFPictureData)document.GetRelationById(id);

            try
            {
                CT_Inline  ctInline = this.run.AddNewDrawing().AddNewInline();
                XmlElement element  = new XmlDocument().CreateElement("pic", "pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
                ctInline.graphic             = new CT_GraphicalObject();
                ctInline.graphic.graphicData = new CT_GraphicalObjectData();
                ctInline.graphic.graphicData.AddPicElement((XmlElement)element.Clone());
                ctInline.distT = 0U;
                ctInline.distR = 0U;
                ctInline.distB = 0U;
                ctInline.distL = 0U;
                CT_NonVisualDrawingProps visualDrawingProps1 = ctInline.AddNewDocPr();
                long num = this.GetParagraph().GetDocument().GetDrawingIdManager().ReserveNew();
                visualDrawingProps1.id    = (uint)num;
                visualDrawingProps1.name  = "Drawing " + (object)num;
                visualDrawingProps1.descr = filename;
                CT_PositiveSize2D ctPositiveSize2D1 = ctInline.AddNewExtent();
                ctPositiveSize2D1.cx = (long)width;
                ctPositiveSize2D1.cy = (long)height;
                NPOI.OpenXmlFormats.Dml.Picture.CT_Picture ctPicture = this.GetCTPictures((object)ctInline.graphic.graphicData)[0];
                CT_PictureNonVisual      pictureNonVisual            = ctPicture.AddNewNvPicPr();
                CT_NonVisualDrawingProps visualDrawingProps2         = pictureNonVisual.AddNewCNvPr();
                visualDrawingProps2.id    = 0U;
                visualDrawingProps2.name  = "Picture " + (object)num;
                visualDrawingProps2.descr = filename;
                pictureNonVisual.AddNewCNvPicPr().AddNewPicLocks().noChangeAspect = true;
                CT_BlipFillProperties blipFillProperties = ctPicture.AddNewBlipFill();
                blipFillProperties.AddNewBlip().embed    = relationById.GetPackageRelationship().Id;
                blipFillProperties.AddNewStretch().AddNewFillRect();
                CT_ShapeProperties ctShapeProperties = ctPicture.AddNewSpPr();
                CT_Transform2D     ctTransform2D     = ctShapeProperties.AddNewXfrm();
                CT_Point2D         ctPoint2D         = ctTransform2D.AddNewOff();
                ctPoint2D.x = 0L;
                ctPoint2D.y = 0L;
                CT_PositiveSize2D ctPositiveSize2D2 = ctTransform2D.AddNewExt();
                ctPositiveSize2D2.cx = (long)width;
                ctPositiveSize2D2.cy = (long)height;
                CT_PresetGeometry2D presetGeometry2D = ctShapeProperties.AddNewPrstGeom();
                presetGeometry2D.prst = ST_ShapeType.rect;
                presetGeometry2D.AddNewAvLst();
                XWPFPicture xwpfPicture = new XWPFPicture(ctPicture, this);
                this.pictures.Add(xwpfPicture);
                return(xwpfPicture);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("", ex);
            }
        }
Exemple #2
0
 public XWPFPicture AddSvg(Stream svgData, Stream altPictureData, int altPictureType, String filename, int width, int height)
 {
     return(AddPicture(altPictureData, altPictureType, filename, width, height, (doc, blip) =>
     {
         String relationId = doc.AddPictureData(svgData, (int)PictureType.SVG);
         XWPFPictureData picData = (XWPFPictureData)doc.GetRelationById(relationId);
         var extLst = new CT_OfficeArtExtensionList();
         var extItem = new CT_OfficeArtExtension();
         extItem.uri = "{96DAC541-7B7A-43D3-8B79-37D633B846F1}";
         extItem.Any = "<asvg:svgBlip xmlns:asvg=\"http://schemas.microsoft.com/office/drawing/2016/SVG/main\" r:embed=\"" + picData.GetPackageRelationship().Id + "\"/>";
         extLst.ext.Add(extItem);
         blip.extLst = extLst;
     }));
 }
Exemple #3
0
        /**
         * Adds a picture to the run. This method handles
         *  attaching the picture data to the overall file.
         *
         * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_EMF
         * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_WMF
         * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_PICT
         * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_JPEG
         * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_PNG
         * @see NPOI.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.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));

            XmlDocument xmlDoc = new XmlDocument();

            //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.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.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();
            extent.cx = (width);
            extent.cy = (height);

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

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

            NPOI.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.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);
        }