⇤ ← Revision 1 as of 2019-03-12 11:20:54
Size: 140
Comment:
|
Size: 5078
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
== Example == == structure == {{{ |-- pom.xml |-- src | `-- main | `-- java | `-- org | `-- allowed | `-- bitarus | `-- apache | `-- poi | `-- test | `-- Main.java | }}} == pom.xml == {{{#!highlight xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>apache-poi-test</groupId> <artifactId>apache-poi-test</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <!--<version>3.5.1</version>--> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>org.allowed.bitarus.apache.poi.test.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> </dependencies> </project> }}} == Main.java == {{{ #!highlight java package org.allowed.bitarus.apache.poi.test; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.File; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFShape; import java.util.List; import org.apache.poi.xslf.usermodel.XSLFTextShape; import org.apache.poi.xslf.usermodel.XSLFPictureShape; import org.apache.poi.xslf.usermodel.XSLFPictureData; import org.apache.poi.xslf.usermodel.XSLFTextParagraph; public class Main { public static void main(String args[]) { try{ File file = new File(args[0]); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); List<XSLFSlide> slides = ppt.getSlides(); for ( XSLFSlide slide : slides ){ System.out.println(String.format("%s %d", slide.getSlideName() , slide.getSlideNumber() )); List<XSLFShape> shapes = slide.getShapes(); for (XSLFShape shape: shapes ){ System.out.println(String.format("Shape name: %s",shape.getShapeName() ) ); if( shape instanceof XSLFTextShape){ System.out.println(String.format("Shape text: %s", ((XSLFTextShape) shape).getText() ) ); for(XSLFTextParagraph para: ( (XSLFTextShape) shape).getTextParagraphs() ){ System.out.println(String.format("Paragraph text: %s bullet: %b indentLevel: %d", para.getText(), para.isBullet(), para.getIndentLevel() ) ); } } if( shape instanceof XSLFPictureShape ){ XSLFPictureData pd = (( XSLFPictureShape ) shape).getPictureData(); System.out.println(String.format("Filename: %s", pd.getFileName() ) ); System.out.println(String.format("Content type: %s", pd.getContentType() ) ); System.out.println(String.format("Image dimension (points): %s", pd.getImageDimension().toString() ) ); System.out.println(String.format("Image dimension (pixels): %s", pd.getImageDimensionInPixels().toString() ) ); } } } ppt.close(); } catch(Exception ex){ ex.printStackTrace(); } } } }}} |
apachePOI
Create, modify, and display MS-Office files using Java programs
Example
structure
|-- pom.xml |-- src | `-- main | `-- java | `-- org | `-- allowed | `-- bitarus | `-- apache | `-- poi | `-- test | `-- Main.java |
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>apache-poi-test</groupId>
7 <artifactId>apache-poi-test</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <build>
10 <sourceDirectory>src</sourceDirectory>
11 <plugins>
12 <plugin>
13 <artifactId>maven-compiler-plugin</artifactId>
14 <!--<version>3.5.1</version>-->
15 <configuration>
16 <source>1.8</source>
17 <target>1.8</target>
18 </configuration>
19 </plugin>
20 <plugin>
21 <artifactId>maven-assembly-plugin</artifactId>
22 <version>2.4</version>
23 <configuration>
24 <descriptorRefs>
25 <descriptorRef>jar-with-dependencies</descriptorRef>
26 </descriptorRefs>
27 <archive>
28 <manifest>
29 <mainClass>org.allowed.bitarus.apache.poi.test.Main</mainClass>
30 </manifest>
31 </archive>
32 </configuration>
33 <executions>
34 <execution>
35 <id>make-assembly</id>
36 <phase>package</phase>
37 <goals>
38 <goal>single</goal>
39 </goals>
40 </execution>
41 </executions>
42 </plugin>
43 </plugins>
44 </build>
45 <dependencies>
46 <dependency>
47 <groupId>org.apache.poi</groupId>
48 <artifactId>poi</artifactId>
49 <version>4.0.1</version>
50 </dependency>
51 <dependency>
52 <groupId>org.apache.poi</groupId>
53 <artifactId>poi-ooxml</artifactId>
54 <version>4.0.1</version>
55 </dependency>
56 </dependencies>
57 </project>
Main.java
1 package org.allowed.bitarus.apache.poi.test;
2
3 import java.io.FileOutputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.File;
7 import org.apache.poi.xslf.usermodel.XMLSlideShow;
8 import org.apache.poi.xslf.usermodel.XSLFSlide;
9 import org.apache.poi.xslf.usermodel.XSLFShape;
10 import java.util.List;
11 import org.apache.poi.xslf.usermodel.XSLFTextShape;
12 import org.apache.poi.xslf.usermodel.XSLFPictureShape;
13 import org.apache.poi.xslf.usermodel.XSLFPictureData;
14 import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
15
16 public class Main {
17
18 public static void main(String args[]) {
19 try{
20
21 File file = new File(args[0]);
22 XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
23
24 List<XSLFSlide> slides = ppt.getSlides();
25
26 for ( XSLFSlide slide : slides ){
27 System.out.println(String.format("%s %d", slide.getSlideName() , slide.getSlideNumber() ));
28 List<XSLFShape> shapes = slide.getShapes();
29 for (XSLFShape shape: shapes ){
30 System.out.println(String.format("Shape name: %s",shape.getShapeName() ) );
31
32 if( shape instanceof XSLFTextShape){
33 System.out.println(String.format("Shape text: %s", ((XSLFTextShape) shape).getText() ) );
34
35 for(XSLFTextParagraph para: ( (XSLFTextShape) shape).getTextParagraphs() ){
36 System.out.println(String.format("Paragraph text: %s bullet: %b indentLevel: %d", para.getText(),
37 para.isBullet(),
38 para.getIndentLevel() ) );
39 }
40 }
41
42 if( shape instanceof XSLFPictureShape ){
43 XSLFPictureData pd = (( XSLFPictureShape ) shape).getPictureData();
44 System.out.println(String.format("Filename: %s", pd.getFileName() ) );
45 System.out.println(String.format("Content type: %s", pd.getContentType() ) );
46 System.out.println(String.format("Image dimension (points): %s", pd.getImageDimension().toString() ) );
47 System.out.println(String.format("Image dimension (pixels): %s",
48 pd.getImageDimensionInPixels().toString() ) );
49 }
50
51 }
52 }
53 ppt.close();
54 }
55 catch(Exception ex){
56 ex.printStackTrace();
57 }
58 }
59 }