国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法

2019-11-26 14:51:52
字體:
供稿:網(wǎng)友

ppt轉(zhuǎn)成pdf,原理是ppt轉(zhuǎn)成圖片,再用圖片生產(chǎn)pdf,過程有個問題,不管是ppt還是pptx,都遇到中文亂碼,編程方框的問題,其中ppt后綴網(wǎng)上隨便找就有解決方案,就是設(shè)置字體為統(tǒng)一字體,pptx如果頁面是一種中文字體不會有問題,如果一個頁面有微軟雅黑和宋體,就會導致部分中文方框,懷疑是poi處理的時候,只讀取第一種字體,所以導致多個中文字體亂碼。

百度和谷歌都找了很久,有看到說apache官網(wǎng)有人說是bug,但他們回復說是字體問題,這個問題其實我覺得poi可能可以自己做,讀取原來字體設(shè)置成當前字體,不過性能應該會有很多消耗,反正我估計很多人跟我一樣花費大量時間找解決方案,網(wǎng)上幾乎沒有現(xiàn)成的方案。自己也是一步步嘗試,最終找到解決辦法,ppt格式的就不說了網(wǎng)上找得到,pptx后綴的網(wǎng)上我是沒找到。

問題前的pptx轉(zhuǎn)成圖片:

解決后的pptx轉(zhuǎn)成圖片:

解決方法:
讀取每個shape,將文字轉(zhuǎn)成統(tǒng)一的字體,網(wǎng)上找到的那段代碼不可行,我自己改的方案如下:       

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>

上面就是為大家分享的java實現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法,希望對大家的學習有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 德惠市| 永年县| 九龙城区| 无锡市| 盐亭县| 青州市| 肥东县| 锡林浩特市| 繁峙县| 马尔康县| 南召县| 南投县| 台南市| 原平市| 延吉市| 南丹县| 镇赉县| 刚察县| 河西区| 手游| 凉城县| 长治市| 阳城县| 长武县| 剑阁县| 肇州县| 建德市| 洪雅县| 诸暨市| 奇台县| 常宁市| 河北省| 祁门县| 宝鸡市| 永嘉县| 柳江县| 封开县| 岳阳市| 信宜市| 丰县| 行唐县|