无法使用Apache poi编辑现有.ppt文档中的链接

时间:2018-01-01 07:07:50

标签: java apache-poi

我的要求是编辑.ppt文档(Microsoft PowerPoint文档)中的超链接。 我正在使用apache poi 3.17编辑现有.ppt文档的链接,下面是我的代码

    public class ReadPPT {
    public static void readPPT(String path) {
        try {
            File file = new File(path);
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\subham\\Desktop\\fromJava.ppt"));
            HSLFSlideShow show = new HSLFSlideShow(fis);

            List<HSLFSlide> slides = show.getSlides();

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

                HSLFSlide slide = slides.get(x);
                List<HSLFShape> shapes = slide.getShapes();
                for (int i=0; i < shapes.size(); i++) {
                    HSLFShape hslfShape = (HSLFShape) shapes.get(i);

                    if(hslfShape instanceof HSLFTextShape) {

                        HSLFTextShape hslfTextShape = (HSLFTextShape)hslfShape;
                        List<HSLFHyperlink> hyperlinks = hslfTextShape.getHyperlinks();

                        for (int j=0; j < hyperlinks.size(); j++) {
                            HSLFHyperlink hslfHyperlink = (HSLFHyperlink) hyperlinks.get(j);

                            if(hslfHyperlink != null) {

                                //System.out.println("Hyperlink label" + hslfHyperlink.getLabel());
                                System.out.println("Hyperlink address " + hslfHyperlink.getAddress());
                                hslfHyperlink.setAddress("http://www.hello.com");
                                hyperlinks.set(j, hslfHyperlink);
                                System.out.println("After change " + hyperlinks.get(j).getAddress());
                            }

                        }
                        shapes.set(i, hslfShape);

                    }

                }

            }
            show.write(fos);
            show.close();
            fis.close(); 
            fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        readPPT("C:\\Users\\subham\\Desktop\\PowerPoint_Tutorial.ppt");

    }
}

问题是hslfHyperlink.setAddress("http://www.hello.com");不会更改文档中的链接,并且在生成的fromJava.ppt文档中链接不会被修改。

0 个答案:

没有答案