org.apache.poi.xslf.usermodel.XSLFPictureShape Java Examples

The following examples show how to use org.apache.poi.xslf.usermodel.XSLFPictureShape. 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: ImageImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public void parseShape(ReaderContext readerContext, XSLFSimpleShape xslfSimpleShape, JSONObject shape) {
    if (!(xslfSimpleShape instanceof XSLFPictureShape))
        return;

    XSLFPictureShape xslfPictureShape = (XSLFPictureShape) xslfSimpleShape;
    parseClipping(xslfPictureShape, shape);
    JSONObject image = new JSONObject();
    XSLFPictureData xslfPictureData = xslfPictureShape.getPictureData();
    parseSize(xslfPictureData, image);
    image.put("contentType", xslfPictureData.getContentType());
    try {
        image.put("url", readerContext.getMediaWriter().write(MediaType.find(xslfPictureData.getContentType()),
                xslfPictureData.getFileName(), xslfPictureData.getInputStream()));
    } catch (IOException e) {
        logger.warn(e, "获取PPTX图片数据时发生异常!");
    }
    image.put("state", ((CTPicture) xslfPictureShape.getXmlObject()).getBlipFill().getBlip().getCstate().toString());
    shape.put("image", image);
}
 
Example #3
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 6 votes vote down vote up
/**
 * Handles shape modification
 * @return true is the shape should be removed
 */
private static boolean processShape(XSLFShape shape, List<ImageToReplace> imagesToReplace, XMLSlideShow ppt, PptMapper mapper) {
	if(shape instanceof XSLFTextShape) {
		return processTextShape((XSLFTextShape) shape, mapper);
	}
	if(shape instanceof XSLFTable) {
		return processTableShape((XSLFTable) shape, mapper);
	}
	if(shape instanceof XSLFPictureShape) {
		return processImageShape((XSLFPictureShape) shape, imagesToReplace, mapper);
	}
	if(shape instanceof XSLFGroupShape) {
		return processGroupShape((XSLFGroupShape) shape, ppt, mapper);
	}

	return false;
}
 
Example #4
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 6 votes vote down vote up
private static boolean processImageShape(XSLFPictureShape imageShape, List<ImageToReplace> imagesToReplace, PptMapper mapper) {
	Optional<PptVariable> imageVariable = parseHyperlinkVariable(imageShape);
	if(shouldHide(imageVariable, mapper)) {
		return true;
	}

	imageVariable
		.flatMap(variable -> mapper.imageMapping(variable.getName()))
		.ifPresent(imageMapper ->
			imagesToReplace.add(ImageToReplace.of(imageShape, imageMapper))
		);

	styleShape(imageShape, imageVariable, mapper);

	return false;
}
 
Example #5
Source File: ImageImpl.java    From tephra with MIT License 5 votes vote down vote up
private void parseClipping(XSLFPictureShape xslfPictureShape, JSONObject shape) {
    Insets insets = xslfPictureShape.getClipping();
    if (insets == null)
        return;

    JSONObject clipping = new JSONObject();
    clipping.put("left", officeHelper.fromPercent(insets.left));
    clipping.put("top", officeHelper.fromPercent(insets.top));
    clipping.put("right", officeHelper.fromPercent(insets.right));
    clipping.put("bottom", officeHelper.fromPercent(insets.bottom));
    shape.put("clipping", clipping);
}
 
Example #6
Source File: ImageParserSupport.java    From tephra with MIT License 4 votes vote down vote up
private void parseImage(XSLFSlide xslfSlide, XSLFPictureData xslfPictureData, JSONObject object) {
    XSLFPictureShape xslfPictureShape = xslfSlide.createPicture(xslfPictureData);
    xslfPictureShape.setAnchor(parserHelper.getRectangle(object));
    parserHelper.rotate(xslfPictureShape, object);
}
 
Example #7
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);
	}
}