/** * Adds a picture to the document. * * @param is The stream to read image from * @param format The format of the picture. * @return the index to this picture (0 based), the added picture can be obtained from {@link * #getAllPictures()} . */ public int addPicture(InputStream is, int format) throws IOException { int imageNumber = getNextPicNameNumber(format); XWPFPictureData img = (XWPFPictureData) createRelationship( XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, true); OutputStream out = img.getPackagePart().getOutputStream(); IOUtils.copy(is, out); out.close(); pictures.add(img); return getAllPictures().size() - 1; }
/** * Adds a picture to the document. * * @param pictureData The picture bytes * @param format The format of the picture. * @return the index to this picture (0 based), the added picture can be obtained from {@link * #getAllPictures()} . */ public int addPicture(byte[] pictureData, int format) { int imageNumber = getNextPicNameNumber(format); XWPFPictureData img = (XWPFPictureData) createRelationship( XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false); try { OutputStream out = img.getPackagePart().getOutputStream(); out.write(pictureData); out.close(); } catch (IOException e) { throw new POIXMLException(e); } pictures.add(img); return getAllPictures().size() - 1; }
/** * Add the picture to drawing relations * * @param is the stream to read picture data from */ public PackageRelationship addPictureReference(InputStream is, int format) { PackageRelationship rel = null; try { int imageNumber = getNextPicNameNumber(format); XWPFPictureData img = (XWPFPictureData) createRelationship( XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false); OutputStream out = img.getPackagePart().getOutputStream(); IOUtils.copy(is, out); out.close(); rel = img.getPackageRelationship(); pictures.add(img); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rel; }