Search in sources :

Example 1 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip in project poi by apache.

the class XSLFSimpleShape method copy.

@Override
void copy(XSLFShape sh) {
    super.copy(sh);
    XSLFSimpleShape s = (XSLFSimpleShape) sh;
    Color srsSolidFill = s.getFillColor();
    Color tgtSoliFill = getFillColor();
    if (srsSolidFill != null && !srsSolidFill.equals(tgtSoliFill)) {
        setFillColor(srsSolidFill);
    }
    XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(getShapeProperties());
    if (fp != null && fp.isSetBlipFill()) {
        CTBlip blip = fp.getBlipFill().getBlip();
        String blipId = blip.getEmbed();
        String relId = getSheet().importBlip(blipId, s.getSheet().getPackagePart());
        blip.setEmbed(relId);
    }
    Color srcLineColor = s.getLineColor();
    Color tgtLineColor = getLineColor();
    if (srcLineColor != null && !srcLineColor.equals(tgtLineColor)) {
        setLineColor(srcLineColor);
    }
    double srcLineWidth = s.getLineWidth();
    double tgtLineWidth = getLineWidth();
    if (srcLineWidth != tgtLineWidth) {
        setLineWidth(srcLineWidth);
    }
    LineDash srcLineDash = s.getLineDash();
    LineDash tgtLineDash = getLineDash();
    if (srcLineDash != null && srcLineDash != tgtLineDash) {
        setLineDash(srcLineDash);
    }
    LineCap srcLineCap = s.getLineCap();
    LineCap tgtLineCap = getLineCap();
    if (srcLineCap != null && srcLineCap != tgtLineCap) {
        setLineCap(srcLineCap);
    }
}
Also used : CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) Color(java.awt.Color) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) LineDash(org.apache.poi.sl.usermodel.StrokeStyle.LineDash) LineCap(org.apache.poi.sl.usermodel.StrokeStyle.LineCap) STLineCap(org.openxmlformats.schemas.drawingml.x2006.main.STLineCap) XSLFFillProperties(org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)

Example 2 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip in project poi by apache.

the class XWPFRun method addPicture.

/**
     * Adds a picture to the run. This method handles
     * attaching the picture data to the overall file.
     *
     * @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 org.apache.poi.openxml4j.exceptions.InvalidFormatException
     * @throws IOException
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
     */
public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height) throws InvalidFormatException, IOException {
    String relationId;
    XWPFPictureData picData;
    // TODO Should we have an interface for this sort of thing?
    if (parent.getPart() instanceof XWPFHeaderFooter) {
        XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) parent.getPart();
        relationId = headerFooter.addPictureData(pictureData, pictureType);
        picData = (XWPFPictureData) headerFooter.getRelationById(relationId);
    } else {
        @SuppressWarnings("resource") XWPFDocument doc = parent.getDocument();
        relationId = doc.addPictureData(pictureData, pictureType);
        picData = (XWPFPictureData) doc.getRelationById(relationId);
    }
    // Create the drawing entry for it
    try {
        CTDrawing drawing = run.addNewDrawing();
        CTInline inline = drawing.addNewInline();
        // Do the fiddly namespace bits on the inline
        // (We need full control of what goes where and as what)
        String xml = "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" + "<a:graphicData uri=\"" + CTPicture.type.getName().getNamespaceURI() + "\">" + "<pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" />" + "</a:graphicData>" + "</a:graphic>";
        InputSource is = new InputSource(new StringReader(xml));
        org.w3c.dom.Document doc = DocumentHelper.readDocument(is);
        inline.set(XmlToken.Factory.parse(doc.getDocumentElement(), DEFAULT_XML_OPTIONS));
        // Setup the inline
        inline.setDistT(0);
        inline.setDistR(0);
        inline.setDistB(0);
        inline.setDistL(0);
        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        long id = getParent().getDocument().getDrawingIdManager().reserveNew();
        docPr.setId(id);
        /* This name is not visible in Word 2010 anywhere. */
        docPr.setName("Drawing " + id);
        docPr.setDescr(filename);
        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);
        // Grab the picture object
        CTGraphicalObject graphic = inline.getGraphic();
        CTGraphicalObjectData graphicData = graphic.getGraphicData();
        CTPicture pic = getCTPictures(graphicData).get(0);
        // Set it up
        CTPictureNonVisual nvPicPr = pic.addNewNvPicPr();
        CTNonVisualDrawingProps cNvPr = nvPicPr.addNewCNvPr();
        /* use "0" for the id. See ECM-576, 20.2.2.3 */
        cNvPr.setId(0L);
        /* This name is not visible in Word 2010 anywhere */
        cNvPr.setName("Picture " + id);
        cNvPr.setDescr(filename);
        CTNonVisualPictureProperties cNvPicPr = nvPicPr.addNewCNvPicPr();
        cNvPicPr.addNewPicLocks().setNoChangeAspect(true);
        CTBlipFillProperties blipFill = pic.addNewBlipFill();
        CTBlip blip = blipFill.addNewBlip();
        blip.setEmbed(parent.getPart().getRelationId(picData));
        blipFill.addNewStretch().addNewFillRect();
        CTShapeProperties spPr = pic.addNewSpPr();
        CTTransform2D xfrm = spPr.addNewXfrm();
        CTPoint2D off = xfrm.addNewOff();
        off.setX(0);
        off.setY(0);
        CTPositiveSize2D ext = xfrm.addNewExt();
        ext.setCx(width);
        ext.setCy(height);
        CTPresetGeometry2D prstGeom = spPr.addNewPrstGeom();
        prstGeom.setPrst(STShapeType.RECT);
        prstGeom.addNewAvLst();
        // Finish up
        XWPFPicture xwpfPicture = new XWPFPicture(pic, this);
        pictures.add(xwpfPicture);
        return xwpfPicture;
    } catch (XmlException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalStateException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) CTTransform2D(org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D) CTShapeProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties) XmlString(org.apache.xmlbeans.XmlString) SAXException(org.xml.sax.SAXException) CTPoint2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) CTPresetGeometry2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D) StringReader(java.io.StringReader) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) CTGraphicalObject(org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject) CTInline(org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline) CTDrawing(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing) CTPictureNonVisual(org.openxmlformats.schemas.drawingml.x2006.picture.CTPictureNonVisual) CTGraphicalObjectData(org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData) CTNonVisualPictureProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualPictureProperties) CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) XmlException(org.apache.xmlbeans.XmlException) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTPositiveSize2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D)

Example 3 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip in project poi by apache.

the class TestXWPFRun method testAddPictureInHeader.

/**
     * Bugzilla #58237 - Unable to add image to word document header
     */
@Test
public void testAddPictureInHeader() throws IOException, InvalidFormatException {
    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
    XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
    XWPFParagraph p = hdr.createParagraph();
    XWPFRun r = p.createRun();
    assertEquals(0, hdr.getAllPictures().size());
    assertEquals(0, r.getEmbeddedPictures().size());
    r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
    assertEquals(1, hdr.getAllPictures().size());
    assertEquals(1, r.getEmbeddedPictures().size());
    XWPFPicture pic = r.getEmbeddedPictures().get(0);
    CTPicture ctPic = pic.getCTPicture();
    CTBlipFillProperties ctBlipFill = ctPic.getBlipFill();
    assertNotNull(ctBlipFill);
    CTBlip ctBlip = ctBlipFill.getBlip();
    assertNotNull(ctBlip);
    assertEquals("rId1", ctBlip.getEmbed());
    XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
    XWPFHeader hdrBack = docBack.getHeaderArray(0);
    XWPFParagraph pBack = hdrBack.getParagraphArray(0);
    XWPFRun rBack = pBack.getRuns().get(0);
    assertEquals(1, hdrBack.getAllPictures().size());
    assertEquals(1, rBack.getEmbeddedPictures().size());
    docBack.close();
    doc.close();
}
Also used : CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) ByteArrayInputStream(java.io.ByteArrayInputStream) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) Test(org.junit.Test)

Example 4 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip in project poi by apache.

the class XSLFShape method selectPaint.

protected static PaintStyle selectPaint(final CTBlipFillProperties blipFill, final PackagePart parentPart) {
    final CTBlip blip = blipFill.getBlip();
    return new TexturePaint() {

        private PackagePart getPart() {
            try {
                String blipId = blip.getEmbed();
                PackageRelationship rel = parentPart.getRelationship(blipId);
                return parentPart.getRelatedPart(rel);
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e);
            }
        }

        public InputStream getImageData() {
            try {
                return getPart().getInputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public String getContentType() {
            /* TOOD: map content-type */
            return getPart().getContentType();
        }

        public int getAlpha() {
            return (blip.sizeOfAlphaModFixArray() > 0) ? blip.getAlphaModFixArray(0).getAmt() : 100000;
        }
    };
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 5 with CTBlip

use of org.openxmlformats.schemas.drawingml.x2006.main.CTBlip in project poi by apache.

the class XSLFPictureShape method copy.

@Override
void copy(XSLFShape sh) {
    super.copy(sh);
    XSLFPictureShape p = (XSLFPictureShape) sh;
    String blipId = p.getBlipId();
    String relId = getSheet().importBlip(blipId, p.getSheet().getPackagePart());
    CTPicture ct = (CTPicture) getXmlObject();
    CTBlip blip = getBlipFill().getBlip();
    blip.setEmbed(relId);
    CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
    if (nvPr.isSetCustDataLst()) {
        // discard any custom tags associated with the picture being copied
        nvPr.unsetCustDataLst();
    }
    if (blip.isSetExtLst()) {
        CTOfficeArtExtensionList extLst = blip.getExtLst();
        for (CTOfficeArtExtension ext : extLst.getExtArray()) {
            String xpath = "declare namespace a14='http://schemas.microsoft.com/office/drawing/2010/main' $this//a14:imgProps/a14:imgLayer";
            XmlObject[] obj = ext.selectPath(xpath);
            if (obj != null && obj.length == 1) {
                XmlCursor c = obj[0].newCursor();
                //selectPath("declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' $this//[@embed]");
                String id = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"));
                String newId = getSheet().importBlip(id, p.getSheet().getPackagePart());
                c.setAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), newId);
                c.dispose();
            }
        }
    }
}
Also used : CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) QName(javax.xml.namespace.QName) CTPicture(org.openxmlformats.schemas.presentationml.x2006.main.CTPicture) CTApplicationNonVisualDrawingProps(org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps) CTOfficeArtExtension(org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtension) XmlObject(org.apache.xmlbeans.XmlObject) CTOfficeArtExtensionList(org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtensionList) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

CTBlip (org.openxmlformats.schemas.drawingml.x2006.main.CTBlip)6 CTBlipFillProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties)3 CTNonVisualDrawingProps (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps)2 CTPresetGeometry2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D)2 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)2 CTPicture (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)2 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)2 Color (java.awt.Color)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 QName (javax.xml.namespace.QName)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1 LineCap (org.apache.poi.sl.usermodel.StrokeStyle.LineCap)1 LineDash (org.apache.poi.sl.usermodel.StrokeStyle.LineDash)1 XSLFFillProperties (org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1