Search in sources :

Example 1 with POIXMLRelation

use of org.apache.poi.POIXMLRelation 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 2 with POIXMLRelation

use of org.apache.poi.POIXMLRelation 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)

Aggregations

IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 POIXMLException (org.apache.poi.POIXMLException)2 POIXMLRelation (org.apache.poi.POIXMLRelation)2 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1