public static void main(String[] args) throws Exception { // String uri = System.getProperty("user.dir") + "/sample-docs/metafile-samples/gradient.emf"; String uri = System.getProperty("user.dir") + "/sample-docs/metafile-samples/freehand_picture_saveas.wmf"; System.out.println(uri); // String uri = "/tmp/img4448.img"; ImageInfo ii = getImageInfo(new URL(uri)); displayImageInfo(ii); }
/** * @param bytes * @param imageFile * @return * @throws Exception * @throws FileNotFoundException * @throws IOException * @throws InterruptedException */ private static ImageInfo ensureFormatIsSupported( URL url, File imageFile, byte[] bytes, boolean isLoad) throws Docx4JException { FileOutputStream fos; // ImageInfo can also tell us what sort of image it is ImageInfo info = null; boolean imagePreloaderFound = true; try { try { info = getImageInfo(url); // Debug ... note that these figures // aren't necessarily accurate for EPS displayImageInfo(info); } catch (org.apache.xmlgraphics.image.loader.ImageException e) { // Assume: The file format is not supported. No ImagePreloader found for /tmp/img55623.img // There is no preloader for eg PDFs. // (To use an image natively, we do need a preloader) imagePreloaderFound = false; log.warn(e.getMessage()); } if (imagePreloaderFound && (info.getMimeType().equals(ContentTypes.IMAGE_TIFF) || info.getMimeType().equals(ContentTypes.IMAGE_EMF2) // ImageInfo || info.getMimeType().equals(ContentTypes.IMAGE_WMF) || info.getMimeType().equals(ContentTypes.IMAGE_PNG) || info.getMimeType().equals(ContentTypes.IMAGE_JPEG) || info.getMimeType().equals(ContentTypes.IMAGE_GIF) // || info.getMimeType().equals(ContentTypes.IMAGE_EPS) || info.getMimeType().equals(ContentTypes.IMAGE_BMP))) { // TODO: add other supported formats // If its a format Word supports natively, // do nothing here log.debug(".. supported natively by Word"); } else if (imageFile != null && bytes != null) { // otherwise (eg if its an EPS or PDF), try to convert it // Although the Word UI suggests you can embed an EPS // directly, Word actually converts it to an EMF; // Word is unable to read a plain EPS image part. // (TODO: detect failure) log.debug(".. attempting to convert to PNG"); // If image haven't been load (using function createImagePartFromFilePath), we load it if (isLoad == false) { // So first, we create tmpFile File tmpImageFile = File.createTempFile("img", ".img"); fos = new FileOutputStream(tmpImageFile); // Now we get the inputStream, which is represented by imageFile in this case FileInputStream bais = new FileInputStream(imageFile); // We convert convertToPNG(bais, fos, density); // We don't forget to change locFile because the new image file is the converted image // file!! imageFile = tmpImageFile; } // Else image has been load in an array (using function cretaImagePart) else { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); fos = new FileOutputStream(imageFile); convertToPNG(bais, fos, density); } fos.close(); fos = null; // We need to refresh image info getImageManager().getCache().clearCache(); info = getImageInfo(new URL("file://" + imageFile.getAbsolutePath())); // Debug ... displayImageInfo(info); } else { throw new Docx4JException("Unsupported linked image type."); } } catch (Exception e) { throw new Docx4JException("Error checking image format", e); } return info; }