word,ppt等office文档转化为pdf进行展示(POI + iText)(亲测有效)
word,ppt等office软件转化为pdf进行展示(POI +iText)(亲测有效)
- 1. ppt转化为pdf,利用java的POI和itext进行转化
- 2.wordtopdf
- 3.处理pptx的中文乱码的问题
废话不多说,上代码
1. ppt转化为pdf,利用java的POI和itext进行转化
/*** 返回pdf文件*/public File convertPPTToPDF(File file, File toFile) {try {Document pdfDocument = new Document();PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(toFile));FileInputStream is = new FileInputStream(file);double zoom = 2;XMLSlideShow ppt = convertPPTToPDFByPPTX(is);if (ppt == null) {throw new NullPointerException("This PPTX get data is error....");}Dimension pgsize = ppt.getPageSize();XSLFSlide slide[] = ppt.getSlides();AffineTransform at = new AffineTransform();at.setToScale(zoom, zoom);pdfDocument.setPageSize(new com.itextpdf.text.Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));pdfWriter.open();pdfDocument.open();PdfPTable table = new PdfPTable(1);for (int i = 0; i < slide.length; i++) {for (XSLFShape shape : slide[i].getShapes()) {if (shape instanceof XSLFTextShape) {XSLFTextShape txtshape = (XSLFTextShape) shape;for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) {List<XSLFTextRun> textRunList = textPara.getTextRuns();for (XSLFTextRun textRun : textRunList) {textRun.setFontFamily("宋体");}}}}BufferedImage img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);Graphics2D graphics = img.createGraphics();graphics.setTransform(at);graphics.setPaint(Color.white);graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));slide[i].draw(graphics);graphics.getPaint();com.itextpdf.text.Image slideImage = Image.getInstance(img, null);table.addCell(new PdfPCell(slideImage, true));}pdfDocument.add(table);pdfDocument.close();pdfWriter.close();log.info(file.getAbsolutePath() + "Powerpoint file converted to PDF successfully");return toFile;} catch (Exception e) {log.info(file.getAbsolutePath() + "--->" + e.getMessage());return null;}}private XMLSlideShow convertPPTToPDFByPPTX(FileInputStream is) {try {return new XMLSlideShow(is);} catch (IOException e) {return null;}}
2.wordtopdf
/*** word to pdf */
public File convertWordToPDF(File file, File toFile) {try {// size of pdfDocumentDocument pdfDocument = new Document(PageSize.A3, 72, 72, 72, 72);PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(toFile));FileInputStream input_document = new FileInputStream(file);XWPFDocument doc = new XWPFDocument(input_document);//72 units=1 inchpdfWriter.setInitialLeading(20);//get all paragraphs from word docxList<XWPFParagraph> plist = doc.getParagraphs();//open pdf document for writingpdfWriter.open();pdfDocument.open();for (int i = 0; i < plist.size(); i++) {//read through the list of paragraphsXWPFParagraph pa = plist.get(i);//get all run objects from each paragraphList<XWPFRun> runs = pa.getRuns();//read through the run objectsfor (int j = 0; j < runs.size(); j++) {XWPFRun run = runs.get(j);//get pictures from the run and add them to the pdf documentList<XWPFPicture> piclist = run.getEmbeddedPictures();//traverse through the list and write each image to a fileIterator<XWPFPicture> iterator = piclist.iterator();while (iterator.hasNext()) {XWPFPicture pic = iterator.next();XWPFPictureData picdata = pic.getPictureData();byte[] bytepic = picdata.getData();Image imag = Image.getInstance(bytepic);pdfDocument.add(imag);}// 中文字体的解决 Font_Songti为字体的配置文件BaseFont bf = BaseFont.createFont(fileProperties.getPropertiesByKey("Font_Songti"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font font = new Font(bf, 15.0f, Font.NORMAL, BaseColor.BLACK);//construct unicode stringString text = run.getText(-1);byte[] bs;if (text != null) {bs = text.getBytes();String str = new String(bs);//add string to the pdf documentChunk chObj1 = new Chunk(str, font);pdfDocument.add(chObj1);}}//output new linepdfDocument.add(new Chunk(Chunk.NEWLINE));}//close pdf documentpdfDocument.close();pdfWriter.close();log.info(file.getAbsolutePath() + "Powerpoint file converted to PDF successfully");return toFile;} catch (Exception e) {log.info(file.getAbsolutePath() + "--->" + e.getMessage());return null;}}
3.处理pptx的中文乱码的问题
for (int i = 0; i < slide.length; i++) {for (XSLFShape shape : slide[i].getShapes()) {if (shape instanceof XSLFTextShape) {XSLFTextShape txtshape = (XSLFTextShape) shape;for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) {List<XSLFTextRun> textRunList = textPara.getTextRuns();for (XSLFTextRun textRun : textRunList) {textRun.setFontFamily("宋体");}}}}
word,ppt等office文档转化为pdf进行展示(POI + iText)(亲测有效)相关推荐
- 如何在谷歌Chrome新版浏览器中打开并编辑Word/Excel/PPT等Office文档?(视频演示)
如何在谷歌Chrome新版浏览器中打开并编辑Word/Excel/PPT等Office文档?
- Python 技术篇-利用Office VBA实现word文档转化为pdf文档实例演示
# -*- coding: UTF8 -*- from win32com.client import *def switch_pdf(path, name):'''作用:将word文档转化为pdf文档 ...
- java 将doc格式的文档转化为pdf格式(实现Word文件预览)
doc格式的文档要实现预览功能,首先要将文档转化为pdf格式的文档 doc文档转pdf文档步骤 导入jar包 将aspose-words-jdk16-14.6.0.0.jar包导入项目中: 创建一个格 ...
- android在线预览excel文件格式,Android打开doc、xlsx、ppt等office文档解决方案
原标题:Android打开doc.xlsx.ppt等office文档解决方案 1.Android端有什么控件可以直接显示office文档吗? 很抱歉没有,对于ios而言想要显示office文档,直接调 ...
- word转图片 java_Java 利用LibreOffice将Office文档转换成 PDF,进而转图片,实现在线预览功能...
项目中需要将 Office 文档上传并实现在线预览,用到了 LibreOffice 将 Office 文档转换为 PDF 文档,然后再用 pdfbox 将 PDF 转为图片. 本文介绍借助 Libre ...
- 文档格式转换(Office文档转成PDF或XPS)
最近在做微信云打印服务器,云服务器与微信云服务器通信己经OK,现在有一个很核心的功能需要实现,就是服务器端文档的格式转换 目标:将office.txt.图片格式的文档转成PDF 查阅了一些资料,格式转 ...
- [Office 2010 易宝典]怎样直接将Office文档保存为PDF格式?
大致内容 如何将Office文档保存问PDF格式: 通过菜单点击的方式把Microsoft Word文档另存为PDF文档. 操作步骤 1.点击"文件"菜单. 2.点击"另 ...
- Office文档转换成PDF
分享知识 传递快乐 Office文档转换成PDF 文档转换的方式有很多种,比如有Apache下的POI.jodconverter等技术.本例用的jodconverter-core-3.0技术对文档进行 ...
- Vue 预览word,excel,ppt等office文档-内网访问(基于onlyoffice,后端返回文件流)
Vue 预览word,excel等office 先看效果!! 需求背景:在前端页面中预览office文件且是内网访问,服务器不可访问外网的前提. 因此微软的接口就废掉了,因为他接口的条件是可以访问外网 ...
最新文章
- 输入字符串,包含数字,大小写字母,编程输出出现做多的数字的和
- 德国小哥1人“黑掉”谷歌地图:99部手机就能造成交通拥堵
- 中的挂起是什么意思_数字博物馆是什么意思?数字博物馆用到了哪些技术?
- HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
- 50道练习带你玩转Pandas
- c语言会员卡管理系统,路西牌会员管理系统。
- 让C/C++程序员告诉你什么叫浪漫,表白黑科技,炫酷多彩求爱利器
- python监控文件内容变化_Python监控文件内容变化
- PyCharm将制表符转换为空格并显示的配置方法
- 智能会议系统(24)---linphone-去掉蓝牙功能
- arm平台下linux c语言编程,简单分析针对ARM平台的C语言程序的编译问题
- 公共方法之运算符,len,del,max,min,range,enumerate
- C++ Primer 5th 第1章 开始
- c++读取文本文件里的指定位置的字符_(12)文本文件操作参考
- HTML5课题意义,毕业论文选题的意义万能套话
- 几何公差基础知识之平面度
- SpringBoot安全登录验证
- python打开json文件_python怎么读json文件
- springboot整合redis、mybatis、@EnableScheduling定时任务,实现日访问量与日活量的统计与记录
- 51单片机 | 蜂鸣器实验
热门文章
- Clover使用笔记(持续更新)
- 阿里系App抓包分析(三)
- 专家修炼-学习的方法
- 获取股票数据【使用JQData查询行情数据、财务指标、估值指标】
- 摘-连载-《青青子衿》(念才)-采葛-96
- Python wxpython篇 | Python生态库之图形用户界面开发库 “wxPython “ 的安装及使用(附. 使用pyinstaller 库打包Python随机点名小程序程序.exe文件)
- Java如何爬虫奥运奖牌榜数据,恭喜中国获得东京奥运会首金!
- 新站如何快速做SEO优化,获收录和排名
- 小学计算机听课总评,小学听课评课评语大全
- iOS和Android的app界面设计规范