Search in sources :

Example 1 with XSLFSlide

use of org.apache.poi.xslf.usermodel.XSLFSlide in project poi by apache.

the class Step1 method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Input file is required");
        return;
    }
    FileInputStream fis = new FileInputStream(args[0]);
    XMLSlideShow ppt = new XMLSlideShow(fis);
    fis.close();
    for (XSLFSlide slide : ppt.getSlides()) {
        System.out.println("Title: " + slide.getTitle());
        for (XSLFShape shape : slide.getShapes()) {
            if (shape instanceof XSLFTextShape) {
                XSLFTextShape tsh = (XSLFTextShape) shape;
                for (XSLFTextParagraph p : tsh) {
                    System.out.println("Paragraph level: " + p.getIndentLevel());
                    for (XSLFTextRun r : p) {
                        System.out.println(r.getRawText());
                        System.out.println("  bold: " + r.isBold());
                        System.out.println("  italic: " + r.isItalic());
                        System.out.println("  underline: " + r.isUnderlined());
                        System.out.println("  font.family: " + r.getFontFamily());
                        System.out.println("  font.size: " + r.getFontSize());
                        System.out.println("  font.color: " + r.getFontColor());
                    }
                }
            }
        }
    }
    ppt.close();
}
Also used : XSLFSlide(org.apache.poi.xslf.usermodel.XSLFSlide) XSLFTextParagraph(org.apache.poi.xslf.usermodel.XSLFTextParagraph) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSLFTextShape(org.apache.poi.xslf.usermodel.XSLFTextShape) XSLFShape(org.apache.poi.xslf.usermodel.XSLFShape) FileInputStream(java.io.FileInputStream) XSLFTextRun(org.apache.poi.xslf.usermodel.XSLFTextRun)

Example 2 with XSLFSlide

use of org.apache.poi.xslf.usermodel.XSLFSlide in project poi by apache.

the class Step2 method main.

public static void main(String[] args) throws Exception {
    XMLSlideShow ppt = new XMLSlideShow();
    // first see what slide layouts are available by default
    System.out.println("Available slide layouts:");
    for (XSLFSlideMaster master : ppt.getSlideMasters()) {
        for (XSLFSlideLayout layout : master.getSlideLayouts()) {
            System.out.println(layout.getType());
        }
    }
    // blank slide
    /*XSLFSlide blankSlide =*/
    ppt.createSlide();
    XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
    // title slide
    XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
    XSLFSlide slide1 = ppt.createSlide(titleLayout);
    XSLFTextShape title1 = slide1.getPlaceholder(0);
    title1.setText("First Title");
    // title and content
    XSLFSlideLayout titleBodyLayout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
    XSLFSlide slide2 = ppt.createSlide(titleBodyLayout);
    XSLFTextShape title2 = slide2.getPlaceholder(0);
    title2.setText("Second Title");
    XSLFTextShape body2 = slide2.getPlaceholder(1);
    // unset any existing text
    body2.clearText();
    body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
    body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
    body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
    FileOutputStream out = new FileOutputStream("step2.pptx");
    ppt.write(out);
    out.close();
    ppt.close();
}
Also used : XSLFSlideMaster(org.apache.poi.xslf.usermodel.XSLFSlideMaster) XSLFSlide(org.apache.poi.xslf.usermodel.XSLFSlide) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSLFTextShape(org.apache.poi.xslf.usermodel.XSLFTextShape) FileOutputStream(java.io.FileOutputStream) XSLFSlideLayout(org.apache.poi.xslf.usermodel.XSLFSlideLayout)

Example 3 with XSLFSlide

use of org.apache.poi.xslf.usermodel.XSLFSlide in project poi by apache.

the class TestXSLFSlideShow method testMasterBackground.

@Test
public void testMasterBackground() throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();
    XSLFBackground b = ppt.getSlideMasters().get(0).getBackground();
    b.setFillColor(Color.RED);
    XSLFSlide sl = ppt.createSlide();
    XSLFAutoShape as = sl.createAutoShape();
    as.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
    as.setShapeType(ShapeType.CLOUD);
    XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt);
    ppt.close();
    XSLFBackground b2 = ppt2.getSlideMasters().get(0).getBackground();
    assertEquals(Color.RED, b2.getFillColor());
    ppt2.close();
}
Also used : XSLFSlide(org.apache.poi.xslf.usermodel.XSLFSlide) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSLFAutoShape(org.apache.poi.xslf.usermodel.XSLFAutoShape) Rectangle2D(java.awt.geom.Rectangle2D) XSLFBackground(org.apache.poi.xslf.usermodel.XSLFBackground) Test(org.junit.Test)

Example 4 with XSLFSlide

use of org.apache.poi.xslf.usermodel.XSLFSlide 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)

Aggregations

XMLSlideShow (org.apache.poi.xslf.usermodel.XMLSlideShow)4 XSLFSlide (org.apache.poi.xslf.usermodel.XSLFSlide)4 XSLFTextShape (org.apache.poi.xslf.usermodel.XSLFTextShape)3 Rectangle2D (java.awt.geom.Rectangle2D)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 XSLFShape (org.apache.poi.xslf.usermodel.XSLFShape)2 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 XSLFAutoShape (org.apache.poi.xslf.usermodel.XSLFAutoShape)1 XSLFBackground (org.apache.poi.xslf.usermodel.XSLFBackground)1 XSLFGroupShape (org.apache.poi.xslf.usermodel.XSLFGroupShape)1 XSLFPictureData (org.apache.poi.xslf.usermodel.XSLFPictureData)1 XSLFPictureShape (org.apache.poi.xslf.usermodel.XSLFPictureShape)1 XSLFSlideLayout (org.apache.poi.xslf.usermodel.XSLFSlideLayout)1 XSLFSlideMaster (org.apache.poi.xslf.usermodel.XSLFSlideMaster)1 XSLFTextParagraph (org.apache.poi.xslf.usermodel.XSLFTextParagraph)1 XSLFTextRun (org.apache.poi.xslf.usermodel.XSLFTextRun)1 Test (org.junit.Test)1