java 使用apche poi 把ppt和pptx转换为图片,总有文字排版错误和部分表格无法显示

虚幻魔灵714 2016-06-30 02:20:46
目前碰到一些问题,使用apace poi 将ppt或pptx 转换成图片时总有部分文字排版错误和部分表格无法显示。
poi 使用的是3.11版,下面贴代码,求大神指教。拜谢。

...全文
1194 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shidilunji118848 2016-10-23
  • 打赏
  • 举报
回复
请问解决了吗 pptx的OK了 ppt格式的文字都是有方框 现在怎么解决的呢
初学者-java 2016-07-25
  • 打赏
  • 举报
回复
上面第二个问题是你的代码吗?
虚幻魔灵714 2016-06-30
  • 打赏
  • 举报
回复
源代码参考:http://blog.csdn.net/hwd15219166920/article/details/50252889
虚幻魔灵714 2016-06-30
  • 打赏
  • 举报
回复
import java.io.FileInputStream; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ResourceBundle; 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.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide; public class PPT2Img3 { private static final String EXTENSION_PPT = ".ppt"; private static final String EXTENSION_PPTX = ".pptx"; private static Log log = LogFactory.getLog(PPT2Img3.class); /*默认转换的图片格式*/ private static String imgtype = ""; /*保存图片路径*/ private static String imgpath = null; static{ ResourceBundle resourceBundle = ResourceBundle.getBundle("qdkConfig"); imgpath = resourceBundle.getString("ppt2imgpath"); imgtype = resourceBundle.getString("imgformat"); if(imgtype == null || imgtype.trim().equals("")){ imgtype = "jpg"; } try { //判断保存图片地址是否存在,否则创建目录 File mkFile = new File(imgpath); if (!mkFile.exists() && !mkFile.isDirectory()) { mkFile.mkdirs(); } } catch (Exception e) { log.error("PPT2Img类,保存图片地址错误!!!错误地址为:"+imgpath,e); } } /** * convertPPT2Image(将ppt转换为图片) * @param pptpath ppt文件路径 * @param imgpath 转换后图片的输出路径 * @param imgformat 转换的图片格式 * @return Integer 返回转换的图片数量 */ public static Integer convertPPT2Image(String pptpath,String imgpath,String imgformat){ try { if (pptpath.endsWith(EXTENSION_PPT)) { return ppt2003(pptpath,imgpath,imgformat); } else if (pptpath.endsWith(EXTENSION_PPTX)) { return ppt2007(pptpath,imgpath,imgformat); } else{ log.error("PPT2Img将ppt转换为图片错误:传入的文件不是ppt文件,"+pptpath); return -1; } } catch (Exception e) { log.error("PPT2Img将ppt转换为图片错误",e); return -1; } } @SuppressWarnings("deprecation") private static Integer ppt2007(String pptpath,String imgpath,String imgformat) throws IOException, XmlException{ //检查传入的ppt文件地址是否正确 String filename = preReadCheck(pptpath); FileInputStream orignalPPTFileInputStream = null; FileOutputStream orignalPPTFileOutputStream = null; XMLSlideShow oneSlideShow = null; Integer imgnum = 0; try{ orignalPPTFileInputStream=new FileInputStream(new File(pptpath)); oneSlideShow=new XMLSlideShow(orignalPPTFileInputStream); /*获得PPT每页的尺寸大小(宽和高度)*/ Dimension onePPTPageSize=oneSlideShow.getPageSize(); /*获得PPT文件中的所有的PPT页面,并转换为一张张的播放片*/ XSLFSlide[]pptPageXSLFSlideArray=oneSlideShow.getSlides(); /*下面的XML配置文件定义转换后的图片内的文字字体,否则将会出现转换后 的图片内的中文为乱码 */ String xmlFontFormat1="<xml-fragment xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"; String xmlFontFormat2= "<a:rPr lang=\"zh-CN\" altLang=\"en-US\" dirty=\"0\" smtClean=\"0\">"; String xmlFontFormat3="<a:latin typeface=\"微软雅黑\" pitchFamily=\"34\" charset=\"-122\"/>"; String xmlFontFormat31 = "<a:ea typeface=\"微软雅黑\" pitchFamily=\"34\" charset=\"-122\"/>"; String xmlFontFormat4= "</a:rPr>"; String xmlFontFormat5="</xml-fragment>"; StringBuffer xmlFontFormatStringBuffer=new StringBuffer(); xmlFontFormatStringBuffer.append(xmlFontFormat1); xmlFontFormatStringBuffer.append(xmlFontFormat2); xmlFontFormatStringBuffer.append(xmlFontFormat3); xmlFontFormatStringBuffer.append(xmlFontFormat31); xmlFontFormatStringBuffer.append(xmlFontFormat4); xmlFontFormatStringBuffer.append(xmlFontFormat5); imgnum = pptPageXSLFSlideArray.length; for(int pptPageXSLFSlideIndex=0;pptPageXSLFSlideIndex< pptPageXSLFSlideArray.length; pptPageXSLFSlideIndex++){ /*设置字体为宋体,解决中文乱码问题*/ CTSlide oneCTSlide = pptPageXSLFSlideArray[pptPageXSLFSlideIndex].getXmlObject(); CTGroupShape oneCTGroupShape = oneCTSlide.getCSld().getSpTree(); CTShape [] oneCTShapeArray = oneCTGroupShape.getSpArray(); for(CTShape oneCTShape : oneCTShapeArray){ CTTextBody oneCTTextBody = oneCTShape.getTxBody(); if(null == oneCTTextBody){ continue;} CTTextParagraph [] oneCTTextParagraph = oneCTTextBody.getPArray(); CTTextFont oneCTTextFont = null; oneCTTextFont = CTTextFont.Factory.parse(xmlFontFormatStringBuffer.toString()); for(CTTextParagraph textParagraph : oneCTTextParagraph){ CTRegularTextRun [] oneCTRegularTextRunArray = textParagraph.getRArray(); for(CTRegularTextRun oneCTRegularTextRun : oneCTRegularTextRunArray){ CTTextCharacterProperties oneCTTextCharacterProperties = oneCTRegularTextRun.getRPr(); oneCTTextCharacterProperties.setLatin(oneCTTextFont); } } } /*创建BufferedImage对象,图象的尺寸为原来PPT的每页的尺寸*/ BufferedImage oneBufferedImage=new BufferedImage(onePPTPageSize.width, onePPTPageSize.height,BufferedImage.TYPE_INT_RGB); Graphics2D oneGraphics2D=oneBufferedImage.createGraphics(); oneGraphics2D.setPaint(Color.white); oneGraphics2D.fill(new Rectangle2D.Float(0, 0, onePPTPageSize.width,onePPTPageSize.height)); /*将PPT文件中的每个页面中的相关内容画到转换后的图片中*/ pptPageXSLFSlideArray[pptPageXSLFSlideIndex].draw(oneGraphics2D); /*设置图片的存放路径和图片的格式,注意生成的文件路径为绝对路径,最 终获得各个图像文件所对应的输出流对象 */ orignalPPTFileOutputStream=new FileOutputStream(imgpath + "/" + filename + "_"+ pptPageXSLFSlideIndex + "."+imgformat); ImageIO.write(oneBufferedImage, imgformat, orignalPPTFileOutputStream); } }finally{ try{ if(orignalPPTFileInputStream!=null){ orignalPPTFileInputStream.close(); } if(orignalPPTFileOutputStream!=null){ orignalPPTFileOutputStream.close(); } } catch(IOException e){ e.printStackTrace(); } } return imgnum; } /** * ppt2003(ppt2003转换为图片) * @param pptpath ppt文件路径 * @param imgpath 转换后图片的输出路径 * @param imgformat 转换的图片格式 * @return Integer 返回转换的图片数量 */ private static Integer ppt2003(String pptpath,String imgpath,String imgformat) throws IOException{ //检查传入的ppt文件地址是否正确 String filename = preReadCheck(pptpath); FileInputStream orignalPPTFileInputStream=null; FileOutputStream orignalPPTFileOutputStream=null; SlideShow oneSlideShow=null; Integer imgnum = 0; try{ orignalPPTFileInputStream=new FileInputStream(pptpath); oneSlideShow=new SlideShow(orignalPPTFileInputStream); /** *获得PPT每页的尺寸大小(宽和高度)*/ Dimension onePPTPageSize=oneSlideShow.getPageSize(); /** *获得PPT文件中的所有的PPT页面(获得每一张幻灯片),并转换为一张张 的播放片 */ Slide[]pptPageSlideArray=oneSlideShow.getSlides(); /** *下面的循环的主要功能是实现对PPT文件中的每一张幻灯片进行转换和操 作。 */ imgnum = pptPageSlideArray.length; for(int pptPageSlideIndex=0; pptPageSlideIndex<pptPageSlideArray.length; pptPageSlideIndex++){ TextRun[]textRunsArray=pptPageSlideArray[pptPageSlideIndex].getTextRuns(); for(int textRunsArrayIndex=0;textRunsArrayIndex<textRunsArray.length; textRunsArrayIndex++){ RichTextRun[] pptRichTextRunsArray = textRunsArray[textRunsArrayIndex].getRichTextRuns(); for(int pptRichTextRunsArrayIndex=0;pptRichTextRunsArrayIndex< pptRichTextRunsArray.length;pptRichTextRunsArrayIndex++){ pptRichTextRunsArray[pptRichTextRunsArrayIndex].setFontIndex(1); pptRichTextRunsArray[pptRichTextRunsArrayIndex].setFontName("宋体"); / * * 但如果 PPT 文件在 WPS 中保存过,则 pptRichTextRunsArray[pptRichTextRunsArrayIndex].getFontSize()的值可能为0或者26040。 因此首先识别当前文本框内的字体尺寸是否为0或者大于26040,则 设置默认的字体尺寸。 */ int currentFontSize = pptRichTextRunsArray[pptRichTextRunsArrayIndex].getFontSize(); if((currentFontSize<=0)||(currentFontSize>=26040)){ pptRichTextRunsArray[pptRichTextRunsArrayIndex].setFontSize(30); } } } /** *创建BufferedImage对象,图象的尺寸为原来PPT的每页的尺寸*/ BufferedImage oneBufferedImage=new BufferedImage(onePPTPageSize.width, onePPTPageSize.height,BufferedImage.TYPE_INT_RGB); Graphics2D oneGraphics2D=oneBufferedImage.createGraphics(); oneGraphics2D.fill(new Rectangle2D.Float(0,0,onePPTPageSize.width, onePPTPageSize.height)); pptPageSlideArray[pptPageSlideIndex].draw(oneGraphics2D); orignalPPTFileOutputStream=new FileOutputStream(imgpath + "/" + filename + "_"+ pptPageSlideIndex + "."+imgformat); ImageIO.write(oneBufferedImage, imgformat, orignalPPTFileOutputStream); } }finally{ try{ if(orignalPPTFileInputStream!=null){ orignalPPTFileInputStream.close(); } if(orignalPPTFileOutputStream!=null){ orignalPPTFileOutputStream.close(); } } catch(IOException e){ e.printStackTrace(); } } return imgnum; } //文件检查,如果存在则返回文件名 private static String preReadCheck(String path) throws FileNotFoundException { File file = new File(path); if (!file.exists()) { throw new FileNotFoundException("传入的文件不存在:" + path); } String filename = file.getName(); return filename.substring(0,filename.lastIndexOf(".")); } }

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧