Search in sources :

Example 31 with POIXMLException

use of org.apache.poi.POIXMLException 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 32 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSLFTextShape method resizeToFitText.

/**
     * Adjust the size of the shape so it encompasses the text inside it.
     *
     * @return a <code>Rectangle2D</code> that is the bounds of this shape.
     */
public Rectangle2D resizeToFitText() {
    Rectangle2D anchor = getAnchor();
    if (anchor.getWidth() == 0.)
        throw new POIXMLException("Anchor of the shape was not set.");
    double height = getTextHeight();
    // add a pixel to compensate rounding errors
    height += 1;
    anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height);
    setAnchor(anchor);
    return anchor;
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) POIXMLException(org.apache.poi.POIXMLException)

Example 33 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSSFWorkbook method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        this.workbook = doc.getWorkbook();
        ThemesTable theme = null;
        Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
        Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof SharedStringsTable) {
                sharedStringSource = (SharedStringsTable) p;
            } else if (p instanceof StylesTable) {
                stylesSource = (StylesTable) p;
            } else if (p instanceof ThemesTable) {
                theme = (ThemesTable) p;
            } else if (p instanceof CalculationChain) {
                calcChain = (CalculationChain) p;
            } else if (p instanceof MapInfo) {
                mapInfo = (MapInfo) p;
            } else if (p instanceof XSSFSheet) {
                shIdMap.put(rp.getRelationship().getId(), (XSSFSheet) p);
            } else if (p instanceof ExternalLinksTable) {
                elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable) p);
            }
        }
        boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
        if (stylesSource == null) {
            // Create Styles if it is missing
            if (packageReadOnly) {
                stylesSource = new StylesTable();
            } else {
                stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
            }
        }
        stylesSource.setWorkbook(this);
        stylesSource.setTheme(theme);
        if (sharedStringSource == null) {
            // Create SST if it is missing
            if (packageReadOnly) {
                sharedStringSource = new SharedStringsTable();
            } else {
                sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
            }
        }
        // Load individual sheets. The order of sheets is defined by the order
        //  of CTSheet elements in the workbook
        sheets = new ArrayList<XSSFSheet>(shIdMap.size());
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            parseSheet(shIdMap, ctSheet);
        }
        // Load the external links tables. Their order is defined by the order 
        //  of CTExternalReference elements in the workbook
        externalLinks = new ArrayList<ExternalLinksTable>(elIdMap.size());
        if (this.workbook.isSetExternalReferences()) {
            for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
                ExternalLinksTable el = elIdMap.get(er.getId());
                if (el == null) {
                    logger.log(POILogger.WARN, "ExternalLinksTable with r:id " + er.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                externalLinks.add(el);
            }
        }
        // Process the named ranges
        reprocessNamedRanges();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : WorkbookDocument(org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument) ThemesTable(org.apache.poi.xssf.model.ThemesTable) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) CTExternalReference(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) StylesTable(org.apache.poi.xssf.model.StylesTable) POIXMLException(org.apache.poi.POIXMLException) CalculationChain(org.apache.poi.xssf.model.CalculationChain) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) XmlException(org.apache.xmlbeans.XmlException) MapInfo(org.apache.poi.xssf.model.MapInfo) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable)

Example 34 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSSFWorkbook method addRelation.

/**
     * @since 3.14-Beta1
     */
private static void addRelation(RelationPart rp, POIXMLDocumentPart target) {
    PackageRelationship rel = rp.getRelationship();
    if (rel.getTargetMode() == TargetMode.EXTERNAL) {
        target.getPackagePart().addRelationship(rel.getTargetURI(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
    } else {
        XSSFRelation xssfRel = XSSFRelation.getInstance(rel.getRelationshipType());
        if (xssfRel == null) {
            // Don't copy all relations blindly, but only the ones we know about
            throw new POIXMLException("Can't clone sheet - unknown relation type found: " + rel.getRelationshipType());
        }
        target.addRelation(rel.getId(), xssfRel, rp.getDocumentPart());
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException)

Example 35 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSSFWorkbook method createBuiltInName.

/**
     * Generates a NameRecord to represent a built-in region
     *
     * @return a new NameRecord
     * @throws IllegalArgumentException if sheetNumber is invalid
     * @throws POIXMLException if such a name already exists in the workbook
     */
XSSFName createBuiltInName(String builtInName, int sheetNumber) {
    validateSheetIndex(sheetNumber);
    CTDefinedNames names = workbook.getDefinedNames() == null ? workbook.addNewDefinedNames() : workbook.getDefinedNames();
    CTDefinedName nameRecord = names.addNewDefinedName();
    nameRecord.setName(builtInName);
    nameRecord.setLocalSheetId(sheetNumber);
    if (getBuiltInName(builtInName, sheetNumber) != null) {
        throw new POIXMLException("Builtin (" + builtInName + ") already exists for sheet (" + sheetNumber + ")");
    }
    return createAndStoreName(nameRecord);
}
Also used : CTDefinedName(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName) POIXMLException(org.apache.poi.POIXMLException) CTDefinedNames(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames)

Aggregations

POIXMLException (org.apache.poi.POIXMLException)50 XmlException (org.apache.xmlbeans.XmlException)19 IOException (java.io.IOException)18 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)11 InputStream (java.io.InputStream)9 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)9 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)8 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStream (java.io.OutputStream)6 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)6 XmlObject (org.apache.xmlbeans.XmlObject)6 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)5 XmlCursor (org.apache.xmlbeans.XmlCursor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3