jacob的方法,足可以解决这个问题,但是我既然以前曾经做过报表,就想尝试不同的方法。
jacob是一座连接java和微软的桥,所有的解析由微软解析。poi是没有微软解析的那么原汁原味的,所以如果要求高的话,还是使用jacob。
大致思路很简单,将ppt先转化为图片,然后将图片写入pdf。转化图片是用poi,操作pdf使用itex。不过这个方法的bug就是转化图片的poi效果不是很好。
导入的包分别是:itextpdf-5.1.3.jar,poi-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。
然后贴代码了:
代码没有进行参数统一,写两个方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package com.zzk.cn;
import java.awt.dimension;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.awt.color;
import java.awt.dimension;
import java.awt.graphics2d;
import java.awt.geom.rectangle2d;
import java.awt.image.bufferedimage;
import org.apache.poi.hslf.model.textrun;
import org.apache.poi.hslf.record.slide;
import org.apache.poi.hslf.usermodel.richtextrun;
import org.apache.poi.hslf.usermodel.slideshow;
public class ppttoimage {
public static void main(string[] args) {
// 读入ppt文件
file file = new file( "d:/书本jvm总结7-9.ppt" );
doppttoimage(file);
}
public static boolean doppttoimage(file file) {
boolean isppt = checkfile(file);
if (!isppt) {
system.out.println( "你指定的文件不是ppt文档!" );
return false ;
}
try {
fileinputstream is = new fileinputstream(file);
slideshow ppt = new slideshow(is);
is.close();
dimension pgsize = ppt.getpagesize();
org.apache.poi.hslf.model.slide[] slide = ppt.getslides();
for ( int i = 0 ; i < slide.length; i++) {
system.out.print( "第" + i + "页。" );
if (slide[i].getnotessheet() != null
&& slide[i].getnotessheet().gettextruns() != null ) {
// 获取第一个备注
system.out.println( "备注:"
+ slide[i].getnotessheet().gettextruns()[ 0 ]
.gettext());
}
textrun[] truns = slide[i].gettextruns();
for ( int k = 0 ; k < truns.length; k++) {
richtextrun[] rtruns = truns[k].getrichtextruns();
for ( int l = 0 ; l < rtruns.length; l++) {
rtruns[l].setfontindex( 1 );
rtruns[l].setfontname( "宋体" );
// 获取文本列表
system.out.println(rtruns[l].gettext());
}
}
bufferedimage img = new bufferedimage(pgsize.width,
pgsize.height, bufferedimage.type_int_rgb);
graphics2d graphics = img.creategraphics();
graphics.setpaint(color.white);
graphics.fill( new rectangle2d. float ( 0 , 0 , pgsize.width,
pgsize.height));
slide[i].draw(graphics);
// 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
fileoutputstream out = new fileoutputstream( "d:/testimage/pict_"
+ (i + 1 ) + ".jpeg" );
javax.imageio.imageio.write(img, "jpeg" , out);
out.close();
}
system.out.println( "ok" );
return true ;
} catch (filenotfoundexception e) {
system.out.println(e);
} catch (ioexception e) {
e.printstacktrace();
}
return false ;
}
// function 检查文件是否为ppt
public static boolean checkfile(file file) {
boolean isppt = false ;
string filename = file.getname();
string suffixname = null ;
if (filename != null && filename.indexof( "." ) != - 1 ) {
suffixname = filename.substring(filename.indexof( "." ));
if (suffixname.equals( ".ppt" )) {
isppt = true ;
}
return isppt;
} else {
return isppt;
}
}
}
|
第二段代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.zzk.cn;
import java.io.fileoutputstream;
import java.io.ioexception;
import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.image;
import com.itextpdf.text.pdf.pdfwriter;
public class imagetopdf {
public static void main(string[] args) {
system.out.println( "chapter 6 example 3: using a relative path for html" );
// step 1: creation of a document-object
document document = new document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a pdf-stream to a file
pdfwriter.getinstance(document, new fileoutputstream( "d:/测试图片.pdf" ));
// htmlwriter writer = htmlwriter.getinstance(document, new fileoutputstream("chap0603.html"));
// writer.setimagepath("../../images/kerstmis/");
// step 3: we open the document
document.open();
for ( int i= 1 ;i<= 7 ;i++) {
// step 4: we add content
image jpg = image.getinstance( "d:/testimage/pict_" +i+ ".jpeg" );
jpg.scalepercent( 50 );
document.add(jpg);
}
}
catch (documentexception de) {
system.err.println(de.getmessage());
}
catch (ioexception ioe) {
system.err.println(ioe.getmessage());
}
// step 5: we close the document
document.close();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/opzoonzhuzhengke/article/details/7609833
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 25
-
2025-05-29 25
-
2025-05-27 40
-
2025-05-29 50
-
2025-05-29 60
热门评论