Search in sources :

Example 71 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XWPFHeaderFooter method addPictureData.

/**
     * 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) throws InvalidFormatException {
    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();
        OutputStream out = null;
        try {
            out = picDataPart.getOutputStream();
            out.write(pictureData);
        } catch (IOException e) {
            throw new POIXMLException(e);
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            // 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.
             */
        // TODO add support for TargetMode.EXTERNAL relations.
        RelationPart rp = addRelation(null, XWPFRelation.IMAGES, xwpfPicData);
        pictures.add(xwpfPicData);
        return rp.getRelationship().getId();
    } else {
        /* Part already existed, get relation id and return it */
        return getRelationId(xwpfPicData);
    }
}
Also used : POIXMLRelation(org.apache.poi.POIXMLRelation) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 72 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XWPFNumbering method commit.

/**
     * save and commit numbering
     */
@Override
protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "numbering"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    ctNumbering.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 73 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XWPFDocument method addPictureData.

public String addPictureData(byte[] pictureData, int format) throws InvalidFormatException {
    XWPFPictureData xwpfPicData = findPackagePictureData(pictureData, format);
    POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
    if (xwpfPicData == null) {
        /* Part doesn't exist, create a new one */
        int idx = getNextPicNameNumber(format);
        xwpfPicData = (XWPFPictureData) createRelationship(relDesc, XWPFFactory.getInstance(), idx);
        /* write bytes to new part */
        PackagePart picDataPart = xwpfPicData.getPackagePart();
        OutputStream out = null;
        try {
            out = picDataPart.getOutputStream();
            out.write(pictureData);
        } catch (IOException e) {
            throw new POIXMLException(e);
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            // ignore
            }
        }
        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.
             */
        // TODO add support for TargetMode.EXTERNAL relations.
        RelationPart rp = addRelation(null, XWPFRelation.IMAGES, xwpfPicData);
        return rp.getRelationship().getId();
    } else {
        /* Part already existed, get relation id and return it */
        return getRelationId(xwpfPicData);
    }
}
Also used : POIXMLRelation(org.apache.poi.POIXMLRelation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 74 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSLFSimpleShape method getLinePaint.

protected PaintStyle getLinePaint() {
    XSLFSheet sheet = getSheet();
    final XSLFTheme theme = sheet.getTheme();
    final boolean hasPlaceholder = getPlaceholder() != null;
    PropertyFetcher<PaintStyle> fetcher = new PropertyFetcher<PaintStyle>() {

        @Override
        public boolean fetch(XSLFShape shape) {
            CTLineProperties spPr = getLn(shape, false);
            XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(spPr);
            if (fp != null && fp.isSetNoFill()) {
                setValue(null);
                return true;
            }
            PackagePart pp = shape.getSheet().getPackagePart();
            PaintStyle paint = selectPaint(fp, null, pp, theme, hasPlaceholder);
            if (paint != null) {
                setValue(paint);
                return true;
            }
            CTShapeStyle style = shape.getSpStyle();
            if (style != null) {
                fp = XSLFPropertiesDelegate.getFillDelegate(style.getLnRef());
                paint = selectPaint(fp, null, pp, theme, hasPlaceholder);
                // line color was not found, check if it is defined in the theme
                if (paint == null) {
                    paint = getThemePaint(style, pp);
                }
            }
            if (paint != null) {
                setValue(paint);
                return true;
            }
            return false;
        }

        PaintStyle getThemePaint(CTShapeStyle style, PackagePart pp) {
            // get a reference to a line style within the style matrix.
            CTStyleMatrixReference lnRef = style.getLnRef();
            if (lnRef == null) {
                return null;
            }
            int idx = (int) lnRef.getIdx();
            CTSchemeColor phClr = lnRef.getSchemeClr();
            if (idx <= 0) {
                return null;
            }
            CTLineProperties props = theme.getXmlObject().getThemeElements().getFmtScheme().getLnStyleLst().getLnArray(idx - 1);
            XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(props);
            return selectPaint(fp, phClr, pp, theme, hasPlaceholder);
        }
    };
    fetchShapeProperty(fetcher);
    return fetcher.getValue();
}
Also used : CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) CTLineProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties) PropertyFetcher(org.apache.poi.xslf.model.PropertyFetcher) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) CTShapeStyle(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) CTStyleMatrixReference(org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference) XSLFFillProperties(org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)

Example 75 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSLFTextRun method getFontColor.

@Override
public PaintStyle getFontColor() {
    final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null;
    CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()) {

        public boolean fetch(CTTextCharacterProperties props) {
            if (props == null) {
                return false;
            }
            XSLFShape shape = _p.getParentShape();
            CTShapeStyle style = shape.getSpStyle();
            CTSchemeColor phClr = null;
            if (style != null && style.getFontRef() != null) {
                phClr = style.getFontRef().getSchemeClr();
            }
            XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(props);
            XSLFSheet sheet = shape.getSheet();
            PackagePart pp = sheet.getPackagePart();
            XSLFTheme theme = sheet.getTheme();
            PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder);
            if (ps != null) {
                setValue(ps);
                return true;
            }
            return false;
        }
    };
    fetchCharacterProperty(fetcher);
    return fetcher.getValue();
}
Also used : CTShapeStyle(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle) CharacterPropertyFetcher(org.apache.poi.xslf.model.CharacterPropertyFetcher) CTTextCharacterProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XSLFFillProperties(org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5