org.apache.poi.hslf.usermodel.HSLFSlide Java Examples

The following examples show how to use org.apache.poi.hslf.usermodel.HSLFSlide. 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: PPTUtil.java    From SpringMVC-Project with MIT License 6 votes vote down vote up
/**
 * 转换2003版(.ppt)格式的PPT文件为图片
 */
private static List<String> convertPPT2003ToImages(String pptFilePath, String imageFolderPath) throws IOException {
    List<String> imagePathList = Lists.newArrayList();

    FileInputStream fis = new FileInputStream(pptFilePath);
    HSLFSlideShow ppt = new HSLFSlideShow(fis);
    fis.close();

    Dimension dimension = ppt.getPageSize();
    List<HSLFSlide> slideList = ppt.getSlides();
    int index = 0;
    for (HSLFSlide slide : slideList) {

        logger.info("正在转换PPT第" + (++index) + "页");

        File imageFile = new File(imageFolderPath + "/" + (index) + ".png");

        convertSlideToImage(slide, dimension, imageFile);

        imagePathList.add(imageFile.getAbsolutePath());
    }

    return imagePathList;
}
 
Example #2
Source File: PPTPresentation.java    From Quelea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Make the slides that go in this presentation, this is what takes time and
 * should only be done once.
 *
 * @return all the slides.
 */
private PresentationSlide[] makeSlides() {
    List<HSLFSlide> lSlides = slideshow.getSlides();
    PresentationSlide[] ret = new PresentationSlide[lSlides.size()];
    for (int i = 0; i < lSlides.size(); i++) {
        ret[i] = new PresentationSlide(lSlides.get(i), i + 1);
    }
    return ret;
}
 
Example #3
Source File: PPTSlideRenderPanel.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void drawSlidesPartially(Graphics2D graphics, HSLFSlide slide) {
        HSLFSlideMaster master = (HSLFSlideMaster) slide.getMasterSheet();

        if (slide.getFollowMasterBackground() && master.getBackground() != null) {
//            master.getBackground().draw(graphics, null);
            factoryDraw(graphics, master.getBackground());
        }

        if (slide.getFollowMasterObjects()) {

            java.util.List<HSLFShape> sh = master.getShapes();
            for (HSLFShape aSh : sh) {
                if (aSh.isPlaceholder()) continue;

                aSh.draw(graphics, null);
            }
        }


        HSLFShape titleShape = getTitleShape(slide);

        for (HSLFShape shape : slide.getShapes()) {
            boolean isTitleShape = shape.getShapeId() == titleShape.getShapeId();

            if (isTitleShape && showTitleShape) {
//                shape.draw(graphics);
                factoryDraw(graphics, shape);
            }

            if (!isTitleShape && showContent) {
//                shape.draw(graphics);
                factoryDraw(graphics, shape);
            }
        }
    }
 
Example #4
Source File: PPTSlideRenderPanel.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private HSLFShape getTitleShape(HSLFSlide slide) {
        String slideTitle = slide.getTitle();

        for (HSLFShape shape : slide.getShapes()) {
            if (shape instanceof AutoShape) {
                HSLFAutoShape autoShape = (HSLFAutoShape) shape;
                if (autoShape.getText() != null && autoShape.getText().equals(slideTitle)) {
                    int type = autoShape.getRunType();

                    if (type == TextHeaderAtom.CENTER_TITLE_TYPE || type == TextHeaderAtom.TITLE_TYPE) {
                        return shape;
                    }
                }
            }
        }

//  When you have a XSLFSlide object you can use .getShapes() to get all shapes in the slide. If the shape is a
// XSLFTextShape you can use .getTextType() to check if it's a title, .getTextParagraphs() to get the paragraphs and
// .getTextRuns() on the paragraphs to get the text runs with the text. That should give you

        return null;

        // can not work as we don't have a slide title for slides without a title element
//        // if we don't find a title shape than use the most topwards element as question
//        if(slide.getShapes().length ==0)
//            return null;
//
//        return Collections.max(Arrays.asList(slide.getShapes()), new Comparator<Shape>() {
//            @Override
//            public int compare(Shape o1, Shape o2) {
//                return o1.getAnchor().getCenterY() - o2.getAnchor().getCenterY() < 0 ? -1 : 1;
//            }
//        });
    }
 
Example #5
Source File: PPTSlideRenderPanel.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void configure(Slide slide, boolean showTitle, boolean showContent) {
    this.slide = (HSLFSlide) slide;

    this.showTitleShape = showTitle;
    this.showContent = showContent;

    repaint();
}
 
Example #6
Source File: ConvertPPT2PNG.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        FileInputStream is = new FileInputStream("/Users/brandl/Dropbox/private/oc2/testdata/experimental design.ppt");
//        FileInputStream is = new FileInputStream("/Users/brandl/Dropbox/private/oc2/testdata/Presentation5.ppt");

        HSLFSlideShow ppt = new HSLFSlideShow(is);


        is.close();

        Dimension pgsize = ppt.getPageSize();

        java.util.List<HSLFSlide> slides = ppt.getSlides();

        for (int i = 0; i < slides.size(); i++) {

            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            //clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

            //render
            HSLFSlide slide1 = slides.get(i);

            slide1.draw(graphics);

            //save the output
            FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + slide1.getTitle() + ".png");
            javax.imageio.ImageIO.write(img, "png", out);
            out.close();
        }
    }
 
Example #7
Source File: ExtractSlidesFromPPTX.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
//        XMLSlideShow ppt = new XMLSlideShow();
        FileInputStream is = new FileInputStream("/Users/brandl/Dropbox/private/oc2/testdata/experimental design.ppt");
        HSLFSlideShow ppt = new HSLFSlideShow(is);

        for (HSLFSlide xslfSlide : ppt.getSlides()) {
            System.out.println(xslfSlide.getTitle());
        }

//        XSLFSlide slide getTitle= ppt.getSlides()[0];0

//         new org.apache.poi.hslf.extractor.PowerPointExtractor("xslf-demo.pptx").getSlides
    }
 
Example #8
Source File: ExtractSlidesFromPPT.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
//        XMLSlideShow ppt = new XMLSlideShow();
//        FileInputStream is = new FileInputStream("/Users/brandl/Dropbox/private/oc2/testdata/experimental design.ppt");
        FileInputStream is = new FileInputStream("testdata/testdata 1 reordered slides.ppt");
        HSLFSlideShow ppt = new HSLFSlideShow(is);

        for (HSLFSlide slide : ppt.getSlides()) {
            String slideTitle = slide.getTitle();

            System.err.println("-----------");
            System.err.println(slideTitle);


//            System.err.println("sheetid   : "+slide.getSlideRecord().getSheetId());
//            // does just reflect the slide number
//
//            System.err.println("refsheetid: "+ slide._getSheetRefId());
//
//            System.err.println("atomhah: "+ slide.getSlideRecord().getSlideAtom().toString());
//
//            System.err.println("ppdrawing: "+ slide.getSlideRecord().toString());

            System.err.println(slide.getSlideRecord().getPPDrawing());

            slide.getSlideRecord().getPPDrawing().toString();
            slide.getSlideRecord().getSlideAtom().hashCode();


//        XSLFSlide slide getTitle= ppt.getSlides()[0];0

//         new org.apache.poi.hslf.extractor.PowerPointExtractor("xslf-demo.pptx").getSlides
        }
    }
 
Example #9
Source File: ConvertPPTX2PNG.java    From opencards with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws IOException, InvalidFormatException {
        FileInputStream is = new FileInputStream("/Users/brandl/Dropbox/private/oc2/testdata/experimental design.pptx");

        XMLSlideShow ppt2 = new XMLSlideShow(OPCPackage.open("/Users/brandl/Dropbox/private/oc2/testdata/experimental design.pptx"));
        XSLFSlide slide1 = ppt2.getSlides().get(0);
//        slide1.get

        HSLFSlideShow ppt = new HSLFSlideShow(is);
//
        HSLFSlide slide2 = ppt.getSlides().get(1);


        is.close();

        Dimension pgsize = ppt.getPageSize();

        java.util.List<HSLFSlide> slides = ppt.getSlides();

        for (int i = 0; i < slides.size(); i++) {

            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            //clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

            //render
            slides.get(i).draw(graphics);

            //save the output
            FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
            javax.imageio.ImageIO.write(img, "png", out);
            out.close();
        }
    }
 
Example #10
Source File: PPTSerializer.java    From opencards with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public FlashCardCollection readFlashcardsFromFile(CardFile cardFile) {
        Utils.log("extracting  flashcards from file '" + cardFile + "'...");

        FlashCardCollection fc = new FlashCardCollection();
        try {
            if (cardFile.getFileLocation().getName().endsWith(".ppt")) {

                FileInputStream is = new FileInputStream(cardFile.getFileLocation());
                HSLFSlideShow ppt = new HSLFSlideShow(is);

                for (HSLFSlide xslfSlide : ppt.getSlides()) {
                    String slideTitle = xslfSlide.getTitle();
                    if (slideTitle == null)
                        continue;

                    // old OC1.x approach to create a unique card-id
//                int cardID = Utils.getRandGen().nextInt(Integer.MAX_VALUE);

                    fc.add(new FlashCard(slideTitle.hashCode(), slideTitle, xslfSlide.getSlideNumber()));
                }


            } else if (cardFile.getFileLocation().getName().endsWith(".md")) {
                boolean useSelector = cardFile.getProperties().useMarkdownSelector();
                List<MarkdownFlashcard> flashcards = MarkdownParserKt.parseMD(cardFile.getFileLocation(), useSelector);

                for (int i = 0; i < flashcards.size(); i++) {
                    MarkdownFlashcard card = flashcards.get(i);
                    String question = card.getQuestion();
                    if (question.trim().isEmpty()) {
                        continue;
                    }

                    fc.add(new FlashCard(question.hashCode(), question, i + 1));

                }
            } else {
                throw new InvalidCardFileFormatException();
            }

        } catch (IOException e) {
            // rephrase IO problem into something more specific
            throw new InvalidCardFileFormatException();
        }

        return fc;
    }
 
Example #11
Source File: ImportManager.java    From opencards with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ImportManager(final Frame owner) {
        final String DEFAULT_DIR = "import.defdir";
        File defDir = new File(Utils.getPrefs().get(DEFAULT_DIR, System.getProperty("user.home")));
        JFileChooser importChooser = new JFileChooser(defDir);

        ImpSeparatorPanel sepPanel = new ImpSeparatorPanel(importChooser);

        importChooser.setDialogTitle(Utils.getRB().getString("cardimport.filechoose.title"));
        importChooser.setMultiSelectionEnabled(false);

        FileFilter csvFilter = new FileFilter() {

            public boolean accept(File f) {
                String fileName = f.getName();
                return (fileName.endsWith(".csv") || fileName.endsWith(".txt")) || f.isDirectory();
            }


            public String getDescription() {
                return "Text (*.csv, *.txt)";
            }
        };

        importChooser.setFileFilter(csvFilter);
        int status = importChooser.showOpenDialog(null);
        if (status != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File selectedFile = importChooser.getSelectedFile();
        FileFilter selectedFilter = importChooser.getFileFilter();

        Utils.getPrefs().put(DEFAULT_DIR, selectedFile.getParentFile().getAbsolutePath());

        HSLFSlideShow slideShow = new HSLFSlideShow();

        if (selectedFilter == csvFilter) {
            Map<String, String> title2contents = readCsvFile(selectedFile, sepPanel.getCurSeparator());

            for (String slideTitle : title2contents.keySet()) {
                HSLFSlide slide = slideShow.createSlide();

                // create question shape
                HSLFTextBox title = slide.addTitle();
                title.setText(slideTitle);

                // create answer shape
                HSLFShape titleShape = slide.getShapes().get(0);

                HSLFTextBox txt = new HSLFTextBox();
                txt.setText(title2contents.get(slideTitle));
                Rectangle titleAnchor = titleShape.getAnchor().getBounds();
                txt.setAnchor(new Rectangle((int) titleAnchor.getX(), (int) titleAnchor.getY() + 200, (int) titleAnchor.getWidth(), (int) titleAnchor.getHeight()));

//use RichTextRun to work with the text format
//                HSLFTextShape titleFormat = ((AutoShape) titleShape).getStrokeStyle().getRichTextRuns()[0];
//                HSLFTextShape questionFormat = txt.getShapeType();
//                questionFormat.setFontSize(titleFormat.getFontSize());
//                slide.getShapes()[0];
//                questionFormat.setFontName(titleFormat.getFontName());
//                questionFormat.setAlignment(TextBox.AlignCenter);

                slide.addShape(txt);
            }

        }

        // show the FAQ if nothing was imported. But do this only once to avoid users to become annoyed
        String SHOWN_IMPORT_HELP_BEFORE = "hasShownImportHelp";
        if (slideShow.getSlides().size() < 2 && !Utils.getPrefs().getBoolean(SHOWN_IMPORT_HELP_BEFORE, false)) {
            Utils.getPrefs().putBoolean(SHOWN_IMPORT_HELP_BEFORE, true);
            new URLAction("nocardsimported", AboutDialog.OC_WEBSITE + "help").actionPerformed(null);
        } else {

            // save the sldeshow into a ppt
            final JFileChooser fc = new JFileChooser();
            fc.setSelectedFile(new File(selectedFile.getAbsolutePath() + ".ppt"));
            fc.setDialogTitle("Save imported flashcards as ");
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fc.setMultiSelectionEnabled(false);
            if (fc.showSaveDialog(owner) == JFileChooser.APPROVE_OPTION) {
                File saveFile = fc.getSelectedFile();

                try {
                    FileOutputStream out = new FileOutputStream(saveFile);
                    slideShow.write(out);
                    out.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }

        }
    }