Java Code Examples for org.apache.poi.xslf.usermodel.XMLSlideShow#addPicture()

The following examples show how to use org.apache.poi.xslf.usermodel.XMLSlideShow#addPicture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WatermarkPptTests.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
@Test
	public void test1() throws IOException {
		 // create a ppt
        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("E:\\ConvertTester\\ppt\\看看addThread方法的源码.pptx"));
        XSLFPictureData pd = ppt.addPicture(new File("E:\\ConvertTester\\images\\jshrss-logo.png"), PictureType.PNG);
        for (int i=0;i<ppt.getSlideMasters().size();i++) {
        	XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(i);
        	XSLFSlideLayout[] slideLayouts = slideMaster.getSlideLayouts();
        	for (XSLFSlideLayout slidelayout : slideLayouts) {
        		XSLFPictureShape ps = slidelayout.createPicture(pd);
            	ps.setAnchor(new Rectangle2D.Double(20, 20, 640, 400));
        	}
        }
        FileOutputStream fos = new FileOutputStream("E:\\ConvertTester\\ppt\\bla.pptx");
        ppt.write(fos);
        fos.close();
        
        
        

//        XSLFSlide sl = ppt.createSlide(slidelayout);
//        ((XSLFAutoShape)sl.getShapes().get(0)).setText("title");
//        ((XSLFAutoShape)sl.getShapes().get(1)).setText("content");

       
	}
 
Example 2
Source File: SvgParserImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public boolean parse(XMLSlideShow xmlSlideShow, XSLFSlide xslfSlide, JSONObject object) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        if (!image.svg2png(object.getString("svg"), object.getIntValue("width"), object.getIntValue("height"), outputStream))
            return false;

        XSLFPictureData xslfPictureData = xmlSlideShow.addPicture(
                parserHelper.getImage(object, "image/png", outputStream), PictureData.PictureType.PNG);
        parse(xslfSlide, xslfPictureData, object);

        return true;
    } catch (Throwable e) {
        logger.warn(e, "解析SVG图片[{}]时发生异常!", object.toJSONString());

        return false;
    }
}
 
Example 3
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 4 votes vote down vote up
private static void replaceImage(XMLSlideShow ppt, ShapeContainer<XSLFShape, ?> shapeContainer, ImageToReplace imageToReplace) {
	byte[] newPictureResized = imageToReplace.imageMapper.getReplacementMode().resize(
		imageToReplace.imageMapper.getValue(),
		imageToReplace.imageMapper.getTargetFormat().name(),
		(int) imageToReplace.toReplace.getAnchor().getWidth(),
		(int) imageToReplace.toReplace.getAnchor().getHeight(),
		imageToReplace.getImageMapper().getQualityFactory(),
		imageToReplace.getImageMapper().getQualityMultiplicator()
	);
	if(newPictureResized == null) {
		// if an error occurred during the resizement of the image, the replacement cannot be processed
		return;
	}

	XSLFPictureData newPictureData = ppt.addPicture(newPictureResized, imageToReplace.imageMapper.getTargetFormat());
	Rectangle2D newImageAnchor = computeNewImageAnchor(
		imageToReplace.toReplace.getAnchor(),
		newPictureResized,
		imageToReplace.imageMapper.getReplacementMode(),
		imageToReplace.imageMapper.getQualityMultiplicator()
	);

	if(shapeContainer instanceof POIXMLDocumentPart) {
		replaceImageInPlace((POIXMLDocumentPart) shapeContainer, imageToReplace, newPictureData, newImageAnchor);
	}
	else if(shapeContainer instanceof XSLFGroupShape) {
		replaceImageInPlace(((XSLFGroupShape) shapeContainer).getSheet(), imageToReplace, newPictureData, newImageAnchor);
	}
	// If the container is not a POIXMLDocumentPart or a XSLFGroupShape,
	// the old image have to deleted along with its properties.
	// The new image will just be place in the same area of the old image.
	// This behavior is a fall back that should not append since
	// I don't think the image container can be something else
	// apart from POIXMLDocumentPart and XSLFGroupShape.
	else {
		XSLFPictureShape newPictureShape = (XSLFPictureShape) shapeContainer.createPicture(newPictureData);
		newPictureShape.setAnchor(newImageAnchor);

		shapeContainer.removeShape(imageToReplace.toReplace);
	}
}