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

The following examples show how to use org.apache.poi.xslf.usermodel.XSLFShape. 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: 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 #2
Source File: ImageImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    if (!shape.containsKey("image"))
        return null;

    JSONObject image = shape.getJSONObject("image");
    try {
        InputStream inputStream = writerContext.getMediaReader().read(image);
        XSLFPictureData xslfPictureData = writerContext.getXmlSlideShow().addPicture(inputStream,
                getPictureType(image.getString("url"), image.getString("contentType")));
        inputStream.close();

        return writerContext.getXslfSlide().createPicture(xslfPictureData);
    } catch (Throwable throwable) {
        logger.warn(throwable, "获取图片资源[{}]时发生异常!", image);

        return null;
    }
}
 
Example #3
Source File: PptTemplates.java    From PPT-Templates with Apache License 2.0 6 votes vote down vote up
private static void processShapesContainer(ShapeContainer<XSLFShape, ?> shapeContainer, XMLSlideShow ppt, PptMapper mapper) {
	List<ImageToReplace> imagesToReplace = new ArrayList<>();
	List<XSLFShape> shapesToDelete = new ArrayList<>();

	for(XSLFShape shape : shapeContainer.getShapes()) {
		if(processShape(shape, imagesToReplace, ppt, mapper)) {
			shapesToDelete.add(shape);
		}
	}

	for(XSLFShape shapeToDelete : shapesToDelete) {
		shapeContainer.removeShape(shapeToDelete);
	}

	for(ImageToReplace imageToReplace : imagesToReplace) {
		replaceImage(ppt, shapeContainer, imageToReplace);
	}
}
 
Example #4
Source File: PowerPointIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRetrievingThePlaceholdersForEachSlide_thenOK() throws Exception {
    XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);

    List<XSLFShape> onlyTitleSlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(0));
    List<XSLFShape> titleAndBodySlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(1));
    List<XSLFShape> emptySlidePlaceholdes = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(3));

    Assert.assertEquals(1, onlyTitleSlidePlaceholders.size());
    Assert.assertEquals(2, titleAndBodySlidePlaceholders.size());
    Assert.assertEquals(0, emptySlidePlaceholdes.size());

}
 
Example #5
Source File: PptxWriterImpl.java    From tephra with MIT License 5 votes vote down vote up
private void parseShapes(WriterContext writerContext, JSONArray shapes) {
    for (int i = 0, size = shapes.size(); i < size; i++) {
        JSONObject shape = shapes.getJSONObject(i);
        XSLFShape xslfShape = parser.createShape(writerContext, shape);
        if (xslfShape == null) {
            logger.warn(null, "无法处理PPTx形状[{}]!", shape);

            continue;
        }

        if (xslfShape instanceof XSLFSimpleShape)
            parser.parseShape(writerContext, (XSLFSimpleShape) xslfShape, shape);
    }
}
 
Example #6
Source File: ParserImpl.java    From tephra with MIT License 5 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    XSLFShape xslfShape;
    for (Simple simple : simples)
        if ((xslfShape = simple.createShape(writerContext, shape)) != null)
            return xslfShape;

    for (Graphic graphic : graphics)
        if ((xslfShape = graphic.createShape(writerContext, shape)) != null)
            return xslfShape;

    return null;
}
 
Example #7
Source File: GraphicImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #8
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);
	}
}
 
Example #9
Source File: FlipImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #10
Source File: AnchorImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #11
Source File: TextureImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #12
Source File: TableParserImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public boolean parse(JSONObject object, XSLFShape xslfShape, StreamWriter streamWriter) {
    return false;
}
 
Example #13
Source File: RotationImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #14
Source File: TableImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #15
Source File: ShadowImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #16
Source File: GeometryImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public XSLFShape createShape(WriterContext writerContext, JSONObject shape) {
    return null;
}
 
Example #17
Source File: SvgParserImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public boolean parse(JSONObject object, XSLFShape xslfShape, StreamWriter writer) {
    return false;
}
 
Example #18
Source File: BackgroundParserImpl.java    From tephra with MIT License 4 votes vote down vote up
@Override
public boolean parse(JSONObject object, XSLFShape xslfShape, StreamWriter streamWriter) {
    return false;
}
 
Example #19
Source File: Parser.java    From tephra with MIT License 2 votes vote down vote up
/**
 * 创建形状。
 *
 * @param writerContext 写上下文。
 * @param shape         形状数据。
 * @return 形状;如果创建失败则返回null。
 */
XSLFShape createShape(WriterContext writerContext, JSONObject shape);
 
Example #20
Source File: Creater.java    From tephra with MIT License 2 votes vote down vote up
/**
 * 创建形状。
 *
 * @param writerContext 写上下文。
 * @param shape         形状数据。
 * @return 形状;如果创建失败则返回null。
 */
XSLFShape createShape(WriterContext writerContext, JSONObject shape);
 
Example #21
Source File: Parser.java    From tephra with MIT License 2 votes vote down vote up
/**
 * 解析PPTx元素数据。
 *
 * @param object       解析后的数据。
 * @param xslfShape    要解析的PPTx元素。
 * @param streamWriter 流数据写入器。
 * @return 如果解析成功则返回true;否则返回false。
 */
boolean parse(JSONObject object, XSLFShape xslfShape, StreamWriter streamWriter);