org.apache.poi.xslf.usermodel.XSLFPictureData Java Examples
The following examples show how to use
org.apache.poi.xslf.usermodel.XSLFPictureData.
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 |
@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 |
@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: ImageImpl.java From tephra with MIT License | 6 votes |
@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 #4
Source File: ImageImpl.java From tephra with MIT License | 6 votes |
@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 #5
Source File: PptTemplates.java From PPT-Templates with Apache License 2.0 | 6 votes |
/** * Replace an image with another while keeping * all the properties of the old image: z-index, border, shadow... */ private static void replaceImageInPlace(POIXMLDocumentPart containerDocument, ImageToReplace imageToReplace, XSLFPictureData newPictureData, Rectangle2D newImageAnchor) { RelationPart rp = containerDocument.addRelation(null, XSLFRelation.IMAGES, newPictureData); CTPicture pictureXml = (CTPicture) imageToReplace.toReplace.getXmlObject(); CTBlip pictureBlip = pictureXml.getBlipFill().getBlip(); String relationId = pictureBlip.getEmbed(); if(canRelationBeRemoved(containerDocument, relationId)) { // clean up the old picture data PptPoiBridge.removeRelation(containerDocument, containerDocument.getRelationById(relationId)); } pictureBlip.setEmbed(rp.getRelationship().getId()); imageToReplace.toReplace.setAnchor(newImageAnchor); }
Example #6
Source File: ImageParserSupport.java From tephra with MIT License | 5 votes |
void parse(XSLFSlide xslfSlide, XSLFPictureData xslfPictureData, JSONObject object) { if (!object.containsKey("alpha")) { parseImage(xslfSlide, xslfPictureData, object); return; } double alpha = object.getDoubleValue("alpha"); if (alpha >= 1.0D) { parseImage(xslfSlide, xslfPictureData, object); return; } PackagePart packagePart = xslfPictureData.getPackagePart(); POIXMLDocumentPart.RelationPart relationPart = xslfSlide.addRelation(null, XSLFRelation.IMAGES, new XSLFPictureData(packagePart)); XSLFAutoShape xslfAutoShape = xslfSlide.createAutoShape(); CTShape ctShape = (CTShape) xslfAutoShape.getXmlObject(); CTBlipFillProperties ctBlipFillProperties = ctShape.getSpPr().addNewBlipFill(); CTBlip ctBlip = ctBlipFillProperties.addNewBlip(); ctBlip.setEmbed(relationPart.getRelationship().getId()); ctBlip.setCstate(STBlipCompression.PRINT); ctBlip.addNewAlphaModFix().setAmt(numeric.toInt(alpha * 100000)); ctBlipFillProperties.addNewSrcRect(); ctBlipFillProperties.addNewStretch().addNewFillRect(); xslfAutoShape.setAnchor(parserHelper.getRectangle(object)); parserHelper.rotate(xslfAutoShape, object); }
Example #7
Source File: ImageImpl.java From tephra with MIT License | 5 votes |
private void parseSize(XSLFPictureData xslfPictureData, JSONObject image) { Dimension dimension = xslfPictureData.getImageDimensionInPixels(); JSONObject size = new JSONObject(); size.put("width", dimension.width); size.put("height", dimension.height); image.put("size", size); }
Example #8
Source File: ImageParserSupport.java From tephra with MIT License | 4 votes |
private void parseImage(XSLFSlide xslfSlide, XSLFPictureData xslfPictureData, JSONObject object) { XSLFPictureShape xslfPictureShape = xslfSlide.createPicture(xslfPictureData); xslfPictureShape.setAnchor(parserHelper.getRectangle(object)); parserHelper.rotate(xslfPictureShape, object); }
Example #9
Source File: PptTemplates.java From PPT-Templates with Apache License 2.0 | 4 votes |
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); } }