没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
转帖|行业资讯|编辑:王香|2016-12-29 10:14:24.000|阅读 1272 次
概述: ppt转成pdf,原理是ppt转成图片,再用图片生产pdf,过程有个问题,不管是ppt还是pptx,都遇到中文乱码,编程方框的问题,其中ppt后缀网上随便找就有解决方案,就是设置字体为统一字体,pptx如果页面是一种中文字体不会有问题,如果一个页面有微软雅黑和宋体,就会导致部分中文方框,怀疑是poi处理的时候,只读取第一种字体,所以导致多个中文字体乱码。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
百度和谷歌上,看到apache官网上有人说是bug,但他们回复说是字体问题,这个问题其实poi可以自己做,读取原来字体设置成当前字体,不过性能应该会有很多消耗,估计很多人花费大量时间找解决方案,但网上几乎没有现成的方案。经过多次尝试,最终为大家提供以下解决方法。
问题前的pptx转成图片:
解决后的pptx转成图片:
解决方法:
图取每个shape,将文字转成统一的字体,方案如下:
for( XSLFShape shape : slide[i].getShapes() ){ if ( shape instanceof XSLFTextShape ){ XSLFTextShape txtshape = (XSLFTextShape)shape ; System.out.println("txtshape" + (i+1) + ":" + txtshape.getShapeName()); System.out.println("text:" +txtshape.getText()); for ( XSLFTextParagraph textPara : txtshape.getTextParagraphs() ){ List<XSLFTextRun> textRunList = textPara.getTextRuns(); for(XSLFTextRun textRun: textRunList) { textRun.setFontFamily("宋体"); } } } }
完整代码如下(除了以上的解决方案,大部分是stackoverflow上的代码)
public static void convertPPTToPDF(String sourcepath, String destinationPath, String fileType) throws Exception { FileInputStream inputStream = new FileInputStream(sourcepath); double zoom = 2; AffineTransform at = new AffineTransform(); at.setToScale(zoom, zoom); Document pdfDocument = new Document(); PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(destinationPath)); PdfPTable table = new PdfPTable(1); pdfWriter.open(); pdfDocument.open(); Dimension pgsize = null; Image slideImage = null; BufferedImage img = null; if (fileType.equalsIgnoreCase(".ppt")) { SlideShow ppt = new SlideShow(inputStream); inputStream.close(); pgsize = ppt.getPageSize(); Slide slide[] = ppt.getSlides(); pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight())); pdfWriter.open(); pdfDocument.open(); for (int i = 0; i < slide.length; 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(); // String name = rtruns[l].getFontName(); rtruns[l].setFontIndex(1); rtruns[l].setFontName("宋体"); } } 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(); slideImage = Image.getInstance(img, null); table.addCell(new PdfPCell(slideImage, true)); } } if (fileType.equalsIgnoreCase(".pptx")) { XMLSlideShow ppt = new XMLSlideShow(inputStream); pgsize = ppt.getPageSize(); XSLFSlide slide[] = ppt.getSlides(); pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight())); pdfWriter.open(); pdfDocument.open(); for (int i = 0; i < slide.length; i++) { for( XSLFShape shape : slide[i].getShapes() ){ if ( shape instanceof XSLFTextShape ){ XSLFTextShape txtshape = (XSLFTextShape)shape ; // System.out.println("txtshape" + (i+1) + ":" + txtshape.getShapeName()); //System.out.println("text:" +txtshape.getText()); for ( XSLFTextParagraph textPara : txtshape.getTextParagraphs() ){ List<XSLFTextRun> textRunList = textPara.getTextRuns(); for(XSLFTextRun textRun: textRunList) { textRun.setFontFamily("宋体"); } } } } 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); // FileOutputStream out = new FileOutputStream("src/main/resources/test"+i+".jpg"); // javax.imageio.ImageIO.write(img, "jpg", out); graphics.getPaint(); slideImage = Image.getInstance(img, null); table.addCell(new PdfPCell(slideImage, true)); } } pdfDocument.add(table); pdfDocument.close(); pdfWriter.close(); System.out.println("Powerpoint file converted to PDF successfully"); }
maven配置:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <!-- <version>3.13</version> --> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <!-- <version>3.10-FINAL</version> --> <version>3.9</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.7</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <!-- <version>3.12</version> --> <version>3.9</version> </dependency>
本文转自:https://www.oschina.net/blog
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
在 Python 中读取 PDF 文档是实现文档自动化、内容分析和数据提取的基础操作之一。本文将介绍如何使用 Spire.PDF for Python 实现用 Python 读取 PDF 文档。该库提供简单易用、功能强大的API,无需依赖第三方工具。
持续测试平台Parasoft CTP通过一键克隆、自动配置和独立沙箱等功能,快速构建稳定一致的测试环境,自动适配虚拟资产,安全隔离高风险测试,高效解决传统测试环境搭建难题。
在Aspose.Words for .NET 25.6版本中,引入了一项新功能,允许您将多页文档导出为单个光栅图像。当您需要将文档作为单个可视文件共享或显示时,此功能非常有用。
Parasoft Insure++将代码覆盖率转化为可操作的洞察见解,让开发者能够有针对性地填补测试空白并优化执行效率,从而确保代码的每一个关键环节都经过严格验证。借助Insure++,团队能够从根本上提升软件的质量与可靠性,真正实现“代码测试无死角”。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号