Search in sources :

Example 21 with XMLSlideShow

use of org.apache.poi.xslf.usermodel.XMLSlideShow in project CodeUtils by boredream.

the class TempUtils method replacePPT.

public static void replacePPT() {
    Map<String, String> replaceMap = new HashMap<String, String>();
    // replaceMap.put("实战名称", "微博实战:Android 视频播放");
    // replaceMap.put("知识点1", "我页面的实现");
    replaceMap.put("我页面的实现", "知识点1");
    replaceMap.put("个人中心页面的基本实现", "知识点2");
    replaceMap.put("个人中心页面菜单栏相关效果", "知识点3");
    replaceMap.put("个人中心页面背景图变化效果", "");
    try {
        // 获取ppt文件
        FileInputStream is = new FileInputStream("temp" + File.separator + "office" + File.separator + "ppt2007.pptx");
        XMLSlideShow ppt = new XMLSlideShow(is);
        is.close();
        // 获取幻灯片
        for (XSLFSlide slide : ppt.getSlides()) {
            // 获取每一张幻灯片中的shape
            for (XSLFShape shape : slide.getShapes()) {
                // position on the canvas
                Rectangle2D anchor = shape.getAnchor();
                if (shape instanceof XSLFTextShape) {
                    XSLFTextShape txShape = (XSLFTextShape) shape;
                    // 获取其中的文字
                    String text = txShape.getText();
                    // 替换文字内容
                    text = replaceAllMap(text, replaceMap);
                    txShape.setText(text);
                    if (text.contains("{picture}")) {
                        // // 替换图片
                        // byte[] pictureData = IOUtils.toByteArray(
                        // new FileInputStream("E:\\33.png"));
                        // int idx = ppt.addPicture(pictureData,
                        // XSLFPictureData.PICTURE_TYPE_PNG);
                        // XSLFPictureShape pic = slide.createPicture(idx);
                        // // 设置XSLFPictureShape的位置信息
                        // pic.setAnchor(anchor);
                        // // 移除XSLFTextShape
                        // slide.removeShape(txShape);
                        System.out.println("替换图片");
                    }
                } else if (shape instanceof XSLFGroupShape) {
                    System.out.println("替换group");
                    for (XSLFShape sunshape : ((XSLFGroupShape) shape).getShapes()) {
                        XSLFTextShape txSunShape = (XSLFTextShape) sunshape;
                        // 获取其中的文字
                        String text = txSunShape.getText();
                        // 替换文字内容
                        text = replaceAllMap(text, replaceMap);
                        txSunShape.setText(text);
                    // if (text.contains("{picture}")) {
                    // // 替换图片
                    // byte[] pictureData = IOUtils
                    // .toByteArray(new FileInputStream(
                    // "E:\\33.png"));
                    // int idx = ppt.addPicture(pictureData,
                    // XSLFPictureData.PICTURE_TYPE_PNG);
                    // XSLFPictureShape pic = slide.createPicture(idx);
                    // slide.removeShape(txSunShape);
                    // pic.setAnchor(anchor);
                    // }
                    }
                } else if (shape instanceof XSLFPictureShape) {
                    System.out.println("替换picture");
                    XSLFPictureShape pShape = (XSLFPictureShape) shape;
                    XSLFPictureData pData = pShape.getPictureData();
                    System.out.println(pData.getFileName());
                } else {
                    System.out.println("Process me: " + shape.getClass());
                }
            }
        }
        File file = new File("temp" + File.separator + "office" + File.separator + "ppt2007plus_new.pptx");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        ppt.write(out);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : XSLFPictureShape(org.apache.poi.xslf.usermodel.XSLFPictureShape) XSLFSlide(org.apache.poi.xslf.usermodel.XSLFSlide) HashMap(java.util.HashMap) XSLFGroupShape(org.apache.poi.xslf.usermodel.XSLFGroupShape) XSLFPictureData(org.apache.poi.xslf.usermodel.XSLFPictureData) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSLFTextShape(org.apache.poi.xslf.usermodel.XSLFTextShape) FileOutputStream(java.io.FileOutputStream) XSLFShape(org.apache.poi.xslf.usermodel.XSLFShape) File(java.io.File)

Example 22 with XMLSlideShow

use of org.apache.poi.xslf.usermodel.XMLSlideShow in project ddf by codice.

the class PptxInputTransformerTest method testThumbnailWithEmptySlideShow.

@Test
public void testThumbnailWithEmptySlideShow() throws IOException, CatalogTransformerException, InterruptedException {
    try (XMLSlideShow ss = new XMLSlideShow()) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ss.write(os);
            try (ByteArrayInputStream inStr = new ByteArrayInputStream(os.toByteArray())) {
                PptxInputTransformer t = new PptxInputTransformer(inputTransformer, true);
                Metacard m = t.transform(inStr);
                assertThat(m.getThumbnail(), is(nullValue()));
            }
        }
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 23 with XMLSlideShow

use of org.apache.poi.xslf.usermodel.XMLSlideShow in project ddf by codice.

the class PptxInputTransformerTest method testWithTitle.

@Test
public void testWithTitle() throws IOException, CatalogTransformerException, InterruptedException {
    try (XMLSlideShow ss = new XMLSlideShow()) {
        ss.createSlide();
        ss.getProperties().getCoreProperties().setTitle("TheTitle");
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ss.write(os);
            try (ByteArrayInputStream inStr = new ByteArrayInputStream(os.toByteArray())) {
                PptxInputTransformer t = new PptxInputTransformer(inputTransformer, true);
                Metacard m = t.transform(inStr);
                assertThat(m.getTitle(), is("TheTitle"));
            }
        }
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 24 with XMLSlideShow

use of org.apache.poi.xslf.usermodel.XMLSlideShow in project ddf by codice.

the class PptxInputTransformerTest method testModifiedDate.

@Test
public void testModifiedDate() throws IOException, CatalogTransformerException, InterruptedException {
    try (XMLSlideShow ss = new XMLSlideShow()) {
        ss.createSlide();
        Date d = createOneSecondPrecisionDate();
        ss.getProperties().getCoreProperties().setModified(new Nullable<>(d));
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ss.write(os);
            try (ByteArrayInputStream inStr = new ByteArrayInputStream(os.toByteArray())) {
                PptxInputTransformer t = new PptxInputTransformer(inputTransformer, true);
                Metacard m = t.transform(inStr);
                assertThat(m.getModifiedDate().getTime(), is(d.getTime()));
            }
        }
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.junit.Test)

Example 25 with XMLSlideShow

use of org.apache.poi.xslf.usermodel.XMLSlideShow in project ddf by codice.

the class PptxInputTransformerTest method testCreatedDate.

@Test
public void testCreatedDate() throws IOException, CatalogTransformerException, InterruptedException {
    try (XMLSlideShow ss = new XMLSlideShow()) {
        ss.createSlide();
        Date d = createOneSecondPrecisionDate();
        ss.getProperties().getCoreProperties().setCreated(new Nullable<>(d));
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ss.write(os);
            try (ByteArrayInputStream inStr = new ByteArrayInputStream(os.toByteArray())) {
                PptxInputTransformer t = new PptxInputTransformer(inputTransformer, true);
                Metacard m = t.transform(inStr);
                assertThat(m.getCreatedDate().getTime(), is(d.getTime()));
            }
        }
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.junit.Test)

Aggregations

XMLSlideShow (org.apache.poi.xslf.usermodel.XMLSlideShow)25 Test (org.junit.Test)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 Metacard (ddf.catalog.data.Metacard)5 XSLFSlide (org.apache.poi.xslf.usermodel.XSLFSlide)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 XSLFTextShape (org.apache.poi.xslf.usermodel.XSLFTextShape)3 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)3 Rectangle2D (java.awt.geom.Rectangle2D)2 FileOutputStream (java.io.FileOutputStream)2 Date (java.util.Date)2 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)2 XSLFShape (org.apache.poi.xslf.usermodel.XSLFShape)2