use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class EmbeddedObjects method main.
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
InputStream is = pPart.getInputStream();
Closeable document;
if (contentType.equals("application/vnd.ms-excel")) {
// Excel Workbook - either binary or OpenXML
document = new HSSFWorkbook(is);
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
// Excel Workbook - OpenXML file format
document = new XSSFWorkbook(is);
} else if (contentType.equals("application/msword")) {
// Word Document - binary (OLE2CDF) file format
document = new HWPFDocument(is);
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
// Word Document - OpenXML file format
document = new XWPFDocument(is);
} else if (contentType.equals("application/vnd.ms-powerpoint")) {
// PowerPoint Document - binary file format
document = new HSLFSlideShow(is);
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
// PowerPoint Document - OpenXML file format
document = new XMLSlideShow(is);
} else {
// Any other type of embedded object.
document = is;
}
document.close();
is.close();
}
workbook.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class POIXMLProperties method getThumbnailFilename.
/**
* Returns the name of the Document thumbnail, eg
* <code>thumbnail.jpeg</code>, or <code>null</code> if there
* isn't one.
*
* @return The thumbnail filename, or null
*/
public String getThumbnailFilename() {
PackagePart tPart = getThumbnailPart();
if (tPart == null)
return null;
String name = tPart.getPartName().getName();
return name.substring(name.lastIndexOf('/'));
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class POIXMLRelation method getContents.
/**
* Fetches the InputStream to read the contents, based
* of the specified core part, for which we are defined
* as a suitable relationship
*
* @since 3.16-beta3
*/
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
PackageRelationshipCollection prc = corePart.getRelationshipsByType(getRelation());
Iterator<PackageRelationship> it = prc.iterator();
if (it.hasNext()) {
PackageRelationship rel = it.next();
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart part = corePart.getPackage().getPart(relName);
return part.getInputStream();
}
log.log(POILogger.WARN, "No part " + getDefaultFileName() + " found");
return null;
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class POIXMLDocumentPart method getPartFromOPCPackage.
/**
* Retrieves the core document part
*
* @since POI 3.14-Beta1
*/
private static PackagePart getPartFromOPCPackage(OPCPackage pkg, String coreDocumentRel) {
PackageRelationship coreRel = pkg.getRelationshipsByType(coreDocumentRel).getRelationship(0);
if (coreRel != null) {
PackagePart pp = pkg.getPart(coreRel);
if (pp == null) {
throw new POIXMLException("OOXML file structure broken/invalid - core document '" + coreRel.getTargetURI() + "' not found.");
}
return pp;
}
coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
if (coreRel != null) {
throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
}
throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class POIXMLProperties method setThumbnail.
/**
* Sets the Thumbnail for the document, replacing any existing one.
*
* @param filename The filename for the thumbnail image, eg {@code thumbnail.jpg}
* @param imageData The inputstream to read the thumbnail image from
*
* @throws IOException if the thumbnail can't be written
*/
public void setThumbnail(String filename, InputStream imageData) throws IOException {
PackagePart tPart = getThumbnailPart();
if (tPart == null) {
// New thumbnail
pkg.addThumbnail(filename, imageData);
} else {
// Change existing
String newType = ContentTypes.getContentTypeFromFileExtension(filename);
if (!newType.equals(tPart.getContentType())) {
throw new IllegalArgumentException("Can't set a Thumbnail of type " + newType + " when existing one is of a different type " + tPart.getContentType());
}
StreamHelper.copyStream(imageData, tPart.getOutputStream());
}
}
Aggregations