package com.clickoo.mercury.mail.attach.ppt;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.xmlbeans.XmlException;
public class PptExtractPicFormat{
private static final Log logger = LogFactory
.getLog(PptExtractPicFormat.class);
private static boolean isExistsFile(String fileName) {
File file = new File(fileName);
if (file.exists()) {
return true;
}
return false;
}
private void formatPptImageStyle(Slide[] slide, int i) {
logger.info("start ppt picrure style " + i);
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++) {
int index = rtruns[l].getFontIndex();
rtruns[l].setFontIndex(index);
rtruns[l].setFontName("Microsoft YaHei");
}
}
}
private static void addImageType(Graphics2D graphics) {
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
}
private byte[] extractPic(String filePath) {
File file = new File(filePath);
if (isExistsFile(filePath)) {
FileInputStream fis = null;
BufferedInputStream bis = null;
XMLSlideShow ppt = null;
BufferedImage img = null;
ByteArrayOutputStream out = null;
// HSLFSlideShow hslfSlideShow = new
// HSLFSlideShow(file.getAbsolutePath());
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
XSLFSlideShow slideShow = new XSLFSlideShow(
file.getAbsolutePath());
ppt = new XMLSlideShow(slideShow.getPackage());
XSLFPowerPointExtractor ppts = new XSLFPowerPointExtractor(ppt);
XSLFSlide[] slide = ppt.getSlides();
Dimension pgsize = ppt.getPageSize();
int slideNumber = slide.length;
// formatPptImageStyle(slide, 10);
img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.WHITE);
addImageType(graphics);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
pgsize.height));
slide[slideNumber - 1].draw(graphics);
ImageIO.write(img, "ppt", out);
graphics.dispose();
logger.info("Extra success!!");
return out.toByteArray();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OpenXML4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
private byte[] extractPicFromFile(String filePath) {
File file = new File(filePath);
if (isExistsFile(filePath)) {
FileInputStream fis = null;
BufferedInputStream bis = null;
SlideShow ppt = null;
BufferedImage img = null;
ByteArrayOutputStream out = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
ppt = new SlideShow(bis);
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
out = new ByteArrayOutputStream();
new ByteArrayOutputStream();
int slideNumber = slide.length;
formatPptImageStyle(slide, 10);
img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.WHITE);
addImageType(graphics);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
pgsize.height));
slide[slideNumber - 1].draw(graphics);
ImageIO.write(img, "ppt", out);
graphics.dispose();
logger.info("Extra success!!");
return out.toByteArray();
} catch (FileNotFoundException e) {
logger.error("Can't find the file!" + e.getMessage());
} catch (IOException e) {
logger.error("Number parse is error!" + e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
logger.error("close ppt file is error!" + e.getMessage());
}
}
}
return null;
}
public static void main(String[] args) throws Exception {
// String filePath = "D:\\true\\定语从句经典选择.pptx";
String filePath = "D:\\error\\415db9b201d3.ppt";
PptExtractPicFormat pepf = new PptExtractPicFormat();
byte[] byteSteam = pepf.extractPicFromFile(filePath);
// byte[] byteStream = pepf.extractPic(filePath);
// FileOutputStream fos = new FileOutputStream("d:\\dd.png");
// fos.write(byteSteam);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
提取Java文件中包含的字符串PoiFormatterTrace
共21个文件
jar:11个
java:3个
class:3个
0 下载量 101 浏览量
2024-10-28
14:05:24
上传
评论
收藏 22.38MB ZIP 举报
温馨提示
可练习,可商用,功能十分强大,可做课程设计
资源推荐
资源详情
资源评论
收起资源包目录
提取Java文件中包含的字符串PoiFormatterTrace.zip (21个子文件)
PoiFormatterTrace
lib
dom4j-1.6.1.jar 307KB
poi-3.8-beta5-sources.jar 1.64MB
ooxml-schemas-1.1.jar 14.15MB
poi-3.8-beta5.jar 1.71MB
poi-scratchpad-3.8-beta5-sources.jar 1.01MB
poi-ooxml-schemas-3.8-beta5.jar 4.45MB
commons-logging-4.0.6.jar 24KB
xmlbeans-2.3.0.jar 2.54MB
log4j-1.2.14.jar 359KB
poi-scratchpad-3.8-beta5.jar 1.13MB
poi-ooxml-3.8-beta5.jar 885KB
.classpath 1KB
src
log4j.properties 651B
com
clickoo
mercury
mail
attach
pptx
PptxExtractPicFormat.java 5KB
ppt
PptExtractPicFormat.java 6KB
io
ExtractString.java 3KB
bin
log4j.properties 651B
com
clickoo
mercury
mail
attach
pptx
PptxExtractPicFormat.class 6KB
ppt
PptExtractPicFormat.class 8KB
io
ExtractString.class 5KB
.project 393B
共 21 条
- 1
资源评论
小小哭包
- 粉丝: 2050
- 资源: 4206
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功